Files

35 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2024-09-06 19:30:49 +03:00
export default class extends HTMLElement {
connectedCallback () {
this.input.addEventListener('change', (event) => {
2026-02-20 18:01:12 +02:00
if (!this.target) return
const value = event.target.type === 'checkbox' ? event.target.checked : event.target.value
const dataValue = this.dataset.value === 'false' ? false : this.dataset.value || true
2024-09-06 19:30:49 +03:00
if (this.dataset.attribute) {
2026-02-20 18:01:12 +02:00
this.target[this.dataset.attribute] = value === dataValue
2024-09-06 19:30:49 +03:00
}
if (this.dataset.className) {
2026-02-20 18:01:12 +02:00
this.target.classList.toggle(this.dataset.className, value !== dataValue)
2024-09-06 19:30:49 +03:00
if (this.dataset.className === 'hidden' && this.target.tagName === 'INPUT') {
this.target.disabled = event.target.value !== this.dataset.value
}
}
if (this.dataset.attribute === 'disabled') {
this.target.value = ''
}
})
}
get input () {
return this.querySelector('input[type="checkbox"]') || this.querySelector('select')
}
get target () {
return document.getElementById(this.dataset.targetId)
}
}