Files
myep/assets/controllers/select_toggle_controller.js
T

25 lines
818 B
JavaScript

import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static classes = [ 'hidden' ]
static targets = [ 'container' ]
static values = { show: Array, visible: Boolean }
toggle(event) {
let fieldType = event.target.type
let formValue = event.target.value
let showContainer
if ('checkbox' !== fieldType && 'radio' !== fieldType) {
this.visibleValue = this.showValue.indexOf(formValue) !== -1
} else {
let toggleValue = event.target.dataset.selectToggleValue
this.visibleValue = true === event.target.checked && this.showValue.includes(toggleValue)
}
}
visibleValueChanged(visible) {
this.containerTarget.classList.toggle(this.hiddenClass, false === visible)
}
}