Select
Picks one option from a list in a dropdown. Use when there are too many options to show as radios.
partially-compliant
- Overview
- Props
- Figma
Usage
Controlled
function Demo() {
const [value, setValue] = useState("");
return <Select options={options} value={value} onChange={(newValue) => setValue(newValue)} />;
}
Clearable
Set clearable prop to display the clear button in the right section:
Searchable
Set searchable prop to allow filtering options by user input:
Multiple
Set multiple prop to allow selecting multiple values.
The multiple prop is compatible with clearable and searchable:
When using multiple, the value is array of strings (so is the defaultValue and onChange's argument):
function Demo() {
const [values, setValues] = useState<string[]>(["React"]);
return <Select options={options} value={values} onChange={(newValues) => setValues(newValues)} />;
}
Option formats
The options prop accepts data of the following formats:
Array of strings:
<Select options={["React", "Angular"]} />
Array of object with value, label and optional disabled keys:
<Select
options={[
{ value: "react", label: "React" },
{ value: "ng", label: "Angular", disabled: true },
]}
/>
Disable options
When option is disabled, it cannot be selected and is ignored in keyboard navigation.
Disabled
Set disabled to disable the input. When disabled is set, user cannot interact with the input and Select will not show suggestions.
Error state
Invalid name
Condensed theme
For applications with condensed theme density, this component renders in a condensed style:
Usage in <Form>
On web, Select can be used in <Form> component and be controleld by Formik:
Usage on mobile
On mobile, you can use the nativeSheetTitle prop to set a custom title to the BottomSheet modal displaying the Select values. By default it will use the label.
Playground
murray and murray/mobile.Select props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| aria-describedby | string | Id of an element outside the input that also describes it, such as an error shared by several inputs | ||
| aria-label | string | Accessibility label, only necessary when not associated with a label | ||
| aria-required | boolean | Overrides `aria-required` set by `required`, to mark the input as mandatory without the asterisk | ||
| autoFocus | boolean | If true, focuses the input on componentDidMount.The default value is false. | ||
| canSelectAll | boolean | Adds a button below the options: selects the displayed, non-disabled options when nothing is selected, clears the selection otherwise. | ||
| clearable | boolean | Determines whether the clear button should be displayed in the right section when the component has value, false by default | ||
| cursor | Cursor | Enforce a cursor type | ||
| defaultValue | stringstring[]null | |||
| disabled | boolean | Disable input | ||
| enterKeyHint | "search""done""go""next""send" | |||
| error | ReactNode | Contents of `InputError` component, if not set, error is not rendered. | ||
| filter | ((value: string, item: Item) => boolean) | Function based on which items in dropdown are filtered | ||
| focused | boolean | Show focus styles even when the input isn't focused, e.g. while a readonly input's picker is open | ||
| helpText | ReactNode | Contents of `InputHelp` component, if not set, description is not rendered. | ||
| id | string | |||
| label | ReactNode | Contents of `InputLabel` component, if not set, label is not rendered. | ||
| locale | Locale | Language to use for Confirm Button | ||
| max | stringnumber | |||
| maxLength | number | Maximum string length that the user can enter in UTF-16 code units | ||
| min | stringnumber | |||
| minLength | number | Maximum string length that the user can enter in UTF-16 code units | ||
| multiple | boolean | Determines whether several options can be selected, `false` by default | ||
| name | string | Html input name | ||
| nativeSheetTitle | string | Title of the BottomSheet on mobile | ||
| onChange | 1 possibilities((value: string) => void) | ((value: string | null) => void) | ((value: string[]) => void) | |||
| onPress | ((event: GestureResponderEvent) => void) | |||
| options* | Item[] | Select data used to render items in dropdown | ||
| placeholder | string | |||
| prefix | ReactNode | Content section rendered on the left side of the input | ||
| prefixPointerEvents | "none""auto""box-none""box-only" | Sets pointer-events styles on the `prefix` element, `none` by default | ||
| readonly | boolean | Make input readonly | ||
| renderPlaceholder | (() => ReactNode) | To render a custom placeholder view when the field is empty | ||
| required | boolean | Adds required attribute to the input and a red asterisk on the right side of label, `false` by default | ||
| searchable | boolean | Determines whether the select should be searchable, `false` by default. | ||
| step | stringnumber | |||
| suffix | ReactNode | Content section rendered on the right side of the input | ||
| suffixPointerEvents | "none""auto""box-none""box-only" | Sets pointer-events styles on the `suffix` element, `none` by default | ||
| sx | PositionProps | Positioning properties | ||
| title | string | Html input title | ||
| type | HTMLInputTypeAttribute | HTML input type | ||
| value | stringstring[]null |
Undocumented props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| data-1p-ignore | any | |||
| data-bwignore | any | |||
| data-lpignore | any | |||
| onBlur | any | |||
| ref | any | |||
| testID | any |
Style API
Use sx for one-off adjustments. It accepts these style keys:
Each can be overridden per breakpoint with: