Raft 设计系统

组件 · 输入

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.

combobox/compositions/tasks-channel-filter

When to use

The full choice between pickers is in Choosing a component.

SituationUse
A filter over a list with many values, such as channelsCombobox with ComboboxTrigger and a search row
A few of many values (people, agents, labels) as chips in a fieldCombobox with ComboboxChips
One or a few of many values in a form, from an array of optionsComboboxSelect
One of up to 10 valuesSelect

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, ComboboxTriggerCount and ComboboxTriggerIndicator;
    • ComboboxControl: a field at the control height (44px on a phone, 32px on a desktop) with an ink border, holding ComboboxInput, ComboboxClear and ComboboxTriggerIndicator;
    • ComboboxChips: a field at least the control height, holding one ComboboxChip per chosen value, each with a ComboboxChipRemove, then ComboboxInput.
  • ComboboxContent: the popup. Required.
  • ComboboxHeader: the popup's top row, with ComboboxLabel and a ComboboxClear. ComboboxSeparator is the hairline under it.
  • ComboboxInputGroup: the search row at the top of the list, holding ComboboxInput.
  • ComboboxList: the scrolling list of ComboboxItems, each with a ComboboxItemIndicator (the check mark). ComboboxGroup and ComboboxGroupLabel group them; ComboboxRow and ComboboxCollection lay out grids and nested collections.
  • ComboboxEmpty: the line shown when nothing matches.
  • ComboboxValue, ComboboxPortal, ComboboxPopup: lower-level parts.

Variants

VariantUse 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
picker-trigger-button/compositions/combobox-filter

States

  • Applied filter: a ComboboxTrigger with a value fills with info-soft and shows the count.
  • No match: ComboboxEmpty says 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 ComboboxEmpty when 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 wears radius-sticker 2px 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, with radius-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's title, 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 with aria-label on 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 ComboboxClear a label that says what it clears when it shows only an icon.

API

Combobox

Prop类型默认值
actionsRefA ref to imperative actions. - `unmount`: Manually unmounts the combobox. Call this after any externally controlled closing animation finishes.RefObject<AriaCombobox.Actions | null>
autoCompleteProvides a hint to the browser for autofill.string
autoHighlightWhether the first matching item is highlighted automatically while filtering.booleanfalse
childrenReactNode
defaultInputValueThe uncontrolled input value when initially rendered.string | number | readonly string[]
defaultOpenWhether the popup is initially open.booleanfalse
defaultValueThe uncontrolled selected value of the combobox when it's initially rendered.NonNullable<Multiple extends true ? Value[] : Value>
disabledWhether the component should ignore user interaction.booleanfalse
filterFilter 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
filteredItemsFiltered 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>[]
formIdentifies the form that owns the internal input. Useful when the combobox is rendered outside the form.string
gridWhether list items are presented in a grid layout. When enabled, arrow keys navigate across rows and columns inferred from DOM rows.booleanfalse
highlightItemOnHoverWhether moving the pointer over items should highlight them. Disabling this prop allows CSS `:hover` to be differentiated from the `:focus` (`data-highlighted`) state.booleantrue
idThe id of the component.string
inlineWhether the list is rendered inline without using the component's own popup.booleanfalse
inputRefA 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>
inputValueThe input value of the combobox. Use when controlled.string | number | readonly string[]
isItemEqualToValueCustom 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
itemsThe 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>
itemToStringLabelWhen 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
itemToStringValueWhen 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
limitThe maximum number of items to display in the list.number-1
localeThe locale to use for string comparison. Defaults to the user's runtime locale.string | Intl.Locale | readonly (string | Intl.Locale)[]
loopFocusWhether 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>.booleantrue
modalDetermines 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.booleanfalse
multipleWhether multiple items can be selected.NonNullable<Multiple>false
nameIdentifies the field when a form is submitted.string
onInputValueChangeEvent handler called when the input value changes.(inputValue: string, eventDetails: BaseCombobox.Root.ChangeEventDetails) => void
onItemHighlightedCallback 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
onOpenChangeEvent handler called when the popup is opened or closed.(open: boolean, eventDetails: BaseCombobox.Root.ChangeEventDetails) => void
onOpenChangeCompleteEvent handler called after any animations complete when the popup is opened or closed.(open: boolean) => void
onValueChangeEvent 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
openWhether the popup is currently open. Use when controlled.boolean
openOnInputClickWhether the popup opens when clicking the input.booleantrue
readOnlyWhether the user should be unable to choose a different option from the popup.booleanfalse
requiredWhether the user must choose a value before submitting a form.booleanfalse
valueThe selected value of the combobox. Use when controlled.NonNullable<Multiple extends true ? Value[] : Value>
virtualizedWhether the items are being externally virtualized.booleanfalse

ComboboxChip

Prop类型默认值
renderAllows 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类型默认值
nativeButtonWhether 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>`).booleantrue
renderAllows 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类型默认值
renderAllows 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类型默认值
disabledWhether the component should ignore user interaction.booleanfalse
keepMountedWhether the component should remain mounted in the DOM when not visible.booleantrue
nativeButtonWhether 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>`).booleantrue
renderAllows 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类型默认值
children (required)(item: any, index: number) => React.ReactNode

ComboboxControl

Prop类型默认值
renderAllows 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类型默认值
portalPropsAutocompletePortalProps
alignHow to align the popup relative to the specified side.Align"start"
alignOffsetAdditional 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 | OffsetFunction0
anchorAn element to position the popup against. By default, the popup will be positioned against the trigger.Element | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null)
arrowPaddingMinimum distance to maintain between the arrow and the edges of the popup.number5
collisionAvoidanceDetermines how to handle collisions when positioning the popup.CollisionAvoidance
collisionBoundaryAn element or a rectangle that delimits the area that the popup is confined to.Boundary'clipping-ancestors'
collisionPaddingAdditional space to maintain from the edge of the collision boundary.Padding8
disableAnchorTrackingWhether to disable the popup from tracking any layout shift of its positioning anchor.booleanfalse
finalFocusDetermines the element to focus when the popup is closed.boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => void | boolean | HTMLElement | null)false
initialFocusDetermines the element to focus when the popup is opened.boolean | RefObject<HTMLElement | null> | ((openType: InteractionType) => void | boolean | HTMLElement | null)false
positionMethodDetermines which CSS `position` property to use."fixed" | "absolute""fixed"
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)
sideWhich side of the anchor element to align the popup against. May automatically change to avoid collisions.Side"bottom"
sideOffsetDistance 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 | OffsetFunction0
stickyWhether to maintain the popup in the viewport after the anchor element was scrolled out of view.booleanfalse

Also takes the attributes of its element.

ComboboxEmpty

Prop类型默认值
renderAllows 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类型默认值
itemsItems to be rendered within this group. When provided, child `Collection` components will use these items.readonly any[]
renderAllows 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类型默认值
renderAllows 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类型默认值
suppressPasswordManagerboolean
disabledWhether the component should ignore user interaction.booleanfalse
renderAllows 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类型默认值
renderAllows 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类型默认值
childrenReactNode
disabledWhether the component should ignore user interaction.booleanfalse
indexThe index of the item in the list. Improves performance when specified by avoiding the need to calculate the index automatically from the DOM.number
nativeButtonWhether 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.booleanfalse
onClickAn 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
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)
valueA unique value that identifies this item.anynull

Also takes the attributes of its element.

ComboboxItemIndicator

Prop类型默认值
childrenReactNode
keepMountedWhether to keep the HTML element in the DOM when the item is not selected.booleanfalse
renderAllows 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类型默认值
renderAllows 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类型默认值
childrenReactNode | ((item: any, index: number) => React.ReactNode)
renderAllows 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类型默认值
containerA parent element to render the portal element into.HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null>
keepMountedWhether to keep the portal mounted in the DOM while the popup is hidden.booleanfalse
renderAllows 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类型默认值
renderAllows 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类型默认值
disabledWhether the component should ignore user interaction.booleanfalse
nativeButtonWhether 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>`).booleantrue
renderAllows 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类型默认值
childrenAccepts 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)
placeholderThe 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, option Card, SearchTrigger) are drawn at 1px in line-field instead of 1.5px, on the phone too.
  • 0.5.37 · FixedBrutal Combobox keeps its shadow when open, as Select does: the control lifts 1px with shadow-raft-md instead of dropping the shadow. In Brutal, SelectContent and ComboboxContent now default sideOffset to 8px (was 4px), so the open trigger's shadow stays clear of the popup instead of running into it. Elegant keeps 4px; an explicit sideOffset still wins.
  • 0.1.0 · AddedExtended Lightbox with thumbnail strip and related chrome parts; added ComboboxTriggerCount and ContextMenuPopup.