Select
This component allows the user to select from suggested options.
Basic Usage
import { Select } from '@krakentech/coral';
<Select
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
/>Properties
value
const [selectedValue, setSelectedValue] = useState<SelectOption | null>({
value: 20,
label: 'Twenty',
});
const onChange = (value: SelectOption | null | undefined) => {
setSelectedValue(value ?? null);
};
<Select
value={selectedValue}
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' }
]}
onChange={onChange}
/>
<Button onClick={() => setSelectedValue({
value: 30,
label: 'Thirty',
})}>
Change value
</Button>defaultOpen
<Select
defaultOpen
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
/>defaultValue
<Select
defaultValue={{ value: 30, label: 'Thirty' }}
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
/>disabled
<Select
disabled
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
/>error / errorMessage
<Select
error
errorMessage="Please select a value"
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
/>Please select a value
label
<Select
label="Select a number"
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
/>optional
<Select
optional
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
/>Theme
<Select
theme="secondary500"
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
/>ReactNode labels
It is possible to pass in React Nodes as the label like below, to add icons for example. Wrapping these in a Stack ensures they are aligned correctly. We are unable to change the typing on this component due to the package we use for our Select but you can use a // @ts-expect-error comment in these instances to get rid of the type error.
<Select
values={[
{
value: 'electricity',
label: (
<>
<Stack direction="horizontal" alignItems="center">
<IconElectricity /> Electricity
</Stack>
</>
),
},
{
value: 'gas',
label: (
<>
<Stack direction="horizontal" alignItems="center">
<IconGas /> Gas
</Stack>
</>
),
},
]}
/>Event Handling
<Select
onChange={(selectValue) => {}}
onBlur={() => {}}
onOpen={() => {}}
onClose={() => {}}
...
/>Responsive
<Select
values={[
{ value: 10, label: 'Ten' },
{ value: 20, label: 'Twenty' },
{ value: 30, label: 'Thirty' },
]}
theme={{ base: "base100", lg: "secondary500" }}
/>Full API
| Name | Type | Default |
|---|---|---|
defaultOpen | booleanIf | false |
defaultValue | SelectOption | nullThe default value to be selected when the component is first rendered. | null |
disabled | booleanIf | false |
error | booleanIf | false |
errorMessage | stringIf set, the | Please select an option |
label | stringThe accessible label for the select. | Label |
id | stringThe HTML ID passed to both the label, and the select. | |
name | stringThe field name used on the POST request when the form is submitted. | |
onBlur | FocusEventHandler<HTMLButtonElement>Callback fired when the select loses user focus. | |
onChange | (value?: SelectOption | null | undefined) => voidCallback fired each time the user selects an option.
| |
onClose | () => voidCallback fired when the select is closed. | |
onOpen | () => voidCallback fired when the select is opened. | |
optional | booleanAdd an optional flag to the label for this field (controlled by https://coral-oe.vercel.app/?path=/docs/guides-configuration—docs#overrides-optional-indicator optionalIndicator). This should be set to true if you have set this field to be optional in your validation schema. | false |
value | SelectOption | null
| |
values | SelectOption[]The options to be displayed in the select dropdown. | [] |
theme | ResponsiveVariant<"base100" | "secondary500">The colourway applied to this component. | base100 |
attributes | AttributesThis prop can be used to pass HTML attributes directly to the component.
We currently allow passing Example usage: |