Raft 设计系统

组件 · 输入

Select

A field that opens a list of values and shows the one chosen, for picking one of up to 10 values or a few of several.

稳定
import { Select } from "@botiverse/raft-ui";
本页尚未翻译
译文完成之前,这里显示英文原文。 阅读英文版

A select is a field at the control height with an ink border, like an input: 44px on a phone, 32px on a desktop. Clicking it opens a list of values; the current value has a check mark. Use it to store a value. When choosing starts an action instead, use a menu.

When to use

The full choice between pickers is in Choosing a component.

SituationUse
One of up to 10 valuesSelect
A few of several valuesSelect with multiple
One or a few of many values, with searchComboboxSelect or Combobox
One of 2 to 5 answers, each worth a sentenceRadioGroup
Choosing starts an action rather than storing a valueDropdownMenu
Filtering a listSelect or Combobox opened from a filter; see Filters

Anatomy

  • Select: the root. Holds the value and the open state. Required.
  • SelectTrigger: the field that opens the list. Required.
  • SelectValue: the chosen value in the trigger, or the placeholder.
  • SelectIcon: the chevron at the trigger's end.
  • SelectContent: the list's popup, positioned next to the trigger. Required.
  • SelectList: the scrolling list of items.
  • SelectItem: one value, with SelectItemText and SelectItemIndicator (the check mark). SelectItemLeading holds an icon or avatar before the text. SelectItemDescription is an optional second line under the text.
  • SelectGroup and SelectGroupLabel: a group of values under a small heading. SelectSeparator is a hairline between groups.
  • SelectLabel: a label for the select when it is not in a Field.
  • SelectScrollUpArrow, SelectScrollDownArrow, SelectPortal, SelectPopup: lower-level parts for custom popups.

Variants

VariantUse for
chrome="default"A select outside a form, such as in a settings row or a toolbar
chrome="field"A select in a form, beside inputs: its value and options use the field's type, so they match the inputs
multipleA few of several values

Filters

A filter is a Select or a Combobox opened from a PickerTriggerButton above a list.

picker-trigger-button/compositions/select-filter
  • Make a filter 32px high, with a 44px hit area.
  • Outline a filter that is not applied with line-seam. Fill an applied filter with info-soft and show how many values it has.
  • Put a ghost Clear All button after the filters.
  • Open the same list as any picker: single or multiple, with search when there are many values.

States

  • No value: SelectValue shows the placeholder in foreground-hint.
  • Invalid: data-invalid on the trigger gives it a danger-strong border.
  • The trigger is a field, so it doesn't hover; a press lays the medium tier (Hover and press).
  • Disabled and the hover highlight in the list follow States.

Behavior

  • Make the list as wide as the field, at least 220px. A longer option widens it up to 24rem, never past the screen less 8px on each side, and then wraps to two lines and truncates (Forms).
  • The selected value in the trigger stays on one line and truncates; its whole text is the title while it is cut off.
  • Align a filter's list with the filter's left edge, and with its right edge when there is no room. How every popup is positioned is in Elevation.
  • Open the same list on a phone. Don't replace it with a bottom sheet: a bottom sheet is for going somewhere (Choosing a component).
  • With multiple, keep the list open when a value is chosen. Clicking outside or Esc closes it.
  • With multiple, put a box before each item (CheckboxIndicator in SelectItemLeading), ink when chosen (Checkbox). The trigger shows the chosen values, or a count (3 selected) when they don't fit.

Content

  • Group values by kind (Agents, People) with SelectGroupLabel.
  • Put secondary information (a runtime, a role, presence) at the end of the item, in foreground-hint.
  • Put a short explanation of what a value means (what a model is good at, what a level does) on a second line, in SelectItemDescription. Don't repeat the label in another form, such as its id in lowercase: a line that says nothing new only makes the list taller.
select/compositions/descriptions

An item is as tall as its content, and its padding is constant (Forms): one line is 36px, and a wrapped label or a description adds only its own height, with the one-line item's space above and below the text. SelectItemDescription sits beside SelectItemText, not inside it, so the trigger shows only the label. With a SelectItemLeading, the description starts under the label, past the icon.

  • Put the empty choice first when there is one, as with Unassigned (Tasks).

Accessibility

Select wraps Base UI Select: a button that opens a listbox, with typeahead and arrow-key navigation.

  • Name the select with a FieldLabel (Field), a SelectLabel, or aria-label on the trigger.
  • In a multi-select, the box before each item is decorative; Base UI states the selection with aria-selected.
  • SelectItemDescription is the option's accessible description: the item is named by SelectItemText alone and points aria-describedby at the description, so a screen reader reads the label, then the description, each once.

API

Select

Prop类型默认值
chrome`field` opts this Select into the field metric axis, so it reads as a form field rather than a button. Defaults to `default`, leaving every existing Select untouched.SelectChrome"default"
actionsRefA ref to imperative actions. - `unmount`: Manually unmounts the select. Call this after any externally controlled closing animation finishes.RefObject<SelectRootActions | null>
autoCompleteProvides a hint to the browser for autofill.string
childrenReactNode
defaultOpenWhether the select popup is initially open.booleanfalse
defaultValueThe uncontrolled value of the select when it's initially rendered.NonNullable<Multiple extends true ? Value[] : Value>
disabledWhether the component should ignore user interaction.booleanfalse
formIdentifies the form that owns the hidden input. Useful when the select is rendered outside the form.string
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 Select.string
inputRefA ref to access 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>
isItemEqualToValueCustom comparison logic used to determine if a select item value matches the current selected value. Useful when item values are objects without matching referentially. Defaults to `Object.is` comparison.(itemValue: Value, value: Value) => boolean
itemsData structure of the items rendered in the select popup. When specified, `<Select.Value>` renders the label of the selected item instead of the raw value.readonly Group<any>[] | Record<string, ReactNode> | readonly { label: React.ReactNode; value: any; }[]
itemToStringLabelWhen the item values are objects (`<Select.Item value={object}>`), this function converts the object value to a string representation for display in the trigger. If the shape of the object is `{ value, label }`, the label will be used automatically without needing to specify this prop.(itemValue: Value) => string
itemToStringValueWhen the item values are objects (`<Select.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.(itemValue: Value) => string
modalDetermines if the select enters a modal state when open. - `true`: user interaction is limited to the select: document page scroll is locked and pointer interactions on outside elements are disabled. - `false`: user interaction with the rest of the document is allowed.booleantrue
multipleWhether multiple items can be selected.NonNullable<Multiple>false
nameIdentifies the field when a form is submitted.string
onOpenChangeEvent handler called when the select popup is opened or closed.(open: boolean, eventDetails: SelectRootChangeEventDetails) => void
onOpenChangeCompleteEvent handler called after any animations complete when the select popup is opened or closed.(open: boolean) => void
onValueChangeEvent handler called when the value of the select changes.(value: (Multiple extends true ? Value[] : Value) | (Multiple extends true ? never : null), eventDetails: SelectRootChangeEventDetails) => void
openWhether the select popup is currently open.boolean
readOnlyWhether the user should be unable to choose a different option from the select popup.booleanfalse
requiredWhether the user must choose a value before submitting a form.booleanfalse
valueThe value of the select. Use when controlled.NonNullable<Multiple extends true ? Value[] : Value>

SelectArrow

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.

SelectContent

Prop类型默认值
portalPropsSelectPortalProps
alignHow to align the popup relative to the specified side.Align"start"
alignItemWithTriggerWhether the positioner overlaps the trigger so the selected item's text is aligned with the trigger's value text. This only applies to mouse input and is automatically disabled if there is not enough space.booleanfalse
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
childrenReactNode
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 select popup is closed.boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | HTMLElement | null | void)
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.

SelectGroup, SelectGroupLabel, SelectIcon

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.

SelectItem

Prop类型默认值
childrenReactNode
disabledWhether the component should ignore user interaction.booleanfalse
labelSpecifies the text label to use when the item is matched during keyboard text navigation.string
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
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 select item.anynull

Also takes the attributes of its element.

SelectItemDescription

A second line under an option's label: what it means, not its name again. It sits beside `SelectItemText`, not inside it, so the selected value in the trigger stays the label alone. The option grows to fit it.

接受其元素的全部属性。

SelectItemIndicator

Prop类型默认值
childrenReactNode
keepMountedWhether to keep the HTML element in the DOM when the item is not selected.boolean
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.

SelectItemLeading

接受其元素的全部属性。

SelectItemText, SelectLabel, SelectList

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.

SelectPopup

接受其元素的全部属性。

SelectPortal

Prop类型默认值
containerA parent element to render the portal element into.HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null>
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.

SelectScrollDownArrow, SelectScrollUpArrow

Prop类型默认值
keepMountedWhether to keep the HTML element in the DOM while the select popup is not scrollable.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.

SelectSeparator

接受其元素的全部属性。

SelectTrigger

Prop类型默认值
childrenReactNode
disabledWhether the component should ignore user interaction.boolean
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.

SelectValue

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 | ((value: 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
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.

改动

  • 0.12.0 · Breaking ChangesComboboxSelect always takes typing on a phone, as on a desktop: tapping the field focuses it and typing filters; create and remote search work in the field. The phone-only pick-only mode and bottom sheet from 0.10.5 are gone, with their props sheetThreshold, sheetTitle and messages.search / done / close. A pick-only field uses Select. The list still picks its side once and doesn't flip while the keyboard opens.
  • 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.9.4 · FixedA SelectItem with a SelectItemDescription is named by its SelectItemText alone and described by the description, so screen readers no longer read the two run together as the option's name. Items without a description are unchanged.
  • 0.9.0 · FixedIn a SelectItem with a SelectItemLeading, the SelectItemDescription starts under the label instead of under the icon.
  • 0.8.3 · AddedSelectItemDescription: an optional second line under an option's label, for what the value means. The trigger still shows only the label.
  • 0.8.3 · FixedA SelectItem grows with its content and keeps 6px above and below its text. A one-line item stays 36px.
  • 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.