Refactor toggles to work without JS, add specs

This commit is contained in:
Râu Cao
2023-03-15 14:04:12 +07:00
committed by Gitea
parent ca1221e9f3
commit fa56d6b772
12 changed files with 86 additions and 50 deletions

View File

@@ -1,12 +1,17 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "button", "switch", "input" ]
static targets = [ "button", "switch", "checkbox" ]
static values = { switchEnabled: Boolean }
connect () {
this.buttonTarget.classList.remove("hidden")
this.checkboxTarget.classList.add("hidden")
}
toggleSwitch () {
this.switchEnabledValue = !this.switchEnabledValue
this.inputTarget.value = this.switchEnabledValue.toString()
this.checkboxTarget.checked = this.switchEnabledValue
if (this.switchEnabledValue) {
this.buttonTarget.setAttribute("aria-checked", "true");