UNPKG

3.74 kBMarkdownView Raw
1---
2title: Select
3storybookPath: forms-select--default
4isExperimentalPackage: false
5---
6
7Allows the user to make a single selection from a list of values — usually in a
8form. If only a few options are provided, consider using a `RadioButton`
9instead.
10
11## Usage
12
13### Field
14
15Each select input must be accompanied by a Field with a label. Effective form
16labeling helps inform users which selection to make.
17
18## Examples
19
20### Controlled
21
22A `<Select>` can be both controlled and uncontrolled. To control a `<Select>`
23provide a `value`, as well as an `onChange` function to set the new value when
24the select is updated.
25
26```jsx live
27const [selectedOption, setSelectedOption] = React.useState('');
28
29const options = [
30 { label: 'NSW', value: 'nsw' },
31 { label: 'VIC', value: 'vic' },
32 { label: 'QLD', value: 'qld' },
33 { label: 'SA', value: 'sa' },
34 { label: 'WA', value: 'wa' },
35 { label: 'TAS', value: 'tas' },
36 { label: 'NT', value: 'nt' },
37 { label: 'ACT', value: 'act' },
38];
39
40return (
41 <Stack gap="large">
42 <Field label="State">
43 <Select
44 placeholder="Choose a state..."
45 value={selectedOption}
46 onChange={event => setSelectedOption(event.target.value)}
47 options={options}
48 required
49 />
50 </Field>
51 {selectedOption && (
52 <Text>
53 You have selected{' '}
54 {options.find(option => option.value === selectedOption).label}
55 </Text>
56 )}
57 </Stack>
58);
59```
60
61### Uncontrolled
62
63The `<Select>`, by default, is an uncontrolled component, meaning that the form
64data is controlled directly by the DOM itself. To access the value, instead of
65writing an `onChange` handler, you would use a `ref` to get form values from the
66DOM.
67
68```jsx live
69<Field label="Breaking Bad Characters">
70 <Select
71 options={[
72 { label: 'Walter White', value: 'walter-white' },
73 { label: 'Jesse Pinkman', value: 'jesse-pinkman' },
74 { label: 'Saul Goodman', value: 'saul-goodman' },
75 { label: 'Gus Fring', value: 'gus-fring' },
76 { label: 'Hank Schrader', value: 'hank-schrader' },
77 { label: 'Mike Ehrmantraut', value: 'mike-ehrmantraut' },
78 ]}
79 />
80</Field>
81```
82
83### Groups
84
85Related options can be grouped by passing in an array of objects with a label
86and option key — where each option is an array of objects with label, value and
87(optionally) disabled keys. Internally this uses the
88[`<optgroup>` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup).
89
90```jsx live
91const [selectedOption, setSelectedOption] = React.useState('');
92
93return (
94 <Field label="Select">
95 <Select
96 placeholder="TV Characters"
97 options={[
98 {
99 label: 'Mad Men',
100 options: [
101 { label: 'Don Draper', value: 'don-draper' },
102 { label: 'Peggy Olson', value: 'peggy-olson' },
103 { label: 'Joan Harris', value: 'joan-harris' },
104 { label: 'Roger Sterling', value: 'roger-sterling' },
105 { label: 'Pete Campbell', value: 'pete-campbell' },
106 ],
107 },
108 {
109 label: 'Breaking Bad',
110 options: [
111 { label: 'Walter White', value: 'walter-white' },
112 { label: 'Jesse Pinkman', value: 'jesse-pinkman' },
113 { label: 'Saul Goodman', value: 'saul-goodman' },
114 { label: 'Gus Fring', value: 'gus-fring' },
115 { label: 'Hank Schrader', value: 'hank-schrader' },
116 { label: 'Mike Ehrmantraut', value: 'mike-ehrmantraut' },
117 ],
118 },
119 ]}
120 />
121 </Field>
122);
123```
124
125## Props
126
127<PropsTable displayName="Select" />
128
129[data-attribute-map]:
130 https://github.com/brighte-labs/spark-web/blob/e7f6f4285b4cfd876312cc89fbdd094039aa239a/packages/utils/src/internal/buildDataAttributes.ts#L1