Sha256: ff29bda8564001752357160f2c9c96a2543396199033a0398327cac26432308b

Contents?: true

Size: 1.35 KB

Versions: 15

Compression:

Stored size: 1.35 KB

Contents

import {controllerFactory} from '@utils/createController'
import {useIntersection} from 'stimulus-use'

export default class InputFilterController extends controllerFactory()({
  targets: {
    emptyRoot: null,
    input: HTMLInputElement,
    searchString: null,
  },
}) {
  private items: {el: HTMLElement; searchString: string}[]

  private handleNewQuery(query: string) {
    let foundSomething = false

    for (const {el, searchString} of this.items) {
      const contains = searchString.includes(query.trim().toLowerCase())

      if (!foundSomething && contains) foundSomething = true

      if (contains) {
        el.classList.remove('ariadne-hidden')
      } else {
        el.classList.add('ariadne-hidden')
      }
    }

    if (foundSomething) {
      this.emptyRootTarget.classList.add('ariadne-hidden')
    } else {
      this.emptyRootTarget.classList.remove('ariadne-hidden')
    }
  }

  connect() {
    this.items = this.searchStringTargets.map((el: HTMLElement) => {
      return {
        el,
        searchString: (el.textContent ?? '').trim().toLowerCase(),
      }
    })
    useIntersection(this)
  }

  appear(entry, observer) {
    this.reset()
  }

  handleInput() {
    if (this.hasSearchStringTarget) this.handleNewQuery(this.inputTarget.value)
  }

  reset() {
    this.handleNewQuery('')
    if (this.hasInputTarget) this.inputTarget.value = ''
  }
}

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.86.6 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.86.5 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.86.4 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.86.3 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.86.2 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.86.1 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.86 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.85 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.84 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.83.3 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.83.2 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.83.1 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.83 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.82 app/components/ariadne/ui/list/component.ts
ariadne_view_components-0.0.81 app/components/ariadne/ui/list/component.ts