@asphalt-react/selection

中文文档
2.19.0发布于 2026/10/29UNLICENSEDTypeScriptESM + CJS近 30 天下载 1805

Selection component is a family of components which allows users to choose option(s) from a list of items.

原始使用文档
文档目录59 节
  1. Usage
  2. items
  3. Multi-select and filtering options
  4. Controlled
  5. Group
  6. Enhancing the options
  7. i18n
  8. Keyboard interactions
  9. Data Attributes
  10. Props
  11. items
  12. onChange
  13. controlled
  14. onControlledChange
  15. multiSelect
  16. typeahead
  17. onInput
  18. onBlur
  19. size
  20. stretch
  21. defaultHighlightedIndex
  22. initialIsOpen
  23. disabled
  24. placeholder
  25. invalid
  26. displayKey
  27. flip
  28. filter
  29. translate
  30. id
  31. labelId
  32. emptyText
  33. tags
  34. getAddonStart
  35. getAddonEnd
  36. selectAll
  37. group
  38. Props
  39. children
  40. disabled
  41. highlight
  42. selected
  43. size
  44. indent
  45. Props
  46. children
  47. wrapText
  48. size
  49. checked
  50. disabled
  51. addOnStart
  52. addOnEnd
  53. checkbox
  54. indeterminate
  55. indent
  56. emphasize
  57. selectable
  58. match
  59. inactive

Selection

npm npm version

Selection is a family of accessible native select alternatives that makes it easy to choose one or more options from a list. Selection exports the "Dropdown" component.

Usage

Dropdown accepts an array of options where each option is an object containing at least 2 properties, id & key.

import { Dropdown } from "@asphalt-react/selection"

function App() {
  let items = [
    { id: "1", key: "Apple" },
    { id: "2", key: "Banana" },
  ]

  return <Dropdown items={items} />
}

Dropdown

A Dropdown shows the list of options in a popover for the users to select from. You can filter options in the list and set initial selected options and also select all the options at once. Dropdown supports custom ordering of the selected options, reseting it to the initial state and set it to be open on the initial render.

A dropdown mainly consists of 3 parts.

  • SelectionBox - holds the selected value
  • ListBox - holds the options
  • ListItem - the option itself

items

The id property is how Dropdown identifies & knows the item. Naturally, it should be unique. The key property's value will be shown as the list item, so users can easily identify the option they would like to choose.

Multi-select and filtering options

Apply the multiSelect prop when you want your user to select more than one option. To enable filtering among the options, use the typeahead prop.

Controlled

Apply controlled props to manage the items' state from the application. When you enable controlled props, ensure the following:

  • Include the selected key in the items props.
  • Use onControlledChange instead of onChange to receive information about the currently selected item and the type of action (selecting or unselecting).
  • Update items state from the application using returned value from onControlledChange event.
  • Note that initialSelected in items props will not take effect.

Group

Apply the group prop to enable grouped display and manage group-base item structures. When you enable group props, ensure the following:

  • Each item in the items prop must include a subitems array containing the child options.
import { Dropdown } from "@asphalt-react/selection"

function App() {
  let items = [
  {
      id: "fruits_1",
      key: "Fruits",
      selectable: false,
      subitems: [
        { id: "1", key: "Apple" },
        { id: "2", key: "Banana" },
      ],
    },
    {
      id: "vegetables_1",
      key: "Vegetables",
      selectable: false,
      subitems: [
        { id: "1", key: "Carrot" },
        { id: "2", key: "Broccoli" },
      ],
    },
    {
      id: "grain_1",
      key: "Grain",
      selectable: false,
      subitems: [
        { id: "1", key: "Bread" },
        { id: "2", key: "Flavor" },
      ],
    },
  ]

  return <Dropdown group items={items} />
}

Enhancing the options

To enhance the option's caption, you can add a custom element, such as an Avatar, along with the caption. For example, showing avatars/initials for a list of users. Use the getAddonStart and getAddonEnd to enable the behavior. You can disable options using the inactive. When you apply it to a group, it also disables all of its subitems.

i18n

Internationalization is the process of designing and preparing your app to be usable in different languages. SelectionBox text supports l10n and can be achieved using translate. It works only with multiselect enabled Dropdown.

Keyboard interactions

The Dropdown component is completely keyboard friendly. It is engineered in such a way that the user never needs to move their hand to the mouse, if they so desire.

  • Use Tab to focus the Dropdown.
  • Use any of ↓ , ↑ , Enter to open the listbox.
  • Use ↓ to navigate down the list.
  • Use ↑ to navigate up the list.
  • Use shift + ↓ to navigate 5 times down the list.
  • Use shift + ↑ to navigate 5 times up the list.
  • Use Enter to select an option from the list.
  • Use Esc to close the list.

Data Attributes

The Dropdown allows passing data-* attributes to the top level element of Dropdown. data-* are used by Dropdown to identify each of its instances and test its behavior. Dropdown's internal elements have a data-testid attribute which are used for testing purposes.

Props

items

Array of objects that the dropdown renders.

Example:

const items = [{ id: "1", key: "Jakarta", ... }];

If you want the items to behave as a group, provide subitems for each group item. Example:

{
  id: "add-unique-string-id",
  key: "DisplayValue",
  initialSelected: false,
  selectionOrder: 0,
  selectable: true,
  selected: true,
  inactive: true,
  subitems: [
    {
      id: "add-unique-child-string-id",
      key: "Child display value",
      initialSelected: false,
      inactive: true,
      selectionOrder: 0
    }
  ]
}

Out of the above only id is required. Typically, at the least, provide id and key. Use selected only in controlled mode to manage selection externally. Use selectable to specify whether the component allows selection of the group label. Use only in multiSelect and group. Use inactive to disable the item. When set on a group level, it also disables all of its subitems.

typerequireddefault
arrayOftrueN/A

onChange

Callback to handle the item selection event. Ivokes on user interaction or the Dropdown reset. It has the following signature

  • item - item selected by user interaction
  • options - an options bucket with the following properties
    • ref: React ref for the Dropdown
    • selectedItems: an array of all the selected items in case of multiselect enabled Dropdown
    • allSelected: a boolean value to represent if all the items are selected
 (item, { ref, selectedItems }) => {}
typerequireddefault
funcfalseN/A

controlled

Dropdown gives you the control of its internal state. Use this prop to manage the selection of items.

typerequireddefault
boolfalseN/A

onControlledChange

Callback to handle the item selection/de-selection for controlled Dropdown. Invokes on user interaction or tag deletion. It has the following signature:

(item, { ref, selection, changedItems }) => {}
  • item - The item that the user interacted with. If the user opts for the "select-all" option, the item is null.
  • options - an options bucket with the following properties:
    • ref: React ref for the Dropdown
    • selection: Boolean value to represent item selection/deselection (true/false)
    • changedItems: only present when the user opts for select all. An array of all the items that should change its state. Use the selection key to determine whether the changedItems were selected or de-selected.
typerequireddefault
funcfalseN/A

multiSelect

Enables multiple item selection

typerequireddefault
boolfalsefalse

typeahead

Enables filtering of matched items based on typed keywords. The matching algorithm used gives the end user the best possible user experience.

typerequireddefault
boolfalsefalse

onInput

Callback to handle user input in a typeahead enabled Dropdown. It receives the React synthetic event as the argument.

(event) => {}
typerequireddefault
funcfalseN/A

onBlur

Callback to handle blur event in a typeahead enabled Dropdown. It receives the React synthetic event as the argument.

(event) => {}
typerequireddefault
funcfalseN/A

size

Renders the component with the provided size. accepts "s", "m", "l" for small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Stretches the component width to match its container.

typerequireddefault
boolfalsefalse

defaultHighlightedIndex

This is the index value of an item which will be highlighted when Dropdown is initialized or reset

typerequireddefault
numberfalse0

initialIsOpen

Opens Dropdown on initialization

typerequireddefault
boolfalsefalse

disabled

Disables the Dropdown.

typerequireddefault
boolfalsefalse

placeholder

Hint text to show when no item is selected.

typerequireddefault
stringfalse"Select"

invalid

Applies invalid styles to the Dropdown.

typerequireddefault
boolfalseN/A

displayKey

Dropdown uses this prop to render the caption of the list item instead of the "key" property.

const item = [
{
 id: 1,
 label: "Bali",
},
{
 id: 2,
 label: "Jakarta"
}]

function App () {
 return <Dropdown items={items} displayKey="label" />
}

In group mode, ensure the group's key matches its subitems's key.

typerequireddefault
stringfalse"key"

flip

Dropdown flips according to viewport boundary if there isn't space to render the popover.

typerequireddefault
boolfalsetrue

filter

Dropdown shows the filtered items in a typeahead enabled Dropdown by default. Set this prop's value to false to show all the items instead.

The matched items appear at the top of the list followed by the rest of the items.

typerequireddefault
boolfalsetrue

translate

Function to show custom text in selectionBox in multiSelect enabled Dropdown.

typerequireddefault
funcfalseN/A

id

DOM id of the Dropdown.

typerequireddefault
stringfalseN/A

labelId

Id of the label used for Dropdown. Used for aria attributes.

typerequireddefault
stringfalseN/A

emptyText

Show custom text when no result is found.

typerequireddefault
stringfalse"No Results Found."

tags

Shows the selected items as tags above a multiselect dropdown.

typerequireddefault
boolfalsetrue

getAddonStart

Use this function to return an element to render as the prefix for the list item. It receives the item as the argument to uniquely identify the list item.

const itemAvatar = (item) => {
 const caption = item.key.substring(0,2)

 return <Avatar uppercase>{caption}</Avatar>
}

function App () {
 return <Dropdown getAddonStart={itemAvatar} />
}
typerequireddefault
funcfalseN/A

getAddonEnd

Use this function to return an element to render as the suffix for the list item. It receives the item as the argument to uniquely identify the list item.

const itemAvatar = (item) => {
 const caption = item.key.substring(0,2)

 return <Avatar uppercase>{caption}</Avatar>
}

function App () {
 return <Dropdown getAddonEnd={itemAvatar} />
}
typerequireddefault
funcfalseN/A

selectAll

Renders a special item in the list that allows the user to select or deselect all the options in the Dropdown. It accepts a boolean value or a node.

Acceptable value:

  • true: enables the select all feature and uses "select all" as the caption for the item.
  • false: skips the select all feature.
  • node: enables the select all feature and uses the element as the caption for the item.
import {Text} from "@asphalt-react/typography"

<Dropdown selectAll={<Text>Pilih Semua</Text>} />
typerequireddefault
unionfalsefalse

group

Enables group items selection.

typerequireddefault
boolfalsefalse

ListItem

Props

children

Node which ListItem wraps

typerequireddefault
nodetrueN/A

disabled

Disabled state of item

typerequireddefault
boolfalsefalse

highlight

Highlighted state of item

typerequireddefault
boolfalsefalse

selected

Selected state of item

typerequireddefault
boolfalsefalse

size

Size of item. Accepts one of "s", "m" & "l"

typerequireddefault
enumfalse"m"

indent

Indent state of item

typerequireddefault
boolfalsefalse

ListItemContent

Props

children

typerequireddefault
nodefalseN/A

wrapText

typerequireddefault
boolfalsetrue

size

typerequireddefault
enumfalse"m"

checked

typerequireddefault
boolfalsefalse

disabled

typerequireddefault
boolfalsefalse

addOnStart

typerequireddefault
nodefalseN/A

addOnEnd

typerequireddefault
nodefalseN/A

checkbox

typerequireddefault
boolfalsefalse

indeterminate

typerequireddefault
boolfalsefalse

indent

typerequireddefault
boolfalseN/A

emphasize

typerequireddefault
boolfalsefalse

selectable

typerequireddefault
boolfalseN/A

match

typerequireddefault
stringfalse""

inactive

typerequireddefault
boolfalsefalse

版本与发布记录

@asphalt-react/selection 共发布 38 个版本,首次发布于 2024/06/21;过去 12 个月发布了 14 个版本。

版本发布日期距今
2.19.0latest2026/10/2910 天前
2.18.02026/10/071 个月前
2.17.02026/08/122 个月前
2.16.02026/07/283 个月前
2.15.32026/07/074 个月前
2.15.22026/05/255 个月前

其他发布通道:next → 2.0.0-alpha.40。

关于 @asphalt-react/selection 的常见问题

@asphalt-react/selection 是什么?

Selection component is a family of components which allows users to choose option(s) from a list of items.

如何安装 @asphalt-react/selection?

在项目目录运行 npm install @asphalt-react/selection,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>= 16.8.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@asphalt-react/selection 支持 TypeScript 吗?

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

@asphalt-react/selection 支持 ESM 还是 CommonJS?

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

@asphalt-react/selection 可以商用吗?

@asphalt-react/selection 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@asphalt-react/selection 的下载量有多少?

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

@asphalt-react/selection 还在维护吗?

最新版本 2.19.0 发布于 2026/10/29(10 天前);至今共发布 38 个版本,其中过去 12 个月发布了 14 个;首次发布于 2024/06/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。