Sha256: 088f6250cab4f6f12c93a79a212ae664303f00a612763b2f3c93cc8c40a419de

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {

  static targets = ['listItem', 'category', 'recommended']

  filter(event) {
    const query = event.currentTarget.value.toLowerCase()
    const visibleCategories = new Set()

    // hide / show listItem links that match the query and note which
    // categories should be visible
    this.listItemTargets.forEach(listItemNode => {
      const listItemName = listItemNode.dataset.listItemValue.toLowerCase()
      if (listItemName.includes(query)) {
        listItemNode.hidden = false
        visibleCategories.add(listItemNode.dataset.category)
      } else {
        listItemNode.hidden = true
      }
    })

    this.recommendedTargets.forEach(recommendedNode => {
      (query !== '' && visibleCategories.size === 0) ? recommendedNode.hidden = true : recommendedNode.hidden = false
    })

    // hide / show category headers that have visible listItems
    this.categoryTargets.forEach(categoryNode => {
      const categoryName = categoryNode.innerHTML
      if (visibleCategories.has(categoryName)) {
        categoryNode.hidden = false
      } else {
        categoryNode.hidden = true
      }
    })
  }
}

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
refine-rails-2.12.0 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.12 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.11 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.10 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.9 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.8 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.6 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.5 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.4 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.3 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.2 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.1 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.11.0 app/javascript/controllers/refine/typeahead-list-controller.js
refine-rails-2.10.1 app/javascript/controllers/refine/typeahead-list-controller.js