Sha256: 0491cacf21b8de16482cb47748437e1b8ec0c65663d3c4716fd6c110fa40b13c
Contents?: true
Size: 1.15 KB
Versions: 19
Compression:
Stored size: 1.15 KB
Contents
import { Controller } from "@hotwired/stimulus" export default class extends Controller { static targets = [ "list" ] static classes = [ "active", "selected" ] initialize() { this.filter = debounce(this.filter.bind(this), 300) } filter({ target }) { this.#reset() if (target.value != "") { this.#selectMatches(target.value) this.#activate() } else { this.#deactivate() } } #reset() { this.listTarget.querySelectorAll(`.${this.selectedClass}`).forEach((element) => { element.classList.remove(this.selectedClass) }) } #selectMatches(value) { this.listTarget.querySelectorAll(`[data-value*="${value.toLowerCase()}"]`).forEach((element) => { element.classList.add(this.selectedClass) }) } #activate() { this.listTarget.classList.add(this.activeClass) this.dispatch("after") } #deactivate() { this.listTarget.classList.remove(this.activeClass) this.dispatch("after") } } // Helpers function debounce(fn, delay = 1000) { let timeoutId = null return (...args) => { clearTimeout(timeoutId) timeoutId = setTimeout(() => fn.apply(this, args), delay) } }
Version data entries
19 entries across 19 versions & 1 rubygems