|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | + |
| 3 | +// Conditionally shows/hides form fields based on a radio button's selected value. |
| 4 | +// |
| 5 | +// Usage: |
| 6 | +// <div data-controller="strata--conditional-field" |
| 7 | +// data-strata--conditional-field-source-value="form_model[field_name]" |
| 8 | +// data-strata--conditional-field-match-value="true" |
| 9 | +// hidden> |
| 10 | +// <!-- conditional content --> |
| 11 | +// </div> |
| 12 | +// |
| 13 | +// The `source` value is the `name` attribute of the radio button group to observe. |
| 14 | +// The `match` value is a comma-separated list of values that make this section visible. |
| 15 | +export default class extends Controller { |
| 16 | + static values = { |
| 17 | + source: String, |
| 18 | + match: String, |
| 19 | + clear: { type: Boolean, default: false } |
| 20 | + } |
| 21 | + |
| 22 | + connect() { |
| 23 | + this.radioButtons = document.querySelectorAll(`input[type="radio"][name="${this.sourceValue}"]`) |
| 24 | + this.boundToggle = this.toggle.bind(this) |
| 25 | + |
| 26 | + this.radioButtons.forEach((radio) => { |
| 27 | + radio.addEventListener("change", this.boundToggle) |
| 28 | + }) |
| 29 | + |
| 30 | + this.toggle() |
| 31 | + } |
| 32 | + |
| 33 | + disconnect() { |
| 34 | + this.radioButtons.forEach((radio) => { |
| 35 | + radio.removeEventListener("change", this.boundToggle) |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + toggle() { |
| 40 | + const selected = document.querySelector(`input[type="radio"][name="${this.sourceValue}"]:checked`) |
| 41 | + const selectedValue = selected ? selected.value : null |
| 42 | + const matchValues = this.matchValue.split(",") |
| 43 | + |
| 44 | + if (selectedValue && matchValues.includes(selectedValue)) { |
| 45 | + this.show() |
| 46 | + } else { |
| 47 | + this.hide() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + show() { |
| 52 | + this.element.hidden = false |
| 53 | + this.element.removeAttribute("aria-hidden") |
| 54 | + this.enableInputs() |
| 55 | + } |
| 56 | + |
| 57 | + hide() { |
| 58 | + this.element.hidden = true |
| 59 | + this.element.setAttribute("aria-hidden", "true") |
| 60 | + this.disableInputs() |
| 61 | + |
| 62 | + if (this.clearValue) { |
| 63 | + this.clearInputs() |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + enableInputs() { |
| 68 | + this.element.querySelectorAll("input, select, textarea").forEach((input) => { |
| 69 | + input.disabled = false |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + disableInputs() { |
| 74 | + this.element.querySelectorAll("input, select, textarea").forEach((input) => { |
| 75 | + input.disabled = true |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + clearInputs() { |
| 80 | + this.element.querySelectorAll("input, select, textarea").forEach((input) => { |
| 81 | + if (input.type === "radio" || input.type === "checkbox") { |
| 82 | + input.checked = false |
| 83 | + } else { |
| 84 | + input.value = "" |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
0 commit comments