Skip to content

Instantly share code, notes, and snippets.

@aseroff
Created September 12, 2023 20:50
Show Gist options
  • Save aseroff/f99c3104f6e494645fbe53770d911575 to your computer and use it in GitHub Desktop.
Save aseroff/f99c3104f6e494645fbe53770d911575 to your computer and use it in GitHub Desktop.
toggle stimulus controller
import { Controller } from "@hotwired/stimulus"
export default class ToggleableController extends Controller {
static targets = [ 'toggle', 'enableSection', 'disableSection', 'hideSection' ]
toggle() {
let checked = this.toggleTarget.checked
if (this.hasEnableSectionTarget) {
this.enableSectionTarget.disabled = !checked
if (!checked) {
this.enableSectionTarget.querySelectorAll("input, select").forEach(ele => {ele.value = ''});
this.enableSectionTarget.querySelectorAll("input[type=checkbox").forEach(ele => { ele.checked = false });
}
}
if (this.hasDisableSectionTarget) {
this.disableSectionTarget.disabled = checked
if (checked) {
this.disableSectionTarget.querySelectorAll("input, select").forEach(ele => {ele.value = '' });
this.disableSectionTarget.querySelectorAll("input[type=checkbox").forEach(ele => { ele.checked = false });
}
}
if (this.hasHideSectionTarget) {
this.hideSectionTarget.hidden = !checked
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment