@anypoint-web-components/anypoint-listbox

中文文档
1.1.7发布于 2021/05/31Apache-2.0ESM + CJS近 30 天下载 3285

Anypoint styled list of options

原始使用文档
文档目录7 节
  1. Accessibility
  2. Usage
  3. Installation
  4. In a LitElement template
  5. Development
  6. Running the demo locally
  7. Running the tests

anypoint-listbox

Published on NPM

tests

The anypoint-listbox implements accessible list of options styled for the Anypoint platform.

<anypoint-listbox>
  <anypoint-item>Item 1</anypoint-item>
  <anypoint-item>Item 2</anypoint-item>
  <anypoint-item>Item 3</anypoint-item>
</anypoint-listbox>

Initial selection can be set by using selected property / attribute:

<anypoint-listbox selected="1">
  <anypoint-item>Item 1</anypoint-item>
  <anypoint-item>Item 2</anypoint-item>
  <anypoint-item>Item 3</anypoint-item>
</anypoint-listbox>

It allows multi selection by using multi property / attribute:

<anypoint-listbox multi>
  <anypoint-item>Item 1</anypoint-item>
  <anypoint-item>Item 2</anypoint-item>
  <anypoint-item>Item 3</anypoint-item>
</anypoint-listbox>

Children can be selected by any attribute instead of index:

<anypoint-listbox attrforselected="value" selected="2">
  <anypoint-item value="1">Item 1</anypoint-item>
  <anypoint-item value="2">Item 2</anypoint-item>
  <anypoint-item value="3">Item 3</anypoint-item>
</anypoint-listbox>

You can observe changes by listening to selected-changed event or by setting onselected property:

list.onselected = (e) => {
  console.log(e.target.selected);
};
// or
list.addEventListener('selected-changed', (e) => {
  console.log(e.target.selected);
  // also e.detail.value
});

Accessibility

<anypoint-listbox> has role="listbox" by default. A multi-select listbox will also have aria-multiselectable set. It implements key bindings to navigate through the listbox with the up and down arrow keys, esc to exit the listbox, and enter to activate a listbox item. Typing the first letter of a listbox item will also focus it.

The element also support aria-selected attribute set on children when useAriaSelected property is set. It should be used when the implementation uses different role where aria-selected is required.

<anypoint-listbox role="tablist" useAriaSelected>
  <button role="tab">Tab #1</button>
  <button role="tab">Tab #2</button>
  <button role="tab">Tab #3</button>
</anypoint-listbox>

Usage

Installation

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

In a LitElement template

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

class SimpleElement extends LitElement {
  render() {
    const { options, selected } = this;
    return html`
    <anypoint-listbox .selected="${selected}" @selected-changed="${this._selectedHandler}">
    ${options.map((item) => html`<anypoint-item>${item}</anypoint-item>`)}
    </anypoint-listbox>
    `;
  }

  _selectedHandler(e) {
    this.selected = e.target.value;
  }
}
window.customElements.define('simple-element', SimpleElement);

Development

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

Running the demo locally

npm start

Running the tests

npm test

版本与发布记录

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

版本发布日期距今
1.1.7latest2021/05/315 年前
1.1.62020/11/265 年前
1.1.52020/10/276 年前
1.1.42020/06/256 年前
1.1.32020/06/256 年前
1.1.22020/06/256 年前

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

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

Anypoint styled list of options

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

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

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

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

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

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

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

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

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

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

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

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