Select
The Select component can be used when you want to offer a handful of options to the user
to choose from (usually more than what would be ideal for a RadioButton
), and the user can
only choose one option.
#Usage
So you can use the Select component this easy:
<Select options={['item-1', 'item-2']} onChange={handleOnChange} />
Of course, you can use labels, if you need to:
<Select options={[ { label: 'Item 1', value: 'item-1' }, { label: 'Item 2', value: 'item-2' }, ]} />
Also, you can use React children as well! The following snippets result is same as the above:
<Select onChange={handleOnChange}> <Select.Option value="item-1">Item 1</Select.Option> <Select.Option value="item-2">Item 2</Select.Option> </Select>
In addition, we introduced a new feature: groups, like the HTML element. Consider this array:
const groups = [
{
label: 'Series',
options: [
{
label: 'Stranger Things',
value: 'stranger-things',
},
{
label: 'Squid Game',
value: 'squid-game',
},
],
},
{
label: 'Movies',
options: [
{
label: 'Top Gun Maverick',
value: 'new-top-gun',
},
],
},
]
<Select options={groups} onChange={handleOnChange} />
Of course, you can use JSX here as well:
<Select onChange={handleOnChange}> <Select.OptionGroup label="Series"> <Select.Option value="stranger-things">Stranger Things</Select.Option> <Select.Option value="squid-game">Squid Game</Select.Option> </Select.OptionGroup> <Select.OptionGroup label="Movies"> <Select.Option value="new-top-gun">Top Gun Maverick</Select.Option> </Select.OptionGroup> </Select>
#Props
The Select
components interface extends the
HTMLSelectElement
interface. So you can use any props which documented there. Also, you can utilize all the React Synthetic events (on*
),
and props related to styling (className
, style
). These props will be applied to the Select's reference button.
import { ReactElement } from 'react' type SelectOption = string | { label: ReactElement | string; value: string } type SelectOptionGroup = { label: string; options: SelectItem[] }
#Types
SelectProps
: props of theSelect
component processed by stratusSelectInterface
:SelectProps
extended byOmit<HTMLProps<HTMLSelectElement>, 'value' | 'onChange' | 'placeholder' | 'label'>
SelectOptionProps
: props of theSelect.Option
component processed by stratusOptionGroupProps
: props of theSelect.OptionGroup
component processed by stratusSelectOptionOrGroupElements
: the type that includes theSelect.Option
,Select.OptionGroup
and arrays of them.
Read more about types here