To add the Select
component to your app:
1 2import { Select } from '@forge/react';
Select allows users to make a single selection or multiple selections from a list of options.
Name | Type | Required | Description |
---|---|---|---|
spacing | "default" | "compact" | No | This prop affects the height of the select control. Compact is gridSize * 4, default is gridSize * 5. |
appearance | "default" | "none" | "subtle" | No | This prop affects the backgroundColor and border of the Select field. subtle makes these transparent while none removes them completely. |
autoFocus | boolean
| No | Focus the control when it is mounted. |
defaultValue | Option | Option[] | null | No | The default value of the select. |
inputValue | string | No | The value of the search input. |
inputId | string | No | The id of the search input. |
isClearable | boolean | No | Is the select value clearable. |
isLoading | boolean | No | Is the select in a state of loading (async). |
isMulti | boolean | No | Support multiple selected options. |
isSearchable | boolean | No | Whether to enable search functionality. |
menuIsOpen | boolean | No | Whether the menu is open. |
onInputChange | (newValue: string, actionMeta: { action: 'set-value' | 'input-change' | 'input-blur' | 'menu-close', prevInputValue: string }) => void | No | Handle change events on the input. |
options | (Option | Group) [] | No | Array of options that populate the select menu. |
placeholder | string | No | Placeholder for the select value. |
onChange | (newValue: Option | Option[]) => void; | No | Handle change events on the select. |
id | string | No | The ID to set on the SelectContainer component. |
isRequired | boolean | No | Indicates that the field is a required field. |
isInvalid | boolean | No | This prop indicates if the component is in an error state. |
onBlur | (e: BlurEvent) => void | No | Handle blur events on the control. |
onFocus | (e: FocusEvent) => void | No | Handle focus events on the control. |
value | Option | Option[] | null | No | The value of the select; reflected by the selected option. |
name | string | No | Name of the input (optional: without this, no input will be rendered). |
The default select appearance.
1 2const SelectAppearanceDefault= () => { return ( <Select appearance="default" options={[ { label: 'Apple', value: 'a' }, { label: 'Banana', value: 'b' }, ]} /> ); }
A select that's transparent until interaction or error.
1 2const SelectAppearanceSubtle= () => { return ( <Select appearance="subtle" options={[ { label: 'Apple', value: 'a' }, { label: 'Banana', value: 'b' }, ]} /> ); }
Always use a label component for each field and associate the label to the field properly. Use the HelperMessage
component for any optional field related message.
For required fields, always add the RequiredAsterisk
component next to the label.
1 2import { Label, RequiredAsterisk, HelperMessage, Select } from '@forge/react'; const SelectFieldLabel = () => { return ( <> <Label labelFor="select">Field label<RequiredAsterisk /></Label> <Select inputId="select" placeholder="Placeholder" /> <HelperMessage>Helper message</HelperMessage> </> ); }
A select with compact spacing.
1 2const SelectCompactSpacing = () => { return ( <Select inputId="compact-select" spacing="compact" placeholder="Compact spacing..." /> ); }
A select can be in different states such as disabled or invalid.
1 2const SelectStates = () => { return ( <Stack space="space.200"> <Select isDisabled placeholder="Disabled" /> <Select isInvalid placeholder="Choose a city" /> </Stack> ); }
Allows the user to select a single item from a dropdown list of options.
1 2const SelectSingleExample = () => ( <> <Label labelFor="single-select-example">What city do you live in?</Label> <Select inputId="single-select-example" options={[ { label: 'Adelaide', value: 'adelaide' }, { label: 'Brisbane', value: 'brisbane' }, { label: 'Canberra', value: 'canberra' }, { label: 'Darwin', value: 'darwin' }, { label: 'Hobart', value: 'hobart' }, { label: 'Melbourne', value: 'melbourne' }, { label: 'Perth', value: 'perth' }, { label: 'Sydney', value: 'sydney' }, ]} placeholder="Choose a city" /> </> );
Setting isClearable
to true lets users clear their selection using the Backspace
or Delete
key.
1 2const SelectSingleClearable = () => ( <> <Label labelFor="single-select-example-clearable">What city do you live in?</Label> <Select inputId="single-select-example-clearable" isClearable={true} options={[ { label: 'Adelaide', value: 'adelaide' }, { label: 'Brisbane', value: 'brisbane' }, { label: 'Canberra', value: 'canberra' }, { label: 'Darwin', value: 'darwin' }, { label: 'Hobart', value: 'hobart' }, { label: 'Melbourne', value: 'melbourne' }, { label: 'Perth', value: 'perth' }, { label: 'Sydney', value: 'sydney' }, ]} placeholder="Choose a city" /> </> );
Allows the user to select multiple items from a dropdown list of options.
1 2const SelectMultiExample = () => ( <> <Label labelFor="multi-select-example">What cities have you lived in?</Label> <Select inputId="multi-select-example" options={[ { label: 'Adelaide', value: 'adelaide' }, { label: 'Brisbane', value: 'brisbane' }, { label: 'Canberra', value: 'canberra' }, { label: 'Darwin', value: 'darwin' }, { label: 'Hobart', value: 'hobart' }, { label: 'Melbourne', value: 'melbourne' }, { label: 'Perth', value: 'perth' }, { label: 'Sydney', value: 'sydney' }, ]} isMulti isSearchable={false} placeholder="Choose a city" /> </> );
Related options can be grouped together in both a single and multi select.
1 2const SelectGroupedOptionsExample = () => ( <> <Label labelFor="grouped-options-example">What city do you live in?</Label> <Select inputId="grouped-options-example" options={[ { label: 'NSW', options: [ { label: 'Sydney', value: 's' }, { label: 'Newcastle', value: 'n' }, ], }, { label: 'QLD',options: [ { label: 'Brisbane', value: 'b' }, { label: 'Gold coast', value: 'g' }, ], }, { label: 'Other', options: [ { label: 'Canberra', value: 'c' }, { label: 'Williamsdale', value: 'w' }, { label: 'Darwin', value: 'd' }, { label: 'Perth', value: 'p' }, ], }, ]} placeholder="Choose a city" /> </> );
When using the Select
component, we recommend keeping the following accessibility considerations in mind:
Rate this page: