# @anypoint-web-components/anypoint-autocomplete 中文文档

规范地址：https://npmcn.com/package/%40anypoint-web-components/anypoint-autocomplete/

版本：0\.2\.13
npm 元数据更新时间：2026-04-07T19:31:22.182Z
中文译文更新时间：2026-11-06T12:06:27.328Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40anypoint-web-components/anypoint-autocomplete
原文对照：https://npmcn.com/package/%40anypoint-web-components/anypoint-autocomplete/?lang=original
项目主页：https://github.com/anypoint-components/anypoint-autocomplete#readme
源码仓库：https://github.com/anypoint-components/anypoint-autocomplete

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：52.8 kB，9 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：4 个
- 首次发布：2019-08-15T03:52:28.280Z
- 最新版本发布：2021-10-15T21:27:39.341Z
- 近 30 天下载量：3283（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：19（近 12 个月 0 个）

## 中文简介

用于输入的任意点样式自动完成 .

## 安装

```sh
npm install @anypoint-web-components/anypoint-autocomplete
```

也可以使用：`pnpm add @anypoint-web-components/anypoint-autocomplete`、`yarn add @anypoint-web-components/anypoint-autocomplete`、`bun add @anypoint-web-components/anypoint-autocomplete`

## README

# 折旧

此组件已移至 `anypoint-web-components/awc`。 。 。 .

* * *

本构成部分以材料设计清单为基础.

任意点网络组件是允许在开源项目中构建任意点启用的UI的组件集.

该要素使输入字段的建议清单易于查阅.

[![Build Status](https://travis-ci.com/anypoint-web-components/anypoint-autocomplete.svg)](https://travis-ci.com/anypoint-web-components/anypoint-autocomplete)

[![tests](https://github.com/anypoint-web-components/anypoint-autocomplete/actions/workflows/deployment.yml/badge.svg)](https://github.com/anypoint-web-components/anypoint-autocomplete/actions/workflows/deployment.yml)

## 使用量

### 安装

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

### 在 HTML 文档中

```html
<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>
```

### 同步建议

当输入值改变自动完成调度时 `query` 活动。 您的应用程序应该处理此事件, 为用户生成建议, 并设置 `source` 属性.

向用户显示您可以设置的建议同步 `loader` 属性。 它使一个进步栏,直到源财产改变.

```html
<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>
```

### 在一个LitElement中

```js
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);
```

### 建议模式

```json
{
  "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!"
}
```

## 无障碍

自动完成元素遵循 W3C 准则 [ARIA 1. 1 有 Listbox 弹出式组合框](https://www.w3.org/TR/wai-aria-practices/examples/combobox/aria1.1pattern/listbox-combo.html)。该元素可以支持屏幕阅读器.

由于屏幕阅读器如何剖析页面内容和关联角色,元素将建议作为自定义元素的儿童元素. 这意味着您可能会不慎给您主 CSS 文件列表项样式 .

因为自動完成元素和文本输入需要具有特定角色的母元素,所以将两个元素都放入单子母体内. 该元素注意在所有元素上设定角色和aria属性.

### 你的密码

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

### 初始化后

```html
<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>
```

你可以设置 `aria-label` 以覆盖默认信件。 然而,其他属性总是被改变以符合无障碍要求.

## 发展

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

### 本地运行演示

```sh
npm start
```

### 运行测试

```sh
npm test
```

## 常见问题

### @anypoint-web-components/anypoint-autocomplete 是什么？

用于输入的任意点样式自动完成 \.

### 如何安装 @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 和提交记录判断。

