Controlling the manage filter section with JS too

This commit is contained in:
Jorge Manrubia
2025-09-18 16:24:25 +02:00
parent 408374ff33
commit 7a916c295a
8 changed files with 59 additions and 11 deletions
@@ -2,7 +2,10 @@ import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "label", "item", "hiddenField" ]
static values = { selectPropertyName: { type: String, default: "aria-checked" } }
static values = {
selectPropertyName: { type: String, default: "aria-checked" },
defaultValue: String
}
connect() {
this.labelTarget.textContent = this.#selectedLabel
@@ -0,0 +1,36 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static classes = ["filtersSet"]
static targets = ["field"]
connect() {
this.change()
}
change() {
this.#toggleFiltersSetClass()
}
#toggleFiltersSetClass(shouldAdd) {
this.element.classList.toggle(this.filtersSetClass, this.#hasFiltersSet)
}
get #hasFiltersSet() {
return this.fieldTargets.some(field => this.#isFieldSet(field))
}
#isFieldSet(field) {
const value = field.value?.trim()
if (!value) return false
const defaultValue = this.#defaultValueForField(field)
return defaultValue ? value !== defaultValue : true
}
#defaultValueForField(field) {
const comboboxContainer = field.closest("[data-combobox-default-value]")
return comboboxContainer?.dataset?.comboboxDefaultValue
}
}