You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/website/src/routes/docs/headless/select/index.mdx
+18-42Lines changed: 18 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,9 +11,7 @@ import { statusByComponent } from '~/_state/component-statuses';
11
11
12
12
Reveals a list of options to choose from, often triggered by a button.
13
13
14
-
<divdata-testid="select-hero-test">
15
-
<Showcasename="hero" />
16
-
</div>
14
+
<Showcasename="hero" />
17
15
18
16
## ✨ Features
19
17
@@ -137,9 +135,7 @@ You are in full control of how the data is rendered. Map over the data, or rende
137
135
138
136
### Passing a distinct value
139
137
140
-
<divdata-testid="select-option-value-test">
141
-
<Showcasename="option-value" />
142
-
</div>
138
+
<Showcasename="option-value" />
143
139
144
140
Sometimes we want to display one thing to the user, but pass another value to the option.
145
141
@@ -149,9 +145,7 @@ By adding the `value` prop to the `<SelectOption />` component, we can pass a di
149
145
150
146
### Handling selection changes
151
147
152
-
<divdata-testid="select-change-test">
153
-
<Showcasename="change-value" />
154
-
</div>
148
+
<Showcasename="change-value" />
155
149
156
150
We can listen to changes in the selected value by using the `onChange$` prop. It provides an argument that is the new selected value.
157
151
@@ -163,9 +157,7 @@ We can select an initial uncontrolled value by passing the `value` prop to the `
163
157
164
158
### Uncontrolled / Initial value
165
159
166
-
<divdata-testid="select-uncontrolled-test">
167
-
<Showcasename="uncontrolled" />
168
-
</div>
160
+
<Showcasename="uncontrolled" />
169
161
170
162
The above example passes one of the option values "Jessie" as the initial value. As a result, the matching value is selected and focused.
171
163
@@ -175,9 +167,7 @@ The above example passes one of the option values "Jessie" as the initial value.
175
167
176
168
We can pass reactive state by using the `bind:value` prop to the `<Select />` root component.
177
169
178
-
<divdata-testid="select-controlled-test">
179
-
<Showcasename="controlled" />
180
-
</div>
170
+
<Showcasename="controlled" />
181
171
182
172
`bind:value` is a signal prop, it allows us to programmatically control the selected value of the select component.
183
173
@@ -187,19 +177,15 @@ We can pass reactive state by using the `bind:value` prop to the `<Select />` ro
187
177
188
178
To combine some of our previous knowledge, let's use the `onChange$` handler and `bind:value` prop in tandem.
189
179
190
-
<divdata-testid="select-controlled-value-test">
191
-
<Showcasename="controlled-value" />
192
-
</div>
180
+
<Showcasename="controlled-value" />
193
181
194
182
In the above example, we can programmatically change the selected value by clicking on the "Change to Abby" button.
195
183
196
184
This allows us to update the signal value and trigger the `onChange$` handler.
197
185
198
186
### Disabled options
199
187
200
-
<divdata-testid="select-disabled-test">
201
-
<Showcasename="disabled" />
202
-
</div>
188
+
<Showcasename="disabled" />
203
189
204
190
Options can be disabled by adding the `disabled` prop to the `<SelectOption />` component.
205
191
@@ -209,9 +195,7 @@ Disabled options are not selectable or focusable. They are also skipped when usi
209
195
210
196
A common use case is the addition of options dynamically. For example, an infinite scrolling list of users.
211
197
212
-
<divdata-testid="select-add-users-test">
213
-
<Showcasename="add-users" />
214
-
</div>
198
+
<Showcasename="add-users" />
215
199
216
200
Clicking the `Add Users` button adds a couple new users mapped to the list. Taking this further, we could grab more data from the server and add it to the list, or even hitting a database to get more users.
217
201
@@ -221,9 +205,7 @@ Clicking the `Add Users` button adds a couple new users mapped to the list. Taki
221
205
222
206
The select offers a typeahead feature that allows users to quickly find options by typing.
223
207
224
-
<divdata-testid="select-typeahead-test">
225
-
<Showcasename="typeahead" />
226
-
</div>
208
+
<Showcasename="typeahead" />
227
209
228
210
It reduces the need to scroll through the available options. Typeahead is particularly handy for expected data sets, such as a list of countries.
229
211
@@ -233,29 +215,23 @@ It reduces the need to scroll through the available options. Typeahead is partic
233
215
234
216
We may want to handle the open / close of the listbox. For example, we may want to show a loading indicator when the listbox is open.
235
217
236
-
<divdata-testid="select-open-change-test">
237
-
<Showcasename="open-change" />
238
-
</div>
218
+
<Showcasename="open-change" />
239
219
240
220
To do that, we can use the `onOpenChange$` prop. A parameter is passed to the handler, which is a boolean indicating whether the listbox is open or closed.
241
221
242
222
### Looping
243
223
244
224
To loop through the options, we can use the `loop` boolean prop on the `<Select />` root component.
245
225
246
-
<divdata-testid="select-loop-test">
247
-
<Showcasename="loop" />
248
-
</div>
226
+
<Showcasename="loop" />
249
227
250
228
- Pressing the down arrow key will move focus to the first option in the list.
251
229
252
230
- Pressing the up arrow key will move focus to the last option in the list.
253
231
254
232
### Grouped options
255
233
256
-
<divdata-testid="select-group-test">
257
-
<Showcasename="group" />
258
-
</div>
234
+
<Showcasename="group" />
259
235
260
236
The `<SelectGroup />` and `<SelectLabel />` components are used to group and label options.
261
237
@@ -265,9 +241,7 @@ Wrap the options in a group, add a Label, and you're good to go!
265
241
266
242
Because focus remains on the select trigger when the listbox is open, it's important to handle scrolling in the listbox.
267
243
268
-
<divdata-testid="select-scroll-test">
269
-
<Showcasename="scrollable" />
270
-
</div>
244
+
<Showcasename="scrollable" />
271
245
272
246
The native `scrollIntoView` method is used to scroll the options into view when the user highlights an option.
273
247
@@ -279,16 +253,18 @@ To customize the scroll behavior, add the `scrollOptions` prop to the `<Select /
279
253
280
254
We can provide a custom placeholder to the `<SelectValue />` component by adding the `placeholder` prop.
281
255
282
-
<divdata-testid="select-wrong-value-test">
283
-
<Showcasename="wrong-value" />
284
-
</div>
256
+
<Showcasename="wrong-value" />
285
257
286
258
When a value is not selected, the placeholder is displayed. The example above shows a placeholder fallback when the value is not selected.
287
259
288
260
The consumer misspelled the the value, and so the fallback is displayed.
289
261
290
262
> Side note: would appreciate some help on TypeScript to make misspelled option values a thing of the past. Ideally, a union of the user's passed in options.
* Describes the type of autocomplete functionality the input should provide if any. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefautocomplete).
16
17
*/
17
-
autoComplete?: string;
18
+
autoComplete?: AutoFill;
18
19
19
20
/** The text label for the select. */
20
21
label?: string;
@@ -24,40 +25,46 @@ export type AriaHiddenSelectProps = {
24
25
25
26
/** Sets the disabled state of the select and input. */
26
27
disabled?: boolean;
28
+
29
+
required?: boolean;
27
30
};
28
31
29
32
exporttypeSelectDataProps={
30
33
options: Opt[]|undefined;
31
34
};
32
35
33
-
// export function useHiddenSelect(props: AriaHiddenSelectProps) {
0 commit comments