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

Version Path
css-zero-0.0.69 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.68 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.67 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.66 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.65 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.64 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.63 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.62 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.61 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.60 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.59 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.58 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.57 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.56 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.55 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.54 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.53 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.52 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js
css-zero-0.0.51 lib/generators/css_zero/add/templates/app/javascript/controllers/filter_controller.js