1
1
import classnames from 'classnames' ;
2
- import { useCallback , useId , useMemo , useState } from 'preact/hooks' ;
2
+ import { useId , useState } from 'preact/hooks' ;
3
3
4
4
import { Link } from '../../../..' ;
5
- import { ArrowLeftIcon , ArrowRightIcon } from '../../../../components/icons' ;
6
5
import type { SelectNextProps } from '../../../../components/input' ;
7
- import { IconButton , InputGroup } from '../../../../components/input' ;
8
6
import SelectNext from '../../../../components/input/SelectNext' ;
7
+ import SelectNextInInputGroup from '../../../examples/select-next-in-input-group' ;
8
+ import SelectNextWithManyOptions from '../../../examples/select-next-with-custom-options' ;
9
9
import Library from '../../Library' ;
10
10
11
11
type ItemType = {
@@ -23,20 +23,12 @@ const defaultItems: ItemType[] = [
23
23
] ;
24
24
25
25
function SelectExample ( {
26
- disabled,
27
26
textOnly,
28
27
items = defaultItems ,
29
28
...rest
30
29
} : Pick <
31
30
SelectNextProps < ItemType > ,
32
- | 'aria-label'
33
- | 'aria-labelledby'
34
- | 'buttonClasses'
35
- | 'containerClasses'
36
- | 'listboxClasses'
37
- | 'disabled'
38
- | 'right'
39
- | 'listboxAsPopover'
31
+ 'buttonClasses' | 'containerClasses' | 'listboxClasses'
40
32
> & {
41
33
textOnly ?: boolean ;
42
34
items ?: ItemType [ ] ;
@@ -46,15 +38,12 @@ function SelectExample({
46
38
47
39
return (
48
40
< >
49
- { ! rest [ 'aria-label' ] && ! rest [ 'aria-labelledby' ] && (
50
- < label htmlFor = { buttonId } > Select a person</ label >
51
- ) }
41
+ < label htmlFor = { buttonId } > Select a person</ label >
52
42
< SelectNext
53
43
{ ...rest }
54
44
buttonId = { buttonId }
55
45
value = { value }
56
46
onChange = { setValue }
57
- disabled = { disabled }
58
47
buttonContent = {
59
48
value ? (
60
49
< >
@@ -68,8 +57,6 @@ function SelectExample({
68
57
</ div >
69
58
) }
70
59
</ >
71
- ) : disabled ? (
72
- < > This is disabled</ >
73
60
) : (
74
61
< > Select one…</ >
75
62
)
@@ -106,79 +93,6 @@ function SelectExample({
106
93
) ;
107
94
}
108
95
109
- function InputGroupSelectExample ( {
110
- buttonClasses,
111
- } : {
112
- buttonClasses ?: string ;
113
- } ) {
114
- const [ selected , setSelected ] = useState < ( typeof defaultItems ) [ number ] > ( ) ;
115
- const selectedIndex = useMemo (
116
- ( ) => ( ! selected ? - 1 : defaultItems . findIndex ( item => item === selected ) ) ,
117
- [ selected ] ,
118
- ) ;
119
- const next = useCallback ( ( ) => {
120
- const newIndex = selectedIndex + 1 ;
121
- setSelected ( defaultItems [ newIndex ] ?? selected ) ;
122
- } , [ selected , selectedIndex ] ) ;
123
- const previous = useCallback ( ( ) => {
124
- const newIndex = selectedIndex - 1 ;
125
- setSelected ( defaultItems [ newIndex ] ?? selected ) ;
126
- } , [ selected , selectedIndex ] ) ;
127
- const buttonId = useId ( ) ;
128
-
129
- return (
130
- < >
131
- < label htmlFor = { buttonId } > Select a person</ label >
132
- < InputGroup >
133
- < IconButton
134
- icon = { ArrowLeftIcon }
135
- title = "Previous student"
136
- variant = "dark"
137
- onClick = { previous }
138
- disabled = { selectedIndex <= 0 }
139
- />
140
- < SelectNext
141
- buttonId = { buttonId }
142
- value = { selected }
143
- onChange = { setSelected }
144
- buttonClasses = { buttonClasses }
145
- buttonContent = {
146
- selected ? (
147
- < div className = "flex" >
148
- < div className = "truncate" > { selected . name } </ div >
149
- < div className = "rounded px-2 ml-2 bg-grey-7 text-white" >
150
- { selected . id }
151
- </ div >
152
- </ div >
153
- ) : (
154
- < > Select one…</ >
155
- )
156
- }
157
- >
158
- { defaultItems . map ( item => (
159
- < SelectNext . Option value = { item } key = { item . id } >
160
- { item . name }
161
- < div className = "grow" />
162
- < div
163
- className = { classnames ( 'rounded px-2 ml-2 text-white bg-grey-7' ) }
164
- >
165
- { item . id }
166
- </ div >
167
- </ SelectNext . Option >
168
- ) ) }
169
- </ SelectNext >
170
- < IconButton
171
- icon = { ArrowRightIcon }
172
- title = "Next student"
173
- variant = "dark"
174
- onClick = { next }
175
- disabled = { selectedIndex >= defaultItems . length - 1 }
176
- />
177
- </ InputGroup >
178
- </ >
179
- ) ;
180
- }
181
-
182
96
export default function SelectNextPage ( ) {
183
97
return (
184
98
< Library . Page
@@ -227,51 +141,49 @@ export default function SelectNextPage() {
227
141
</ p >
228
142
229
143
< Library . Example title = "Composing and styling Selects" >
230
- < Library . Demo title = "Select with custom Options" >
231
- < div className = "w-96" >
232
- < SelectExample />
233
- </ div >
234
- </ Library . Demo >
144
+ < Library . Demo
145
+ title = "Select with custom Options"
146
+ exampleFile = "select-next-with-custom-options"
147
+ withSource
148
+ / >
235
149
236
- < Library . Demo title = "Select in InputGroup" >
237
- < div className = "w-96" >
238
- < InputGroupSelectExample />
239
- </ div >
240
- </ Library . Demo >
150
+ < Library . Demo
151
+ title = "Select in InputGroup"
152
+ exampleFile = "select-next-in-input-group"
153
+ withSource
154
+ / >
241
155
</ Library . Example >
242
156
243
157
< Library . Example title = "Select with many options" >
244
158
< Library . Demo title = "Select with many options" >
245
- < div className = "w-96 mx-auto" >
246
- < SelectExample
247
- items = { [
248
- ...defaultItems . map ( ( { id, name } ) => ( {
249
- id : `1${ id } ` ,
250
- name : `1 ${ name } ` ,
251
- } ) ) ,
252
- ...defaultItems . map ( ( { id, name } ) => ( {
253
- id : `2${ id } ` ,
254
- name : `2 ${ name } ` ,
255
- } ) ) ,
256
- ...defaultItems . map ( ( { id, name } ) => ( {
257
- id : `3${ id } ` ,
258
- name : `3 ${ name } ` ,
259
- } ) ) ,
260
- ...defaultItems . map ( ( { id, name } ) => ( {
261
- id : `4${ id } ` ,
262
- name : `4 ${ name } ` ,
263
- } ) ) ,
264
- ...defaultItems . map ( ( { id, name } ) => ( {
265
- id : `5${ id } ` ,
266
- name : `5 ${ name } ` ,
267
- } ) ) ,
268
- ...defaultItems . map ( ( { id, name } ) => ( {
269
- id : `6${ id } ` ,
270
- name : `6 ${ name } ` ,
271
- } ) ) ,
272
- ] }
273
- />
274
- </ div >
159
+ < SelectNextWithManyOptions
160
+ items = { [
161
+ ...defaultItems . map ( ( { id, name } ) => ( {
162
+ id : `1${ id } ` ,
163
+ name : `1 ${ name } ` ,
164
+ } ) ) ,
165
+ ...defaultItems . map ( ( { id, name } ) => ( {
166
+ id : `2${ id } ` ,
167
+ name : `2 ${ name } ` ,
168
+ } ) ) ,
169
+ ...defaultItems . map ( ( { id, name } ) => ( {
170
+ id : `3${ id } ` ,
171
+ name : `3 ${ name } ` ,
172
+ } ) ) ,
173
+ ...defaultItems . map ( ( { id, name } ) => ( {
174
+ id : `4${ id } ` ,
175
+ name : `4 ${ name } ` ,
176
+ } ) ) ,
177
+ ...defaultItems . map ( ( { id, name } ) => ( {
178
+ id : `5${ id } ` ,
179
+ name : `5 ${ name } ` ,
180
+ } ) ) ,
181
+ ...defaultItems . map ( ( { id, name } ) => ( {
182
+ id : `6${ id } ` ,
183
+ name : `6 ${ name } ` ,
184
+ } ) ) ,
185
+ ] }
186
+ />
275
187
</ Library . Demo >
276
188
</ Library . Example >
277
189
@@ -291,26 +203,21 @@ export default function SelectNextPage() {
291
203
linked to < code > buttonId</ code >
292
204
</ >
293
205
}
294
- >
295
- < div className = "w-96 mx-auto" >
296
- < SelectExample />
297
- </ div >
298
- </ Library . Demo >
206
+ exampleFile = "select-next-basic"
207
+ withSource
208
+ />
299
209
300
- < Library . Demo title = "Via aria-label" >
301
- < div className = "w-96 mx-auto" >
302
- < SelectExample aria-label = "Select a person with aria label" />
303
- </ div >
304
- </ Library . Demo >
210
+ < Library . Demo
211
+ title = "Via aria-label"
212
+ exampleFile = "select-next- aria- label"
213
+ withSource
214
+ / >
305
215
306
- < Library . Demo title = "Via aria-labelledby" >
307
- < div className = "w-96 mx-auto" >
308
- < p id = "select-next-meta-label" >
309
- Select a person with aria labelledby
310
- </ p >
311
- < SelectExample aria-labelledby = "select-next-meta-label" />
312
- </ div >
313
- </ Library . Demo >
216
+ < Library . Demo
217
+ title = "Via aria-labelledby"
218
+ exampleFile = "select-next-aria-labelledby"
219
+ withSource
220
+ />
314
221
</ Library . Example >
315
222
316
223
< Library . Example title = "Select with long content" >
@@ -343,7 +250,10 @@ export default function SelectNextPage() {
343
250
344
251
< Library . Demo title = "Input group" >
345
252
< div className = "mx-auto" >
346
- < InputGroupSelectExample buttonClasses = "!w-36" />
253
+ < SelectNextInInputGroup
254
+ buttonClasses = "!w-36"
255
+ wrapperClasses = ""
256
+ />
347
257
</ div >
348
258
</ Library . Demo >
349
259
</ Library . Example >
@@ -409,11 +319,11 @@ export default function SelectNextPage() {
409
319
< code > undefined</ code >
410
320
</ Library . InfoItem >
411
321
</ Library . Info >
412
- < Library . Demo title = "Disabled Select" >
413
- < div className = "w-96 mx-auto" >
414
- < SelectExample disabled />
415
- </ div >
416
- </ Library . Demo >
322
+ < Library . Demo
323
+ title = "Disabled Select"
324
+ exampleFile = "select-next- disabled"
325
+ withSource
326
+ / >
417
327
</ Library . Example >
418
328
< Library . Example title = "right" >
419
329
< Library . Info >
@@ -428,11 +338,11 @@ export default function SelectNextPage() {
428
338
< code > false</ code >
429
339
</ Library . InfoItem >
430
340
</ Library . Info >
431
- < Library . Demo title = "Right listbox" >
432
- < div className = "mx-auto" >
433
- < SelectExample right buttonClasses = "!w-36" />
434
- </ div >
435
- </ Library . Demo >
341
+ < Library . Demo
342
+ title = "Right listbox"
343
+ exampleFile = "select-next-right"
344
+ withSource
345
+ / >
436
346
</ Library . Example >
437
347
< Library . Example title = "buttonClasses" >
438
348
< Library . Info >
0 commit comments