@github/filter-input-element

中文文档
0.1.1发布于 2019/12/11MITTypeScriptESM + CJS零依赖近 30 天下载 2489

Display elements in a subtree that match filter input text.

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. Elements and attributes
  4. Required
  5. Optional
  6. Methods
  7. Events
  8. Browser support
  9. Development
  10. License

<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.

版本与发布记录

@github/filter-input-element 共发布 2 个版本,首次发布于 2019/11/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.1latest2019/12/116 年前
0.1.02019/11/286 年前

关于 @github/filter-input-element 的常见问题

@github/filter-input-element 是什么?

Display elements in a subtree that match filter input text.

如何安装 @github/filter-input-element?

在项目目录运行 npm install @github/filter-input-element,或使用 pnpm add / yarn add / bun add。

@github/filter-input-element 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@github/filter-input-element 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@github/filter-input-element 可以商用吗?

@github/filter-input-element 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@github/filter-input-element 的下载量有多少?

根据 npm 官方下载统计,@github/filter-input-element 在 2026/10/17 – 2026/11/15 这 30 天内被下载 2,489 次,日均约 83 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@github/filter-input-element 还在维护吗?

最新版本 0.1.1 发布于 2019/12/11(6 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/11/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。