Add form field update capability to toggle components
This commit is contained in:
25
app/javascript/controllers/settings/toggle_controller.js
Normal file
25
app/javascript/controllers/settings/toggle_controller.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = [ "button", "switch", "input" ]
|
||||
static values = { switchEnabled: Boolean }
|
||||
|
||||
toggleSwitch () {
|
||||
this.switchEnabledValue = !this.switchEnabledValue
|
||||
this.inputTarget.value = this.switchEnabledValue.toString()
|
||||
|
||||
if (this.switchEnabledValue) {
|
||||
this.buttonTarget.setAttribute("aria-checked", "true");
|
||||
this.buttonTarget.classList.remove("bg-gray-200")
|
||||
this.buttonTarget.classList.add("bg-blue-600")
|
||||
this.switchTarget.classList.remove("translate-x-0")
|
||||
this.switchTarget.classList.add("translate-x-5")
|
||||
} else {
|
||||
this.buttonTarget.setAttribute("aria-checked", "false");
|
||||
this.buttonTarget.classList.remove("bg-blue-600")
|
||||
this.buttonTarget.classList.add("bg-gray-200")
|
||||
this.switchTarget.classList.remove("translate-x-5")
|
||||
this.switchTarget.classList.add("translate-x-0")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user