@anypoint-web-components/anypoint-autocomplete

中文文档
0.2.13发布于 2021/10/16Apache-2.0ESM + CJS近 30 天下载 3283

Anypoint styled autocomplete for inputs.

原始使用文档
文档目录12 节
  1. Usage
  2. Installation
  3. In a HTML document
  4. Asynchronous suggestions
  5. In a LitElement
  6. Suggestions model
  7. Accessibility
  8. Your code
  9. After initialization
  10. Development
  11. Running the demo locally
  12. Running the tests

Deprecated

This component has been moved to anypoint-web-components/awc.


This component is based on Material Design lists.

Anypoint web components are set of components that allows to build Anypoint enabled UI in open source projects.

The element renders accessible list of suggestions for an input field.

Build Status

tests

Usage

Installation

npm install --save @anypoint-web-components/anypoint-autocomplete

In a HTML document

<html>
  <head>
    <script type="module">
      import '@anypoint-web-components/anypoint-autocomplete/anypoint-autocomplete.js';
      import '@anypoint-web-components/anypoint-input/anypoint-input.js';
    </script>
  </head>
  <body>
    <div class="parent">
      <anypoint-input id="targetInput"></anypoint-input>
      <anypoint-autocomplete target="targetInput"></anypoint-autocomplete>
    </div>

    <script>
    {
      document.querySelector('anypoint-autocomplete').source = [
        'a',
        'b',
        'c',
        'd'
      ];
    }
    </script>
  </body>
</html>

Asynchronous suggestions

When the input value changes the autocomplete dispatches query event. Your application should handle this event, generate suggestions for the user, and set the source property.

To indicate to the user that the suggestions are async you may set loader property. It renders a progress bar until source property change.

<div class="parent">
  <anypoint-input id="targetInput"></anypoint-input>
  <anypoint-autocomplete target="targetInput"></anypoint-autocomplete>
</div>
<script>
{
  document.querySelector('anypoint-autocomplete').onquery = (e) => {
    const { value } = e.detail;
    const suggestions = await getAsyncSuggestions(value);
    e.target.source = suggestions;
  };
}
</script>

In a LitElement

import { LitElement, html } from 'lit-element';
import '@anypoint-web-components/anypoint-autocomplete/anypoint-autocomplete.js';
import '@anypoint-web-components/anypoint-input/anypoint-input.js';

class SimpleElement extends ControlStateMixin(ButtonStateMixin(LitElement)) {
  render() {
    return html`
    <div class="parent">
      <anypoint-input id="targetInput"></anypoint-input>
      <anypoint-autocomplete
        target="targetInput"
        loader
        .source="${this.suggestions}"
        @query="${this._handleQuery}"></anypoint-autocomplete>
    </div>
    `;
  }

  async _handleQuery(e) {
    const { value } = e.detail;
    this.suggestions = await getAsyncSuggestions(value);
  }
}
window.customElements.define('simple-element', SimpleElement);

Suggestions model

{
  "value": "Required, string. The value to insert into the input field",
  "label": "Optional, string. When set this will be used as the drop down list label",
  "description": "Optional, string. When set it renders a second line for the suggestion with help message. Keep it short!"
}

Accessibility

The autocomplete element follows W3C guidelines for ARIA 1.1 Combobox with Listbox Popup. The element is enabled to support screen readers.

Because of how screen readers parses page content and associate roles, the element places suggestions as child elements of the autosuggestion element. This means that you may accidentally style list items from your master CSS file.

Because autocomplete element and text input requires a parent element with specific role, put both elements inside single parent. The element takes care of setting roles and aria attributes on all elements.

Your code

<div class="parent">
  <anypoint-input id="targetInput"></anypoint-input>
  <anypoint-autocomplete target="targetInput"></anypoint-autocomplete>
</div>

After initialization

<div
  class="parent"
  role="combobox"
  aria-label="Text input with list suggestions"
  aria-expanded="true"
  aria-owns="paperAutocompleteInput7302"
  aria-haspopup="listbox">
  <anypoint-input
    id="targetInput"
    aria-autocomplete="list"
    autocomplete="off"
    aria-haspopup="true"
    aria-controls="paperAutocompleteInput63418"></anypoint-input>
  <anypoint-autocomplete
    target="targetInput"
    id="paperAutocompleteInput7302"
    aria-controls="paperAutocompleteInput63418"
    >
    <anypoint-dropdown>
      <anypoint-listbox
        aria-label="Use arrows and enter to select list item. Escape to close the list."
        role="listbox"
        aria-activedescendant=""
        id="paperAutocompleteInput63418"></anypoint-listbox>
    </anypoint-dropdown>
  </anypoint-autocomplete>
</div>

You can set aria-label on the parent to override default message. However other attributes are always changed to comply with accessibility requirements.

Development

git clone https://github.com/anypoint-web-components/anypoint-autocomplete
cd anypoint-autocomplete
npm install

Running the demo locally

npm start

Running the tests

npm test

版本与发布记录

@anypoint-web-components/anypoint-autocomplete 共发布 19 个版本,首次发布于 2019/08/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.13latest2021/10/165 年前
0.2.122021/08/185 年前
0.2.102021/06/015 年前
0.2.92021/03/225 年前
0.2.82020/12/235 年前
0.2.72020/10/046 年前

关于 @anypoint-web-components/anypoint-autocomplete 的常见问题

@anypoint-web-components/anypoint-autocomplete 是什么?

Anypoint styled autocomplete for inputs.

如何安装 @anypoint-web-components/anypoint-autocomplete?

在项目目录运行 npm install @anypoint-web-components/anypoint-autocomplete,或使用 pnpm add / yarn add / bun add。

@anypoint-web-components/anypoint-autocomplete 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/anypoint-web-components__anypoint-autocomplete 是否存在。

@anypoint-web-components/anypoint-autocomplete 支持 ESM 还是 CommonJS?

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

@anypoint-web-components/anypoint-autocomplete 可以商用吗?

@anypoint-web-components/anypoint-autocomplete 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@anypoint-web-components/anypoint-autocomplete 的下载量有多少?

根据 npm 官方下载统计,@anypoint-web-components/anypoint-autocomplete 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,283 次,日均约 109 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anypoint-web-components/anypoint-autocomplete 还在维护吗?

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