Display elements in a subtree that match filter input text.
Перейти к файлу
dependabot[bot] efd29955f8
Bump the npm_and_yarn group with 7 updates (#26)
Bumps the npm_and_yarn group with 7 updates:

| Package | From | To |
| --- | --- | --- |
| [@75lb/deep-merge](https://github.com/75lb/deep-merge) | `1.1.1` | `1.1.2` |
| [ip](https://github.com/indutny/node-ip) | `1.1.9` | `removed` |
| [@open-wc/semantic-dom-diff](https://github.com/open-wc/open-wc/tree/HEAD/packages/semantic-dom-diff) | `0.20.0` | `0.20.1` |
| [@web/test-runner](https://github.com/modernweb-dev/web/tree/HEAD/packages/test-runner) | `0.17.2` | `0.19.0` |
| [@web/test-runner-playwright](https://github.com/modernweb-dev/web/tree/HEAD/packages/test-runner-playwright) | `0.10.2` | `0.11.0` |
| [ws](https://github.com/websockets/ws) | `7.5.9` | `7.5.10` |
| [rollup](https://github.com/rollup/rollup) | `3.29.4` | `4.22.4` |


Updates `@75lb/deep-merge` from 1.1.1 to 1.1.2
- [Commits](https://github.com/75lb/deep-merge/compare/v1.1.1...v1.1.2)

Removes `ip`

Updates `@open-wc/semantic-dom-diff` from 0.20.0 to 0.20.1
- [Release notes](https://github.com/open-wc/open-wc/releases)
- [Changelog](https://github.com/open-wc/open-wc/blob/master/packages/semantic-dom-diff/CHANGELOG.md)
- [Commits](https://github.com/open-wc/open-wc/commits/@open-wc/semantic-dom-diff@0.20.1/packages/semantic-dom-diff)

Updates `@web/test-runner` from 0.17.2 to 0.19.0
- [Release notes](https://github.com/modernweb-dev/web/releases)
- [Changelog](https://github.com/modernweb-dev/web/blob/master/packages/test-runner/CHANGELOG.md)
- [Commits](https://github.com/modernweb-dev/web/commits/@web/test-runner@0.19.0/packages/test-runner)

Updates `@web/test-runner-playwright` from 0.10.2 to 0.11.0
- [Release notes](https://github.com/modernweb-dev/web/releases)
- [Changelog](https://github.com/modernweb-dev/web/blob/master/packages/test-runner-playwright/CHANGELOG.md)
- [Commits](https://github.com/modernweb-dev/web/commits/@web/test-runner-playwright@0.11.0/packages/test-runner-playwright)

Updates `ws` from 7.5.9 to 7.5.10
- [Release notes](https://github.com/websockets/ws/releases)
- [Commits](https://github.com/websockets/ws/compare/7.5.9...7.5.10)

Updates `rollup` from 3.29.4 to 4.22.4
- [Release notes](https://github.com/rollup/rollup/releases)
- [Changelog](https://github.com/rollup/rollup/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rollup/rollup/compare/v3.29.4...v4.22.4)

---
updated-dependencies:
- dependency-name: "@75lb/deep-merge"
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: ip
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: "@open-wc/semantic-dom-diff"
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: "@web/test-runner"
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: "@web/test-runner-playwright"
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: ws
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: rollup
  dependency-type: indirect
  dependency-group: npm_and_yarn
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-30 10:49:06 -05:00
.github/workflows Create .github/workflows/publish.yml 2022-03-30 13:01:34 +01:00
examples Use unpkg 2019-11-26 16:53:38 -05:00
src use new standards for CEs 2023-10-20 12:37:07 +01:00
test use new standards for CEs 2023-10-20 12:37:07 +01:00
.eslintignore Convert to typescript setup 2019-11-25 17:28:31 -05:00
.eslintrc.json use new standards for CEs 2023-10-20 12:37:07 +01:00
.gitignore Initial commit 2019-11-25 14:58:27 -05:00
CODEOWNERS move AOR to primer 2022-09-23 18:36:07 +01:00
CODE_OF_CONDUCT.md Fix repo name 2020-01-21 16:57:58 -05:00
CONTRIBUTING.md Fix repo name 2020-01-21 16:57:58 -05:00
LICENSE Initial commit 2019-11-25 14:58:27 -05:00
README.md Add actions CI badge 2019-11-27 16:11:48 -05:00
SECURITY.md Add files in preparation for open source 2020-01-21 11:54:07 -05:00
custom-elements.json use new standards for CEs 2023-10-20 12:37:07 +01:00
package-lock.json Bump the npm_and_yarn group with 7 updates (#26) 2024-09-30 10:49:06 -05:00
package.json Bump the npm_and_yarn group with 7 updates (#26) 2024-09-30 10:49:06 -05:00
tsconfig.json use new standards for CEs 2023-10-20 12:37:07 +01:00
web-test-runner.config.js use new standards for CEs 2023-10-20 12:37:07 +01:00

README.md

<filter-input> element

Display elements in a subtree that match filter input text.

Installation

$ npm install @github/filter-input-element

Usage

<filter-input aria-owns="robots">
  <label>
    Filter robots
    <input type="text" autofocus autocomplete="off">
  </label>
</filter-input>
<div id="robots">
  <ul data-filter-list>
    <li>Bender</li>
    <li>Hubot</li>
    <li>Wall-E</li>
    <li>BB-8</li>
    <li>R2-D2</li>
  </ul>
  <p data-filter-empty-state hidden>0 robots found.</p>
</div>

Elements and attributes

Required

  • filter-input[aria-owns] should point to the container ID that wraps all <filter-input> related elements.
  • filter-input should have one input child element that is used to filter.
  • [id] should be set on a container that either contains or has [data-filter-list] attribute.
  • [data-filter-list] should be set on the element whose direct child elements are to be filtered.

Optional

Specify filter text

Use [data-filter-item-text] to specify an element whose text should be used for filtering. In the following example, the text (current) would not be matched.

<div data-filter-list>
  <a href="/bender">Bender</a>
  <a href="/hubot">
    <span data-filter-item-text>Hubot</span>
    (current)
  </a>
</div>

Blankslate

Use [data-filter-empty-state] to specify an element to be displayed when no results were found. This element must be inside of the container aria-owns points to.

<div id="filter-list">
  <div data-filter-list>
    <a href="/bender">Bender</a>
    <a href="/hubot">Hubot</a>
  </div>
  <div data-filter-empty-state hidden>No results found.</div>
</div>

Create new item

Use [data-filter-new-item] to include an item to create a new instance when no exact match were found. The element with [data-filter-new-text]'s text content will be set to the input value. You can also use [data-filter-new-value] to set an input value to the query param.

<div id="filter-list">
  <div data-filter-list>
    <a href="/bender">Bender</a>
    <a href="/hubot">Hubot</a>
  </div>
  <form action="/new" data-filter-new-item hidden>
    <button name="robot" data-filter-new-item-value>
      Create robot "<span data-filter-new-item-text></span>"
    </button>
  </form>
</div>

Methods

filterInputElement.filter can be optionally set to provide an alternative filtering logic. The default is substring.

const fuzzyFilterInput = document.querySelector('.js-fuzzy-filter-input')
fuzzyFilterInput.filter = (element, elementText, query) => {
  // fuzzy filtering logic
  return {match: boolean, hideNew: boolean}
}

match(required) indicates whether the item should be shown. hideNew (optional) will determine whether the "Create new item" element should be hidden. For example, when an exact match is found, the "create new item" option should be hidden.

Events

  • filter-input-start (bubbles) - fired on <filter-input> when a filter action is about to start.
  • filter-input-updated (bubbles) - fired on <filter-input> when filter has finished. event.detail.count is the number of matches found, and event.detail.total is the total number of elements.

To ensure that the client side update is communicated to assistive technologies, filter-input-updated event can be used to update filter results to screen readers. For example:

const ariaLiveContainer = document.querySelector('[aria-live="assertive"]')
document.addEventListener('filter-input-updated', event => {
  ariaLiveContainer.textContent = `${event.detail.count} results found.`
})

For more details on this technique, check out Improving accessibility on GOV.UK search.

Browser support

Browsers without native custom element support require a polyfill.

  • Chrome
  • Firefox
  • Safari
  • Microsoft Edge

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.