组件 · 输入
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.
| Situation | Use |
|---|---|
| One of up to 10 values | Select |
| A few of several values | Select with multiple |
| One or a few of many values, with search | ComboboxSelect or Combobox |
| One of 2 to 5 answers, each worth a sentence | RadioGroup |
| Choosing starts an action rather than storing a value | DropdownMenu |
| Filtering a list | Select 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, withSelectItemTextandSelectItemIndicator(the check mark).SelectItemLeadingholds an icon or avatar before the text.SelectItemDescriptionis an optional second line under the text.SelectGroupandSelectGroupLabel: a group of values under a small heading.SelectSeparatoris 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
| Variant | Use 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 |
multiple | A few of several values |
Filters
A filter is a Select or a Combobox opened from a PickerTriggerButton above a list.
- Make a filter 32px high, with a 44px hit area.
- Outline a filter that is not applied with
line-seam. Fill an applied filter withinfo-softand show how many values it has. - Put a
ghostClear 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:
SelectValueshows the placeholder inforeground-hint. - Invalid:
data-invalidon the trigger gives it adanger-strongborder. - 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
titlewhile 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 (CheckboxIndicatorinSelectItemLeading), 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.
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), aSelectLabel, oraria-labelon the trigger. - In a multi-select, the box before each item is decorative; Base UI states the selection with
aria-selected. SelectItemDescriptionis the option's accessible description: the item is named bySelectItemTextalone and pointsaria-describedbyat the description, so a screen reader reads the label, then the description, each once.
API
Select
| Prop | 类型 | 默认值 |
|---|---|---|
| `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" |
| A ref to imperative actions. - `unmount`: Manually unmounts the select. Call this after any externally controlled closing animation finishes. | RefObject<SelectRootActions | null> | |
| Provides a hint to the browser for autofill. | string | |
| ReactNode | ||
| Whether the select popup is initially open. | boolean | false |
| The uncontrolled value of the select when it's initially rendered. | NonNullable<Multiple extends true ? Value[] : Value> | |
| Whether the component should ignore user interaction. | boolean | false |
| Identifies the form that owns the hidden input. Useful when the select is rendered outside the form. | string | |
| 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 Select. | string | |
| A 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> | |
| Custom 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 | |
| Data 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; }[] | |
| When 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 | |
| When 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 | |
| Determines 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. | boolean | true |
| Whether multiple items can be selected. | NonNullable<Multiple> | false |
| Identifies the field when a form is submitted. | string | |
| Event handler called when the select popup is opened or closed. | (open: boolean, eventDetails: SelectRootChangeEventDetails) => void | |
| Event handler called after any animations complete when the select popup is opened or closed. | (open: boolean) => void | |
| Event handler called when the value of the select changes. | (value: (Multiple extends true ? Value[] : Value) | (Multiple extends true ? never : null), eventDetails: SelectRootChangeEventDetails) => void | |
| Whether the select popup is currently open. | boolean | |
| Whether the user should be unable to choose a different option from the select popup. | boolean | false |
| Whether the user must choose a value before submitting a form. | boolean | false |
| The value of the select. Use when controlled. | NonNullable<Multiple extends true ? Value[] : Value> |
SelectArrow
| 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.
SelectContent
| Prop | 类型 | 默认值 |
|---|---|---|
| SelectPortalProps | ||
| How to align the popup relative to the specified side. | Align | "start" |
| Whether 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. | boolean | false |
| 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 |
| ReactNode | ||
| 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 select popup is closed. | boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | HTMLElement | null | void) | |
| 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.
SelectGroup, SelectGroupLabel, SelectIcon
| 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.
SelectItem
| Prop | 类型 | 默认值 |
|---|---|---|
| ReactNode | ||
| Whether the component should ignore user interaction. | boolean | false |
| Specifies the text label to use when the item is matched during keyboard text navigation. | string | |
| 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 |
| 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 select item. | any | null |
Also takes the attributes of its element.
SelectItemDescription
接受其元素的全部属性。
SelectItemIndicator
| Prop | 类型 | 默认值 |
|---|---|---|
| ReactNode | ||
| Whether to keep the HTML element in the DOM when the item is not selected. | boolean | |
| 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.
SelectItemLeading
接受其元素的全部属性。
SelectItemText, SelectLabel, SelectList
| 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.
SelectPopup
接受其元素的全部属性。
SelectPortal
| Prop | 类型 | 默认值 |
|---|---|---|
| A parent element to render the portal element into. | HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | |
| 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.
SelectScrollDownArrow, SelectScrollUpArrow
| Prop | 类型 | 默认值 |
|---|---|---|
| Whether to keep the HTML element in the DOM while the select popup is not scrollable. | 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.
SelectSeparator
接受其元素的全部属性。
SelectTrigger
| Prop | 类型 | 默认值 |
|---|---|---|
| ReactNode | ||
| Whether the component should ignore user interaction. | boolean | |
| 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.
SelectValue
| 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 | ((value: 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 | |
| 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.
相关组件
改动
- 0.12.0 · Breaking Changes
ComboboxSelectalways takes typing on a phone, as on a desktop: tapping the field focuses it and typing filters;createand remote search work in the field. The phone-only pick-only mode and bottom sheet from 0.10.5 are gone, with their propssheetThreshold,sheetTitleandmessages.search/done/close. A pick-only field usesSelect. 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, optionCard,SearchTrigger) are drawn at 1px inline-fieldinstead of 1.5px, on the phone too. - 0.9.4 · FixedA
SelectItemwith aSelectItemDescriptionis named by itsSelectItemTextalone 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
SelectItemwith aSelectItemLeading, theSelectItemDescriptionstarts under the label instead of under the icon. - 0.8.3 · Added
SelectItemDescription: 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
SelectItemgrows with its content and keeps 6px above and below its text. A one-line item stays 36px. - 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.