forked from umbraco/Umbraco.UI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuui-combobox.story.ts
449 lines (413 loc) · 12.6 KB
/
uui-combobox.story.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import '@umbraco-ui/uui-scroll-container/lib';
import '@umbraco-ui/uui-icon/lib';
import '@umbraco-ui/uui-input/lib';
import '@umbraco-ui/uui-button/lib';
import '@umbraco-ui/uui-popover/lib';
import '.';
import './uui-combobox-async-example';
import './uui-combobox-async-options-example';
import { StoryFn } from '@storybook/web-components';
import { html } from 'lit';
import { useArgs } from '@storybook/preview-api';
import { repeat } from 'lit/directives/repeat.js';
import RegionsAndCountries from '../../../storyhelpers/RegionsAndCountries';
import readme from '../README.md?raw';
export default {
id: 'uui-combobox',
title: 'Inputs/Combobox',
component: 'uui-combobox',
args: {
search: '',
disabled: false,
readonly: false,
},
parameters: {
readme: {
markdown: readme,
},
docs: {
source: {
code: `<uui-combobox style="width: 250px"></uui-combobox>`,
},
},
},
};
export const AsyncOptions: StoryFn = () => html`
<uui-combobox-async-options-example></uui-combobox-async-options-example>
`;
export const AsyncData: StoryFn = () =>
html`<uui-combobox-async-example></uui-combobox-async-example>`;
const fruits = [
'apple',
'orange',
'lemon',
'melon',
'banana',
'pear',
'mango',
'plum',
'raspberry',
'kiwi',
'avocado',
'coconut',
'grape',
];
const avatars = [
{
id: 'SM',
name: 'Superman',
title: 'A pretty strong guy',
},
{
id: 'RD',
name: 'R2-D2',
title: 'Bip Bub',
},
{
id: 'LS',
name: 'Luke Skywalker',
title: 'Guy with a funky sword',
},
{
id: 'BM',
name: 'Batman',
title: "I'M BATMAN!",
},
];
const basicFilter = (options: string[], search: string) =>
options.filter(option => option.toLowerCase().includes(search.toLowerCase()));
const renderAvatar = (option: any) =>
html` <uui-combobox-list-option
.displayValue=${option.name}
style="display: flex; gap: 9px; align-items: center; padding: var(--uui-size-3)"
.value=${option.id}>
<uui-avatar
style="background-color: #c8d1dd"
.name=${option.name}></uui-avatar>
<div style="display: flex; flex-direction: column">
<b>${option.name}</b>
<div style="font-size: 0.8rem">${option.title}</div>
</div>
</uui-combobox-list-option>`;
const Template: StoryFn = props => {
const [, updateSearch] = useArgs();
const [, updateSelected] = useArgs();
const {
options,
selected,
search,
filter,
displayValueMod,
valueMod,
renderMod,
disabled,
value,
} = props;
const handleSearch = (e: any) => {
props.search = e.target.search;
updateSearch(props);
};
const handleSelect = (e: any) => {
props.selected = e.target.value;
updateSelected(props);
};
return html`<uui-combobox
@change=${handleSelect}
@search=${handleSearch}
style="width: 250px"
.disabled=${disabled}
.value=${value}>
<uui-combobox-list>
${repeat(filter(options, search), (option: any, index: number) =>
renderMod
? renderMod(option, index)
: html`<uui-combobox-list-option
.value=${valueMod ? valueMod(option) : option}
.displayValue=${displayValueMod
? displayValueMod(option)
: option}
style="padding: 8px">
${option}
</uui-combobox-list-option>`
)}
</uui-combobox-list>
</uui-combobox>
<span style="margin-left: 16px">Selected value: ${selected}</span> `;
};
export const AAAOverview: StoryFn = Template.bind({});
AAAOverview.args = {
options: fruits,
filter: basicFilter,
};
AAAOverview.storyName = 'Overview';
AAAOverview.parameters = {
docs: {
source: {
code: `
<uui-combobox style="width: 250px">
<uui-combobox-list>
<uui-combobox-list-option style="padding: 8px">
apple
</uui-combobox-list-option>
<uui-combobox-list-option style="padding: 8px">
orange
</uui-combobox-list-option>
<uui-combobox-list-option style="padding: 8px">
lemon
</uui-combobox-list-option>
...
</uui-combobox-list>
</uui-combobox>
`,
},
},
};
export const Disabled: StoryFn = Template.bind({});
Disabled.args = {
options: fruits,
filter: basicFilter,
disabled: true,
value: 'banana',
};
Disabled.parameters = {
docs: {
source: {
code: `
<uui-combobox style="width: 250px" disabled>
<uui-combobox-list>
<uui-combobox-list-option style="padding: 8px">
apple
</uui-combobox-list-option>
<uui-combobox-list-option style="padding: 8px">
orange
</uui-combobox-list-option>
<uui-combobox-list-option style="padding: 8px">
lemon
</uui-combobox-list-option>
...
</uui-combobox-list>
</uui-combobox>
`,
},
},
};
export const CustomValue: StoryFn = Template.bind({});
CustomValue.args = {
options: fruits,
valueMod: (fruit: string) => 'FRUIT_' + fruit.toUpperCase(),
filter: basicFilter,
};
CustomValue.parameters = {
docs: {
source: {
code: `
<uui-combobox style="width: 250px">
<uui-combobox-list>
<uui-combobox-list-option value="FRUIT_APPLE" style="padding: 8px">
apple
</uui-combobox-list-option>
<uui-combobox-list-option value="FRUIT_ORANGE" style="padding: 8px">
orange
</uui-combobox-list-option>
<uui-combobox-list-option value="LEMON" style="padding: 8px">
lemon
</uui-combobox-list-option>
...
</uui-combobox-list>
</uui-combobox>
`,
},
},
};
export const CustomDisplayValue: StoryFn = Template.bind({});
CustomDisplayValue.args = {
options: fruits,
displayValueMod: (fruit: string) =>
fruit.charAt(0).toUpperCase() + fruit.slice(1),
filter: basicFilter,
};
CustomDisplayValue.parameters = {
docs: {
source: {
code: `
<uui-combobox style="width: 250px">
<uui-combobox-list>
<uui-combobox-list-option displayValue="Apple" style="padding: 8px">
apple
</uui-combobox-list-option>
<uui-combobox-list-option displayValue="Orange" style="padding: 8px">
orange
</uui-combobox-list-option>
<uui-combobox-list-option displayValue="Lemon" style="padding: 8px">
lemon
</uui-combobox-list-option>
...
</uui-combobox-list>
</uui-combobox>
`,
},
},
};
export const Avatars: StoryFn = Template.bind({});
Avatars.args = {
options: avatars,
renderMod: (avatar: any) => renderAvatar(avatar),
filter: (options: any[], search: string) =>
options.filter(option =>
option.name.toLowerCase().includes(search.toLowerCase())
),
};
export const CountrySelect: StoryFn = props => {
const [, updateSearch] = useArgs();
const [, updateSelected] = useArgs();
const handleSearch = (e: any) => {
props.search = e.target.search;
updateSearch(props);
};
const handleSelect = (e: any) => {
props.selected = e.target.value;
updateSelected(props);
};
const renderCountry = (country: any) =>
html`<uui-combobox-list-option
style="scroll-margin-top: 40px; display: flex; align-items: center; gap: 8px; padding: 8px 8px;"
.value=${country.ISOAlpha3Code}
.displayValue=${country.countryName}>
<img
style="height: 24px"
src=${country.flag}
alt=${country.countryName} />${country.countryName}
</uui-combobox-list-option>`;
const renderRegion = (region: any, index: number) => html`
<span
style=${`${
index > 0 ? 'margin-top: 6px' : ''
}; position: sticky; top: 0; text-align: center; padding: 8px; margin-bottom: 6px; font-weight: bold; color: #333333; background: #eeeeee; z-index: 1; outline: 1px solid var(--uui-color-border,#c4c4c4);`}>
${region.name}
</span>
${repeat(
region.countries,
(item: any) => item.ISOAlpha3Code,
item => renderCountry(item)
)}
`;
const filterOptions = (regions: any, search: string): any[] => {
const filteredRegions = regions.filter((region: any) =>
region.countries.some((country: any) =>
country.countryName.toLowerCase().includes(search.toLowerCase())
)
);
const filterFinal = filteredRegions.map((region: any) => ({
name: region.name,
countries: region.countries.filter((country: any) =>
country.countryName.toLowerCase().includes(search.toLowerCase())
),
}));
return filterFinal;
};
const renderFilteredOptions = () => {
const options = filterOptions(props.regions, props.search).map(
(region: any, i: number) => renderRegion(region, i)
);
return options.length > 0
? options
: html`<div style="text-align: center; padding: var(--uui-size-4);">
No countries found
</div>`;
};
const renderSelectedFlag = () => {
const country = props.regions
.flatMap((region: any) => region.countries)
.find((country: any) => country.ISOAlpha3Code === props.selected);
return props.selected
? html`<img
style="width: 24px; display: flex; padding-left: var(--uui-size-3);"
src=${country.flag}
alt="" />`
: '';
};
return html`<uui-combobox
.value=${props.selected}
style="--uui-combobox-popover-max-height: 300px; width: 250px;"
@search=${handleSearch}
@change=${handleSelect}>
<span slot="input-prepend" style="display: flex; align-items: center;"
>${renderSelectedFlag()}</span
>
<uui-combobox-list>${renderFilteredOptions()}</uui-combobox-list>
</uui-combobox>
<span style="margin-left: 16px">Selected value: ${props.selected}</span> `;
};
Avatars.parameters = {
docs: {
source: {
code: `
<uui-combobox style="width: 250px">
<uui-combobox-list>
<uui-combobox-list-option display-value="Superman" value="SM" style="display: flex; gap: 9px; align-items: center; padding: var(--uui-size-3)">
<uui-avatar style="background-color: #c8d1dd"></uui-avatar>
<div style="display: flex; flex-direction: column">
<b>Superman</b>
<div style="font-size: 0.8rem">A pretty strong guy</div>
</div>
</uui-combobox-list-option>
<uui-combobox-list-option display-value="R2-D2" value="RD" style="display: flex; gap: 9px; align-items: center; padding: var(--uui-size-3)">
<uui-avatar style="background-color: #c8d1dd"></uui-avatar>
<div style="display: flex; flex-direction: column">
<b>R2-D2</b>
<div style="font-size: 0.8rem">Bip Bub</div>
</div>
</uui-combobox-list-option>
<uui-combobox-list-option display-value="Luke Skywalker" value="LS" style="display: flex; gap: 9px; align-items: center; padding: var(--uui-size-3)">
<uui-avatar style="background-color: #c8d1dd"></uui-avatar>
<div style="display: flex; flex-direction: column">
<b>Luke Skywalker</b>
<div style="font-size: 0.8rem">Guy with a funky sword</div>
</div>
</uui-combobox-list-option>
<uui-combobox-list-option display-value="Batman" value="BM" style="display: flex; gap: 9px; align-items: center; padding: var(--uui-size-3)">
<uui-avatar style="background-color: #c8d1dd"></uui-avatar>
<div style="display: flex; flex-direction: column">
<b>Batman</b>
<div style="font-size: 0.8rem">I'M BATMAN!</div>
</div>
</uui-combobox-list-option>
</uui-combobox-list>
</uui-combobox>
`,
},
},
};
CountrySelect.parameters = {
docs: {
source: {
code: `
<uui-combobox style="width: 250px">
<uui-combobox-list>
<span style="; position: sticky; top: 0; text-align: center; padding: 8px; margin-bottom: 6px; font-weight: bold; color: #333333; background: #eeeeee; z-index: 1; outline: 1px solid var(--uui-color-border,#c4c4c4);">
Africa
</span>
<uui-combobox-list-option style="scroll-margin-top: 40px; display: flex; align-items: center; gap: 8px; padding: 8px 8px;" tabindex="0">
<img style="height: 24px" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/images/DZ.svg" alt="Algeria">
Algeria
</uui-combobox-list-option>
<uui-combobox-list-option style="scroll-margin-top: 40px; display: flex; align-items: center; gap: 8px; padding: 8px 8px;" tabindex="0">
<img style="height: 24px" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/images/EG.svg" alt="Egypt">
Egypt
</uui-combobox-list-option>
<uui-combobox-list-option style="scroll-margin-top: 40px; display: flex; align-items: center; gap: 8px; padding: 8px 8px;" tabindex="0">
<img style="height: 24px" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/images/LY.svg" alt="Libya">
Libya
</uui-combobox-list-option>
...
</uui-combobox-list>
</uui-combobox>
`,
},
},
};
CountrySelect.args = {
search: '',
selected: 'DK',
regions: RegionsAndCountries,
};