@@ -13,17 +13,74 @@ const defaultItems = [
13
13
{ id : '4' , name : 'Cecelia Davenport' } ,
14
14
{ id : '5' , name : 'Doris Evanescence' } ,
15
15
] ;
16
+ const longListOfItems = [
17
+ ...defaultItems . map ( ( { id, name } ) => ( {
18
+ id : `1${ id } ` ,
19
+ name : `1 ${ name } ` ,
20
+ } ) ) ,
21
+ ...defaultItems . map ( ( { id, name } ) => ( {
22
+ id : `2${ id } ` ,
23
+ name : `2 ${ name } ` ,
24
+ } ) ) ,
25
+ ...defaultItems . map ( ( { id, name } ) => ( {
26
+ id : `3${ id } ` ,
27
+ name : `3 ${ name } ` ,
28
+ } ) ) ,
29
+ ...defaultItems . map ( ( { id, name } ) => ( {
30
+ id : `4${ id } ` ,
31
+ name : `4 ${ name } ` ,
32
+ } ) ) ,
33
+ ...defaultItems . map ( ( { id, name } ) => ( {
34
+ id : `5${ id } ` ,
35
+ name : `5 ${ name } ` ,
36
+ } ) ) ,
37
+ ...defaultItems . map ( ( { id, name } ) => ( {
38
+ id : `6${ id } ` ,
39
+ name : `6 ${ name } ` ,
40
+ } ) ) ,
41
+ ] ;
42
+
43
+ function SelectOptionExample ( {
44
+ item,
45
+ textOnly,
46
+ } : {
47
+ item : ( typeof defaultItems ) [ number ] ;
48
+ textOnly : boolean ;
49
+ } ) {
50
+ return (
51
+ < SelectNext . Option value = { item } >
52
+ { ( ) =>
53
+ textOnly ? (
54
+ < > { item . name } </ >
55
+ ) : (
56
+ < >
57
+ { item . name }
58
+ < div className = "grow" />
59
+ < div className = "rounded px-2 bg-grey-7 text-white" > { item . id } </ div >
60
+ </ >
61
+ )
62
+ }
63
+ </ SelectNext . Option >
64
+ ) ;
65
+ }
16
66
17
67
function SelectExample ( {
18
68
disabled,
19
- textOnly,
69
+ textOnly = false ,
70
+ filterable,
20
71
items = defaultItems ,
21
72
} : {
22
73
disabled ?: boolean ;
23
74
textOnly ?: boolean ;
75
+ filterable ?: boolean ;
24
76
items ?: typeof defaultItems ;
25
77
} ) {
26
78
const [ value , setValue ] = useState < ( typeof items ) [ number ] > ( ) ;
79
+ const onFilter = useCallback (
80
+ ( query : string , value : ( typeof items ) [ number ] ) =>
81
+ value . name . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ,
82
+ [ ] ,
83
+ ) ;
27
84
28
85
return (
29
86
< SelectNext
@@ -47,23 +104,43 @@ function SelectExample({
47
104
}
48
105
disabled = { disabled }
49
106
>
50
- { items . map ( item => (
51
- < SelectNext . Option value = { item } key = { item . id } >
52
- { ( ) =>
53
- textOnly ? (
54
- < > { item . name } </ >
55
- ) : (
56
- < >
57
- { item . name }
58
- < div className = "grow" />
59
- < div className = "rounded px-2 bg-grey-7 text-white" >
60
- { item . id }
61
- </ div >
62
- </ >
63
- )
64
- }
65
- </ SelectNext . Option >
66
- ) ) }
107
+ { ! filterable &&
108
+ items . map ( item => (
109
+ < SelectOptionExample item = { item } textOnly = { textOnly } key = { item . id } />
110
+ ) ) }
111
+ { filterable && (
112
+ < >
113
+ { /* Render a couple options before filterable to demonstrate stickiness */ }
114
+ < SelectOptionExample item = { items [ 0 ] } textOnly = { textOnly } />
115
+ < SelectOptionExample item = { items [ 1 ] } textOnly = { textOnly } />
116
+
117
+ { /* Render two filterable blocks to demonstrate sections filtering */ }
118
+ < SelectNext . Filterable onFilter = { onFilter } >
119
+ { items
120
+ . slice ( 2 )
121
+ . splice ( 0 , items . length / 2 - 2 )
122
+ . map ( item => (
123
+ < SelectOptionExample
124
+ item = { item }
125
+ textOnly = { textOnly }
126
+ key = { item . id }
127
+ />
128
+ ) ) }
129
+ </ SelectNext . Filterable >
130
+ < SelectNext . Filterable onFilter = { onFilter } >
131
+ { items
132
+ . slice ( 2 )
133
+ . splice ( items . length / 2 - 2 + 1 )
134
+ . map ( item => (
135
+ < SelectOptionExample
136
+ item = { item }
137
+ textOnly = { textOnly }
138
+ key = { item . id }
139
+ />
140
+ ) ) }
141
+ </ SelectNext . Filterable >
142
+ </ >
143
+ ) }
67
144
</ SelectNext >
68
145
) ;
69
146
}
@@ -154,7 +231,7 @@ export default function SelectNextPage() {
154
231
155
232
< Library . Example >
156
233
< Library . Demo title = "Basic Select" >
157
- < div className = "w-[350px] mx-auto" >
234
+ < div className = "w-96 mx-auto" >
158
235
< SelectExample textOnly />
159
236
</ div >
160
237
</ Library . Demo >
@@ -183,42 +260,20 @@ export default function SelectNextPage() {
183
260
184
261
< Library . Example title = "Select with many options" >
185
262
< Library . Demo title = "Select with many options" >
186
- < div className = "w-[350px] mx-auto" >
187
- < SelectExample
188
- items = { [
189
- ...defaultItems . map ( ( { id, name } ) => ( {
190
- id : `1${ id } ` ,
191
- name : `1 ${ name } ` ,
192
- } ) ) ,
193
- ...defaultItems . map ( ( { id, name } ) => ( {
194
- id : `2${ id } ` ,
195
- name : `2 ${ name } ` ,
196
- } ) ) ,
197
- ...defaultItems . map ( ( { id, name } ) => ( {
198
- id : `3${ id } ` ,
199
- name : `3 ${ name } ` ,
200
- } ) ) ,
201
- ...defaultItems . map ( ( { id, name } ) => ( {
202
- id : `4${ id } ` ,
203
- name : `4 ${ name } ` ,
204
- } ) ) ,
205
- ...defaultItems . map ( ( { id, name } ) => ( {
206
- id : `5${ id } ` ,
207
- name : `5 ${ name } ` ,
208
- } ) ) ,
209
- ...defaultItems . map ( ( { id, name } ) => ( {
210
- id : `6${ id } ` ,
211
- name : `6 ${ name } ` ,
212
- } ) ) ,
213
- ] }
214
- />
263
+ < div className = "w-96 mx-auto" >
264
+ < SelectExample items = { longListOfItems } />
265
+ </ div >
266
+ </ Library . Demo >
267
+ < Library . Demo title = "Select with filtering" >
268
+ < div className = "w-96 mx-auto" >
269
+ < SelectExample items = { longListOfItems } filterable />
215
270
</ div >
216
271
</ Library . Demo >
217
272
</ Library . Example >
218
273
219
274
< Library . Example title = "Disabled Select" >
220
275
< Library . Demo title = "Disabled Select" >
221
- < div className = "w-[350px] mx-auto" >
276
+ < div className = "w-96 mx-auto" >
222
277
< SelectExample disabled />
223
278
</ div >
224
279
</ Library . Demo >
0 commit comments