组件 · 输入
Combobox
A searchable list of values built from parts, for filters with search and for picking a few of many values as chips.
import { Combobox } from "@botiverse/raft-ui";A combobox is a list of values that people narrow down by typing. raft-ui gives it as parts: a filter that opens a list with a search row, a field that people type into, or a field of chips for several values. For a ready-made field driven by an array of options, use ComboboxSelect.
When to use
The full choice between pickers is in Choosing a component.
| Situation | Use |
|---|---|
| A filter over a list with many values, such as channels | Combobox with ComboboxTrigger and a search row |
| A few of many values (people, agents, labels) as chips in a field | Combobox with ComboboxChips |
| One or a few of many values in a form, from an array of options | ComboboxSelect |
| One of up to 10 values | Select |
Anatomy
Combobox: the root. Holds the value, the typed text and the open state. Required.- What opens the list, one of:
ComboboxTrigger: the filter button, with an icon, a label,ComboboxTriggerCountandComboboxTriggerIndicator;ComboboxControl: a field at the control height (44px on a phone, 32px on a desktop) with an ink border, holdingComboboxInput,ComboboxClearandComboboxTriggerIndicator;ComboboxChips: a field at least the control height, holding oneComboboxChipper chosen value, each with aComboboxChipRemove, thenComboboxInput.
ComboboxContent: the popup. Required.ComboboxHeader: the popup's top row, withComboboxLabeland aComboboxClear.ComboboxSeparatoris the hairline under it.ComboboxInputGroup: the search row at the top of the list, holdingComboboxInput.ComboboxList: the scrolling list ofComboboxItems, each with aComboboxItemIndicator(the check mark).ComboboxGroupandComboboxGroupLabelgroup them;ComboboxRowandComboboxCollectionlay out grids and nested collections.ComboboxEmpty: the line shown when nothing matches.ComboboxValue,ComboboxPortal,ComboboxPopup: lower-level parts.
Variants
| Variant | Use for |
|---|---|
Filter (ComboboxTrigger) | Filtering a list: the look of a filter is in Select › Filters |
Field (ComboboxControl) | Typing to find one value in a form |
Chips (ComboboxChips with multiple) | A few of many values, shown as chips in the field |
States
- Applied filter: a
ComboboxTriggerwith a value fills withinfo-softand shows the count. - No match:
ComboboxEmptysays what found nothing: No one matches "zz". - Focus: a chips field rings as a whole (States); the search row in the popup takes the inset ink outline (Elevation).
Behavior
- Filter the list as people type.
- Show
ComboboxEmptywhen nothing matches. Never leave an empty popup open. - With
multiple, keep the list open when a value is chosen. Clicking outside or Esc closes it. - Make a chosen value a chip on
fill-muted, with a remove button at its end. Typing continues after the last chip. - A chip sits 6px inside the field's outer edge and 6px from the next one, so it is 20px on a desktop and 32px on a phone and the field keeps the control height until the chips wrap. It wears
radius-chip, like every chip; its remove button wearsradius-sticker2px inside the chip, and its focus ring is drawn inside it, where the chip would clip one outside. The clear button in a field is 6px from the field's end, withradius-chip(Shape). - Put a chip's text in an element of its own (a
<span>): a long value truncates there, at the width of the field, with its whole text as the chip'stitle, and the remove button stays visible (Forms). - An item is as tall as its content: one line is 36px, and each further line adds only its own height, with the same space above and below the text (Forms).
- A long item wraps onto as many lines as it needs. The list grows to fit it up to 24rem (or the field's width, when that is wider), never past the screen less 8px on each side.
- For results that come from a server, pass
filter={null}so the list shows them as they are, and keep the typed text controlled.
Accessibility
Combobox wraps Base UI Combobox: a text input with role="combobox" that controls a listbox, with arrow-key navigation and the highlighted item announced.
- Name the input: with a
FieldLabel(Field), or witharia-labelon a search row in a popup (Search Channels). - In a chips field, each chip's remove button and the input are reachable with the keyboard. Name each remove button after its value (Remove Bugen).
- Give
ComboboxCleara label that says what it clears when it shows only an icon.
API
Combobox
| Prop | 类型 | 默认值 |
|---|---|---|
| A ref to imperative actions. - `unmount`: Manually unmounts the combobox. Call this after any externally controlled closing animation finishes. | RefObject<AriaCombobox.Actions | null> | |
| Provides a hint to the browser for autofill. | string | |
| Whether the first matching item is highlighted automatically while filtering. | boolean | false |
| ReactNode | ||
| The uncontrolled input value when initially rendered. | string | number | readonly string[] | |
| Whether the popup is initially open. | boolean | false |
| The uncontrolled selected value of the combobox when it's initially rendered. | NonNullable<Multiple extends true ? Value[] : Value> | |
| Whether the component should ignore user interaction. | boolean | false |
| Filter function used to match items vs input query. Receives the source item, which is the derived value's item when `items` is a `createItems()` collection, and the item itself otherwise. | (item: Value, query: string, itemToString?: ((item: Value) => string) | undefined) => boolean | |
| Filtered items to display in the list. When provided, the list uses these items instead of filtering the `items` prop internally. When `items` is also provided, this array must preserve its flat or grouped structure. With a `createItems()` collection, pass source items rather than derived values. Nullish entries are not supported, as in `items`. Use when you want to control filtering logic externally with the `useFilter()` hook. | readonly Value[] | readonly Group<Value>[] | |
| Identifies the form that owns the internal input. Useful when the combobox is rendered outside the form. | string | |
| Whether list items are presented in a grid layout. When enabled, arrow keys navigate across rows and columns inferred from DOM rows. | boolean | false |
| Whether moving the pointer over items should highlight them. Disabling this prop allows CSS `:hover` to be differentiated from the `:focus` (`data-highlighted`) state. | boolean | true |
| The id of the component. | string | |
| Whether the list is rendered inline without using the component's own popup. | boolean | false |
| A ref to the hidden input element. | ((instance: HTMLInputElement | null) => void | (() => void | { [UNDEFINED_VOID_ONLY]: never; }) | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES[keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES]) | RefObject<HTMLInputElement | null> | |
| The input value of the combobox. Use when controlled. | string | number | readonly string[] | |
| Custom comparison logic used to determine if a combobox item value matches the current selected value. Useful when item values are objects without matching referentially. With a `createItems()` collection, both arguments are derived values. Defaults to `Object.is` comparison. | (itemValue: Value, value: Value) => boolean | |
| The items to be displayed in the list. Can be a flat array of items, an array of groups with items, or a collection created by the `createItems()` function, which derives each item's selection value and label. Nullish entries are not supported: remove them from the data before passing it. | readonly any[] | readonly Group<any>[] | ComboboxItemCollection<Value, Value> | |
| When the item values are objects (`<Combobox.Item value={object}>`), this function converts the object value to a string representation for display in the input. If the shape of the object is `{ value, label }`, the label will be used automatically without needing to specify this prop. With a `createItems()` collection, this receives the derived value, and the collection's `getLabel` takes precedence for values it can resolve. | (itemValue: Value) => string | |
| When the item values are objects (`<Combobox.Item value={object}>`), this function converts the object value to a string representation for form submission. If the shape of the object is `{ value, label }`, the value will be used automatically without needing to specify this prop. With a `createItems()` collection, this receives the derived value. | (itemValue: Value) => string | |
| The maximum number of items to display in the list. | number | -1 |
| The locale to use for string comparison. Defaults to the user's runtime locale. | string | Intl.Locale | readonly (string | Intl.Locale)[] | |
| Whether to loop keyboard focus back to the input when the end of the list is reached while using the arrow keys. The first item can then be reached by pressing <kbd>ArrowDown</kbd> again from the input, or the last item can be reached by pressing <kbd>ArrowUp</kbd> from the input. The input is always included in the focus loop per [ARIA Authoring Practices](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/). When disabled, focus does not move when on the last element and the user presses <kbd>ArrowDown</kbd>, or when on the first element and the user presses <kbd>ArrowUp</kbd>. | boolean | true |
| Determines if the popup enters a modal state when open. - `true`: user interaction is limited to the popup: document page scroll is locked and pointer interactions on outside elements are disabled. - `false`: user interaction with the rest of the document is allowed. | boolean | false |
| Whether multiple items can be selected. | NonNullable<Multiple> | false |
| Identifies the field when a form is submitted. | string | |
| Event handler called when the input value changes. | (inputValue: string, eventDetails: BaseCombobox.Root.ChangeEventDetails) => void | |
| Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item value (or `undefined` if no item is highlighted) and event details with a `reason` property describing why the highlight changed. The `reason` can be: - `'keyboard'`: the highlight changed due to keyboard navigation. - `'pointer'`: the highlight changed due to pointer hovering. - `'none'`: the highlight changed programmatically. | (highlightedValue: Value | undefined, eventDetails: BaseCombobox.Root.HighlightEventDetails) => void | |
| Event handler called when the popup is opened or closed. | (open: boolean, eventDetails: BaseCombobox.Root.ChangeEventDetails) => void | |
| Event handler called after any animations complete when the popup is opened or closed. | (open: boolean) => void | |
| Event handler called when the selected value of the combobox changes. | (value: (Multiple extends true ? Value[] : Value) | (Multiple extends true ? never : null), eventDetails: BaseCombobox.Root.ChangeEventDetails) => void | |
| Whether the popup is currently open. Use when controlled. | boolean | |
| Whether the popup opens when clicking the input. | boolean | true |
| Whether the user should be unable to choose a different option from the popup. | boolean | false |
| Whether the user must choose a value before submitting a form. | boolean | false |
| The selected value of the combobox. Use when controlled. | NonNullable<Multiple extends true ? Value[] : Value> | |
| Whether the items are being externally virtualized. | boolean | false |
ComboboxChip
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxChipRemove
| Prop | 类型 | 默认值 |
|---|---|---|
| Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | boolean | true |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxChips
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxClear
| Prop | 类型 | 默认值 |
|---|---|---|
| Whether the component should ignore user interaction. | boolean | false |
| Whether the component should remain mounted in the DOM when not visible. | boolean | true |
| Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | boolean | true |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxCollection
| Prop | 类型 | 默认值 |
|---|---|---|
| (item: any, index: number) => React.ReactNode |
ComboboxControl
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxContent
| Prop | 类型 | 默认值 |
|---|---|---|
| AutocompletePortalProps | ||
| How to align the popup relative to the specified side. | Align | "start" |
| Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment. | number | OffsetFunction | 0 |
| An element to position the popup against. By default, the popup will be positioned against the trigger. | Element | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null) | |
| Minimum distance to maintain between the arrow and the edges of the popup. | number | 5 |
| Determines how to handle collisions when positioning the popup. | CollisionAvoidance | |
| An element or a rectangle that delimits the area that the popup is confined to. | Boundary | 'clipping-ancestors' |
| Additional space to maintain from the edge of the collision boundary. | Padding | 8 |
| Whether to disable the popup from tracking any layout shift of its positioning anchor. | boolean | false |
| Determines the element to focus when the popup is closed. | boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => void | boolean | HTMLElement | null) | false |
| Determines the element to focus when the popup is opened. | boolean | RefObject<HTMLElement | null> | ((openType: InteractionType) => void | boolean | HTMLElement | null) | false |
| Determines which CSS `position` property to use. | "fixed" | "absolute" | "fixed" |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) | |
| Which side of the anchor element to align the popup against. May automatically change to avoid collisions. | Side | "bottom" |
| Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment. | number | OffsetFunction | 0 |
| Whether to maintain the popup in the viewport after the anchor element was scrolled out of view. | boolean | false |
Also takes the attributes of its element.
ComboboxEmpty
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxGroup
| Prop | 类型 | 默认值 |
|---|---|---|
| Items to be rendered within this group. When provided, child `Collection` components will use these items. | readonly any[] | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxGroupLabel
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxHeader
接受其元素的全部属性。
ComboboxInput
| Prop | 类型 | 默认值 |
|---|---|---|
| boolean | ||
| Whether the component should ignore user interaction. | boolean | false |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxInputGroup
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxItem
| Prop | 类型 | 默认值 |
|---|---|---|
| ReactNode | ||
| Whether the component should ignore user interaction. | boolean | false |
| The index of the item in the list. Improves performance when specified by avoiding the need to calculate the index automatically from the DOM. | number | |
| Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `true` if the rendered element is a native button. | boolean | false |
| An optional click handler for the item when selected. It fires when clicking the item with the pointer, as well as when pressing `Enter` with the keyboard if the item is highlighted when the `Input` or `List` element has focus. | (event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) | |
| A unique value that identifies this item. | any | null |
Also takes the attributes of its element.
ComboboxItemIndicator
| Prop | 类型 | 默认值 |
|---|---|---|
| ReactNode | ||
| Whether to keep the HTML element in the DOM when the item is not selected. | boolean | false |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxLabel
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxList
| Prop | 类型 | 默认值 |
|---|---|---|
| ReactNode | ((item: any, index: number) => React.ReactNode) | ||
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxPopup
接受其元素的全部属性。
ComboboxPortal
| Prop | 类型 | 默认值 |
|---|---|---|
| A parent element to render the portal element into. | HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | |
| Whether to keep the portal mounted in the DOM while the popup is hidden. | boolean | false |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxRow
| Prop | 类型 | 默认值 |
|---|---|---|
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxSeparator
接受其元素的全部属性。
ComboboxTrigger
| Prop | 类型 | 默认值 |
|---|---|---|
| Whether the component should ignore user interaction. | boolean | false |
| Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | boolean | true |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ComboboxTriggerCount, ComboboxTriggerIndicator
接受其元素的全部属性。
ComboboxValue
| Prop | 类型 | 默认值 |
|---|---|---|
| Accepts a function that returns a `ReactNode` to format the selected value. Treat the value as read-only: in `multiple` mode it may be a shared frozen array when nothing is selected. | ReactNode | ((selectedValue: any) => React.ReactNode) | |
| The placeholder value to display when no value is selected. This is overridden by `children` if specified, or by a null item's label in `items`. | ReactNode |
相关组件
改动
- 0.10.0 · Breaking ChangesField frames (
Input,Textarea,InputGroup,Select,Combobox,ComboboxSelect,Composer, optionCard,SearchTrigger) are drawn at 1px inline-fieldinstead of 1.5px, on the phone too. - 0.5.37 · FixedBrutal
Comboboxkeeps its shadow when open, asSelectdoes: the control lifts 1px withshadow-raft-mdinstead of dropping the shadow. In Brutal,SelectContentandComboboxContentnow defaultsideOffsetto 8px (was 4px), so the open trigger's shadow stays clear of the popup instead of running into it. Elegant keeps 4px; an explicitsideOffsetstill wins. - 0.1.0 · AddedExtended
Lightboxwith thumbnail strip and related chrome parts; addedComboboxTriggerCountandContextMenuPopup.