Created
September 12, 2023 20:50
-
-
Save aseroff/f99c3104f6e494645fbe53770d911575 to your computer and use it in GitHub Desktop.
toggle stimulus controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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