uikit-react-public 0.51.0 → 0.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/dist/components/MultiSelect/MultiSelect.d.ts +4 -0
  2. package/dist/components/MultiSelect/MultiSelect.stories.d.ts +206 -0
  3. package/dist/components/MultiSelect/MultiSelect.types.d.ts +128 -0
  4. package/dist/components/MultiSelect/__tests__/MultiSelect.test.d.ts +1 -0
  5. package/dist/components/MultiSelect/index.d.ts +2 -0
  6. package/dist/components/MultiSelect/subcomponents/CustomOption.d.ts +3 -0
  7. package/dist/components/MultiSelect/subcomponents/CustomSelect.d.ts +4 -0
  8. package/dist/components/MultiSelect/subcomponents/FilterInput.d.ts +16 -0
  9. package/dist/components/MultiSelect/subcomponents/Panel.d.ts +8 -0
  10. package/dist/components/MultiSelect/subcomponents/VisibleField.d.ts +17 -0
  11. package/dist/components/MultiSelect/subcomponents/index.d.ts +5 -0
  12. package/dist/components/index.d.ts +2 -0
  13. package/dist/index.js +963 -498
  14. package/lib/components/MultiSelect/MultiSelect.mdx +151 -0
  15. package/lib/components/MultiSelect/MultiSelect.stories.tsx +308 -0
  16. package/lib/components/MultiSelect/MultiSelect.tsx +24 -0
  17. package/lib/components/MultiSelect/MultiSelect.types.ts +142 -0
  18. package/lib/components/MultiSelect/__tests__/MultiSelect.test.tsx +1300 -0
  19. package/lib/components/MultiSelect/__tests__/__snapshots__/MultiSelect.test.tsx.snap +44 -0
  20. package/lib/components/MultiSelect/index.ts +2 -0
  21. package/lib/components/MultiSelect/subcomponents/CustomOption.tsx +107 -0
  22. package/lib/components/MultiSelect/subcomponents/CustomSelect.tsx +701 -0
  23. package/lib/components/MultiSelect/subcomponents/FilterInput.tsx +90 -0
  24. package/lib/components/MultiSelect/subcomponents/Panel.tsx +69 -0
  25. package/lib/components/MultiSelect/subcomponents/VisibleField.tsx +147 -0
  26. package/lib/components/MultiSelect/subcomponents/index.tsx +5 -0
  27. package/lib/components/index.ts +3 -0
  28. package/package.json +1 -1
@@ -0,0 +1,1300 @@
1
+ import { describe, expect, test, vi, beforeAll } from 'vitest';
2
+ import { useState } from 'react';
3
+ import {
4
+ createEvent,
5
+ fireEvent,
6
+ render,
7
+ waitFor,
8
+ } from '@testing-library/react';
9
+ import userEvent from '@testing-library/user-event';
10
+ import MultiSelect from '../MultiSelect.tsx';
11
+ import { ThemeContextProvider } from '../../../theme/useTheme';
12
+
13
+ const defaultOptions = [
14
+ { label: 'Option 1', value: '1' },
15
+ { label: 'Option 2', value: '2' },
16
+ { label: 'Option 3', value: '3' },
17
+ ];
18
+
19
+ describe('MultiSelect', () => {
20
+ beforeAll(() => {
21
+ Object.defineProperty(Element.prototype, 'scrollIntoView', {
22
+ value: vi.fn(),
23
+ writable: true,
24
+ });
25
+ });
26
+
27
+ // Snapshot tests
28
+
29
+ test('Snapshot: default', () => {
30
+ const renderResult = render(
31
+ <ThemeContextProvider>
32
+ <MultiSelect
33
+ options={defaultOptions}
34
+ value={[]}
35
+ onValueChange={() => {}}
36
+ />
37
+ </ThemeContextProvider>
38
+ );
39
+ expect(renderResult.container.firstChild).toMatchSnapshot();
40
+ });
41
+
42
+ // Interation tests
43
+ test('Can be found by default testId', () => {
44
+ const defaultTestId = 'ucl-uikit-multiselect';
45
+
46
+ const renderResult = render(
47
+ <ThemeContextProvider>
48
+ <MultiSelect
49
+ options={defaultOptions}
50
+ value={[]}
51
+ onValueChange={() => {}}
52
+ />
53
+ </ThemeContextProvider>
54
+ );
55
+ const select = renderResult.getByTestId(defaultTestId);
56
+ expect(select).toBeDefined();
57
+ });
58
+
59
+ test('Opens the panel when clicked', async () => {
60
+ const user = userEvent.setup();
61
+ const renderResult = render(
62
+ <ThemeContextProvider>
63
+ <MultiSelect
64
+ options={defaultOptions}
65
+ value={[]}
66
+ onValueChange={() => {}}
67
+ />
68
+ </ThemeContextProvider>
69
+ );
70
+ const select = renderResult.getByTestId('ucl-uikit-multiselect');
71
+ await user.click(select);
72
+ // Panel should open
73
+ const panel = renderResult.getByTestId('ucl-uikit-multiselect__panel');
74
+ expect(panel).toBeInTheDocument();
75
+ // Options should be present
76
+ const options = renderResult.getAllByTestId(
77
+ 'ucl-uikit-multiselect__option'
78
+ );
79
+ expect(options.length).toBe(defaultOptions.length);
80
+ expect(options[0]).toHaveTextContent(defaultOptions[0].label);
81
+ expect(options[1]).toHaveTextContent(defaultOptions[1].label);
82
+ expect(options[2]).toHaveTextContent(defaultOptions[2].label);
83
+ });
84
+
85
+ test('renders the open panel inside an overlay', async () => {
86
+ const user = userEvent.setup();
87
+ const renderResult = render(
88
+ <ThemeContextProvider>
89
+ <MultiSelect
90
+ options={defaultOptions}
91
+ value={[]}
92
+ onValueChange={() => {}}
93
+ />
94
+ </ThemeContextProvider>
95
+ );
96
+
97
+ await user.click(renderResult.getByRole('combobox'));
98
+
99
+ const panel = renderResult.getByTestId('ucl-uikit-multiselect__panel');
100
+ const overlay = panel.parentElement;
101
+
102
+ expect(overlay).toHaveClass('ucl-overlay');
103
+ await waitFor(() => {
104
+ expect(overlay).toHaveStyle({
105
+ position: 'fixed',
106
+ });
107
+ });
108
+ });
109
+
110
+ test.each([
111
+ { fieldTop: 200, menuHeight: 120, menuTop: 256 },
112
+ { fieldTop: 500, menuHeight: 120, menuTop: 372 },
113
+ { fieldTop: 280, menuHeight: 400, menuTop: 336 },
114
+ ])(
115
+ 'anchors the menu without covering the input at $fieldTop with height $menuHeight',
116
+ async ({ fieldTop, menuHeight, menuTop }) => {
117
+ vi.spyOn(document.documentElement, 'clientWidth', 'get').mockReturnValue(
118
+ 800
119
+ );
120
+ vi.spyOn(document.documentElement, 'clientHeight', 'get').mockReturnValue(
121
+ 600
122
+ );
123
+ vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(
124
+ 240
125
+ );
126
+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(
127
+ menuHeight
128
+ );
129
+ vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(
130
+ function (this: Element) {
131
+ return this.getAttribute('role') === 'combobox'
132
+ ? new DOMRect(80, fieldTop, 240, 48)
133
+ : new DOMRect(0, 0, 240, menuHeight);
134
+ }
135
+ );
136
+
137
+ try {
138
+ const user = userEvent.setup();
139
+ const result = render(
140
+ <ThemeContextProvider>
141
+ <MultiSelect
142
+ filterable
143
+ options={defaultOptions}
144
+ value={['2']}
145
+ onValueChange={() => {}}
146
+ />
147
+ </ThemeContextProvider>
148
+ );
149
+
150
+ await user.click(result.getByRole('combobox'));
151
+
152
+ const expectAnchoredMenu = () => {
153
+ expect(result.getByRole('listbox').parentElement).toHaveStyle({
154
+ position: 'fixed',
155
+ left: '80px',
156
+ top: `${menuTop}px`,
157
+ });
158
+ };
159
+ await waitFor(expectAnchoredMenu);
160
+
161
+ await user.type(result.getByRole('searchbox'), '2');
162
+ await waitFor(expectAnchoredMenu);
163
+ } finally {
164
+ vi.restoreAllMocks();
165
+ }
166
+ }
167
+ );
168
+
169
+ test('dropdownWidth defaults to content width', async () => {
170
+ const user = userEvent.setup();
171
+ const renderResult = render(
172
+ <ThemeContextProvider>
173
+ <MultiSelect
174
+ options={defaultOptions}
175
+ value={[]}
176
+ onValueChange={() => {}}
177
+ />
178
+ </ThemeContextProvider>
179
+ );
180
+
181
+ await user.click(renderResult.getByRole('combobox'));
182
+
183
+ expect(renderResult.getByRole('listbox')).toHaveStyle({
184
+ width: 'fit-content',
185
+ });
186
+ });
187
+
188
+ test('dropdownWidth=match-select caps panel width to the select width', async () => {
189
+ const user = userEvent.setup();
190
+ const getBoundingClientRectSpy = vi.spyOn(
191
+ Element.prototype,
192
+ 'getBoundingClientRect'
193
+ );
194
+
195
+ getBoundingClientRectSpy.mockImplementation(() => ({
196
+ x: 0,
197
+ y: 0,
198
+ top: 0,
199
+ left: 0,
200
+ right: 240,
201
+ bottom: 48,
202
+ width: 240,
203
+ height: 48,
204
+ toJSON: () => {},
205
+ }));
206
+
207
+ const renderResult = render(
208
+ <ThemeContextProvider>
209
+ <MultiSelect
210
+ dropdownWidth='match-select'
211
+ options={defaultOptions}
212
+ value={[]}
213
+ onValueChange={() => {}}
214
+ />
215
+ </ThemeContextProvider>
216
+ );
217
+
218
+ await user.click(renderResult.getByRole('combobox'));
219
+
220
+ await waitFor(() => {
221
+ expect(renderResult.getByRole('listbox')).toHaveStyle({
222
+ width: '240px',
223
+ maxWidth: '240px',
224
+ });
225
+ });
226
+
227
+ getBoundingClientRectSpy.mockRestore();
228
+ });
229
+
230
+ test('open dropdown closes when clicking outside', async () => {
231
+ const user = userEvent.setup();
232
+ const renderResult = render(
233
+ <ThemeContextProvider>
234
+ <div>
235
+ <MultiSelect
236
+ options={defaultOptions}
237
+ value={[]}
238
+ onValueChange={() => {}}
239
+ />
240
+ <button type='button'>Outside</button>
241
+ </div>
242
+ </ThemeContextProvider>
243
+ );
244
+
245
+ await user.click(renderResult.getByRole('combobox'));
246
+ expect(renderResult.getByRole('listbox')).toBeInTheDocument();
247
+
248
+ await user.click(renderResult.getByRole('button', { name: 'Outside' }));
249
+
250
+ expect(renderResult.queryByRole('listbox')).not.toBeInTheDocument();
251
+ });
252
+
253
+ test('panel width is capped to dialog body width inside a dialog', async () => {
254
+ const user = userEvent.setup();
255
+ const getBoundingClientRectSpy = vi.spyOn(
256
+ Element.prototype,
257
+ 'getBoundingClientRect'
258
+ );
259
+
260
+ getBoundingClientRectSpy.mockImplementation(function (this: Element) {
261
+ if (this.getAttribute('role') === 'document') {
262
+ return {
263
+ x: 0,
264
+ y: 0,
265
+ top: 0,
266
+ left: 0,
267
+ right: 431,
268
+ bottom: 200,
269
+ width: 431,
270
+ height: 200,
271
+ toJSON: () => {},
272
+ };
273
+ }
274
+
275
+ return {
276
+ x: 0,
277
+ y: 0,
278
+ top: 0,
279
+ left: 0,
280
+ right: 400,
281
+ bottom: 48,
282
+ width: 400,
283
+ height: 48,
284
+ toJSON: () => {},
285
+ };
286
+ });
287
+
288
+ const result = render(
289
+ <ThemeContextProvider>
290
+ <dialog open>
291
+ <div role='document'>
292
+ <MultiSelect
293
+ options={[
294
+ {
295
+ label:
296
+ 'A very long option label that should be truncated in the dialog',
297
+ value: 'long',
298
+ },
299
+ ]}
300
+ value={[]}
301
+ onValueChange={() => {}}
302
+ />
303
+ </div>
304
+ </dialog>
305
+ </ThemeContextProvider>
306
+ );
307
+
308
+ await user.click(result.getByRole('combobox'));
309
+
310
+ await waitFor(() => {
311
+ expect(result.getByRole('listbox')).toHaveStyle({
312
+ maxWidth: 'min(431px, calc(100vw - (32px * 2)))',
313
+ });
314
+ });
315
+
316
+ getBoundingClientRectSpy.mockRestore();
317
+ });
318
+
319
+ test('Cannot be used when disabled', async () => {
320
+ const user = userEvent.setup();
321
+ const renderResult = render(
322
+ <ThemeContextProvider>
323
+ <MultiSelect
324
+ disabled
325
+ options={defaultOptions}
326
+ value={[]}
327
+ onValueChange={() => {}}
328
+ />
329
+ </ThemeContextProvider>
330
+ );
331
+ const select = renderResult.getByTestId('ucl-uikit-multiselect');
332
+ await user.click(select);
333
+ // Panel should not open
334
+ const panel = renderResult.queryByTestId('ucl-uikit-multiselect__panel');
335
+ expect(panel).not.toBeInTheDocument();
336
+ });
337
+
338
+ test('Filters options when filterable is true', async () => {
339
+ const user = userEvent.setup();
340
+ const renderResult = render(
341
+ <ThemeContextProvider>
342
+ <MultiSelect
343
+ filterable
344
+ options={defaultOptions}
345
+ value={[]}
346
+ onValueChange={() => {}}
347
+ />
348
+ </ThemeContextProvider>
349
+ );
350
+ await user.click(renderResult.getByTestId('ucl-uikit-multiselect'));
351
+ const input = renderResult.container.querySelector('input');
352
+ expect(input).not.toBeNull();
353
+ await user.type(input as HTMLInputElement, '2');
354
+
355
+ const options = await renderResult.findAllByRole('option');
356
+ expect(options.length).toBe(1);
357
+ expect(options[0].textContent).toBe('Option 2');
358
+ });
359
+
360
+ test('shows empty state when filter yields no options', async () => {
361
+ const user = userEvent.setup();
362
+ const result = render(
363
+ <ThemeContextProvider>
364
+ <MultiSelect
365
+ filterable
366
+ options={defaultOptions}
367
+ value={[]}
368
+ onValueChange={() => {}}
369
+ />
370
+ </ThemeContextProvider>
371
+ );
372
+
373
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
374
+ await user.type(result.getByRole('searchbox'), 'zzz');
375
+
376
+ expect(result.getByText('No options')).toBeInTheDocument();
377
+ expect(
378
+ result.queryAllByTestId('ucl-uikit-multiselect__option')
379
+ ).toHaveLength(0);
380
+ });
381
+
382
+ test('clears filter text after selecting an option and reopening multiselect', async () => {
383
+ const user = userEvent.setup();
384
+ const ControlledSelect = () => {
385
+ const [value, setValue] = useState<string[]>([]);
386
+ return (
387
+ <MultiSelect
388
+ filterable
389
+ options={defaultOptions}
390
+ value={value}
391
+ onValueChange={(next) => setValue(next)}
392
+ />
393
+ );
394
+ };
395
+
396
+ const result = render(
397
+ <ThemeContextProvider>
398
+ <ControlledSelect />
399
+ </ThemeContextProvider>
400
+ );
401
+
402
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
403
+ await user.type(result.getByRole('searchbox'), '2');
404
+ await user.click(result.getByText('Option 2'));
405
+
406
+ await user.click(document.body);
407
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
408
+ expect(result.getByRole('searchbox')).toHaveValue('');
409
+ expect(result.getAllByTestId('ucl-uikit-multiselect__option')).toHaveLength(
410
+ defaultOptions.length
411
+ );
412
+ });
413
+
414
+ test('clears filter text when dropdown closes', async () => {
415
+ const user = userEvent.setup();
416
+ const result = render(
417
+ <ThemeContextProvider>
418
+ <MultiSelect
419
+ filterable
420
+ options={defaultOptions}
421
+ value={[]}
422
+ onValueChange={() => {}}
423
+ />
424
+ </ThemeContextProvider>
425
+ );
426
+
427
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
428
+ await user.type(result.getByRole('searchbox'), '1');
429
+
430
+ await user.click(document.body);
431
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
432
+
433
+ expect(result.getByRole('searchbox')).toHaveValue('');
434
+ });
435
+
436
+ test('keyboard navigation uses filtered options', async () => {
437
+ const user = userEvent.setup();
438
+ const options = [
439
+ { label: 'Alpha', value: 'alpha' },
440
+ { label: 'Echo', value: 'echo' },
441
+ { label: 'Gamma', value: 'gamma' },
442
+ ];
443
+ const changeSpy = vi.fn();
444
+
445
+ const ControlledSelect = () => {
446
+ const [value, setValue] = useState<string[]>([]);
447
+ const handleChange = vi.fn((next, ev) => {
448
+ setValue(next);
449
+ changeSpy(next, ev);
450
+ });
451
+ return (
452
+ <MultiSelect
453
+ filterable
454
+ options={options}
455
+ value={value}
456
+ onValueChange={handleChange}
457
+ />
458
+ );
459
+ };
460
+
461
+ const result = render(
462
+ <ThemeContextProvider>
463
+ <ControlledSelect />
464
+ </ThemeContextProvider>
465
+ );
466
+
467
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
468
+ await user.type(result.getByRole('searchbox'), 'a');
469
+ expect(result.getAllByTestId('ucl-uikit-multiselect__option')).toHaveLength(
470
+ 2
471
+ );
472
+
473
+ await user.keyboard('{ArrowDown}');
474
+ await user.keyboard('{ArrowDown}');
475
+ await user.keyboard('{ArrowDown}');
476
+ await user.keyboard('{Enter}');
477
+
478
+ expect(changeSpy.mock.calls.map(([val]) => val)).toEqual([['alpha']]);
479
+ });
480
+
481
+ test('arrow keys move highlight without calling onValueChange', async () => {
482
+ const user = userEvent.setup();
483
+ const changeSpy = vi.fn();
484
+
485
+ const ControlledSelect = () => {
486
+ const [value, setValue] = useState<string[]>([]);
487
+ return (
488
+ <MultiSelect
489
+ options={defaultOptions}
490
+ value={value}
491
+ onValueChange={(next, ev) => {
492
+ setValue(next);
493
+ changeSpy(next, ev);
494
+ }}
495
+ />
496
+ );
497
+ };
498
+
499
+ const result = render(
500
+ <ThemeContextProvider>
501
+ <ControlledSelect />
502
+ </ThemeContextProvider>
503
+ );
504
+
505
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
506
+ await user.keyboard('{ArrowDown}');
507
+ await user.keyboard('{ArrowDown}');
508
+ await user.keyboard('{ArrowDown}');
509
+
510
+ expect(changeSpy).not.toHaveBeenCalled();
511
+ });
512
+
513
+ test('Enter toggles the highlighted option and keeps the panel open', async () => {
514
+ const user = userEvent.setup();
515
+ const changeSpy = vi.fn();
516
+
517
+ const ControlledSelect = () => {
518
+ const [value, setValue] = useState<string[]>([]);
519
+ return (
520
+ <MultiSelect
521
+ options={defaultOptions}
522
+ value={value}
523
+ onValueChange={(next, ev) => {
524
+ setValue(next);
525
+ changeSpy(next, ev);
526
+ }}
527
+ />
528
+ );
529
+ };
530
+
531
+ const result = render(
532
+ <ThemeContextProvider>
533
+ <ControlledSelect />
534
+ </ThemeContextProvider>
535
+ );
536
+
537
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
538
+ await user.keyboard('{ArrowDown}');
539
+ await user.keyboard('{ArrowDown}');
540
+ await user.keyboard('{Enter}');
541
+
542
+ expect(changeSpy.mock.calls.map(([val]) => val)).toEqual([['2']]);
543
+ expect(
544
+ result.getByTestId('ucl-uikit-multiselect__panel')
545
+ ).toBeInTheDocument();
546
+ // expect(result.getByTestId('ucl-uikit-multiselect')).toHaveTextContent(
547
+ // 'Option 2'
548
+ // );
549
+ });
550
+
551
+ test('Enter keeps the panel open and the filter input focused (filterable)', async () => {
552
+ const user = userEvent.setup();
553
+
554
+ const ControlledSelect = () => {
555
+ const [value, setValue] = useState<string[]>([]);
556
+ return (
557
+ <MultiSelect
558
+ filterable
559
+ options={defaultOptions}
560
+ value={value}
561
+ onValueChange={(next) => setValue(next)}
562
+ />
563
+ );
564
+ };
565
+
566
+ const result = render(
567
+ <ThemeContextProvider>
568
+ <ControlledSelect />
569
+ </ThemeContextProvider>
570
+ );
571
+
572
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
573
+ // Filter input has focus while the panel is open
574
+ expect(result.getByRole('searchbox')).toHaveFocus();
575
+
576
+ await user.keyboard('{ArrowDown}');
577
+ await user.keyboard('{Enter}');
578
+
579
+ // Panel (and its filter input) stays open to allow multiple selections
580
+ expect(
581
+ result.getByTestId('ucl-uikit-multiselect__panel')
582
+ ).toBeInTheDocument();
583
+ expect(result.getByRole('searchbox')).toHaveFocus();
584
+ });
585
+
586
+ test('announces the selected option via a live region', async () => {
587
+ const user = userEvent.setup();
588
+
589
+ const ControlledSelect = () => {
590
+ const [value, setValue] = useState<string[]>([]);
591
+ return (
592
+ <MultiSelect
593
+ filterable
594
+ options={defaultOptions}
595
+ value={value}
596
+ onValueChange={(next) => setValue(next)}
597
+ />
598
+ );
599
+ };
600
+
601
+ const result = render(
602
+ <ThemeContextProvider>
603
+ <ControlledSelect />
604
+ </ThemeContextProvider>
605
+ );
606
+
607
+ const status = result.getByRole('status');
608
+ expect(status).toHaveTextContent('');
609
+
610
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
611
+ await user.keyboard('{ArrowDown}');
612
+ await user.keyboard('{ArrowDown}');
613
+ await user.keyboard('{Enter}');
614
+
615
+ expect(status).toHaveTextContent('Option 2 selected');
616
+ });
617
+
618
+ test('accumulates multiple selections via keyboard (including duplicate values)', async () => {
619
+ const user = userEvent.setup();
620
+ const options = [
621
+ { label: 'First duplicate', value: 'dup' },
622
+ { label: 'Second duplicate', value: 'dup' },
623
+ { label: 'Unique', value: 'unique' },
624
+ ];
625
+ const changeSpy = vi.fn();
626
+
627
+ const ControlledSelect = () => {
628
+ const [value, setValue] = useState<string[]>([]);
629
+ const handleChange = vi.fn((next, ev) => {
630
+ setValue(next);
631
+ changeSpy(next, ev);
632
+ });
633
+ return (
634
+ <MultiSelect
635
+ options={options}
636
+ value={value}
637
+ onValueChange={handleChange}
638
+ />
639
+ );
640
+ };
641
+
642
+ const result = render(
643
+ <ThemeContextProvider>
644
+ <ControlledSelect />
645
+ </ThemeContextProvider>
646
+ );
647
+
648
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
649
+ await user.keyboard('{ArrowDown}');
650
+ await user.keyboard('{Enter}');
651
+
652
+ await user.keyboard('{ArrowDown}');
653
+ await user.keyboard('{ArrowDown}');
654
+ await user.keyboard('{Enter}');
655
+
656
+ // Both selections should accumulate for multi-select
657
+ expect(changeSpy.mock.calls.map(([val]) => val)).toEqual([
658
+ ['dup'],
659
+ ['dup', 'unique'],
660
+ ]);
661
+ });
662
+
663
+ test('keeps duplicate label identity when selecting by click', async () => {
664
+ const user = userEvent.setup();
665
+ const options = [
666
+ { label: 'First duplicate', value: 'dup' },
667
+ { label: 'Second duplicate', value: 'dup' },
668
+ ];
669
+
670
+ const ControlledSelect = () => {
671
+ const [value, setValue] = useState<string[]>([]);
672
+ return (
673
+ <MultiSelect
674
+ options={options}
675
+ value={value}
676
+ onValueChange={(next) => setValue(next)}
677
+ />
678
+ );
679
+ };
680
+
681
+ const result = render(
682
+ <ThemeContextProvider>
683
+ <ControlledSelect />
684
+ </ThemeContextProvider>
685
+ );
686
+
687
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
688
+ await user.click(result.getAllByRole('option')[1]);
689
+
690
+ expect(result.getByTestId('ucl-uikit-multiselect')).toHaveTextContent(
691
+ 'Second duplicate'
692
+ );
693
+ });
694
+
695
+ test('filterable select keeps option clicks working while the filter input is focused', async () => {
696
+ const user = userEvent.setup();
697
+
698
+ const ControlledSelect = () => {
699
+ const [value, setValue] = useState<string[]>([]);
700
+ return (
701
+ <MultiSelect
702
+ filterable
703
+ options={defaultOptions}
704
+ value={value}
705
+ onValueChange={(next) => setValue(next)}
706
+ />
707
+ );
708
+ };
709
+
710
+ const result = render(
711
+ <ThemeContextProvider>
712
+ <ControlledSelect />
713
+ </ThemeContextProvider>
714
+ );
715
+
716
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
717
+ const filterInput = result.getByRole('searchbox');
718
+ expect(filterInput).toHaveFocus();
719
+
720
+ await user.click(result.getByRole('option', { name: 'Option 2' }));
721
+
722
+ expect(
723
+ result.queryByTestId('ucl-uikit-multiselect__panel')
724
+ ).toBeInTheDocument();
725
+ expect(result.getByRole('option', { name: 'Option 2' })).toHaveAttribute(
726
+ 'aria-selected',
727
+ 'true'
728
+ );
729
+ });
730
+
731
+ test('filterable: typing and pressing Enter selects the first filtered option', async () => {
732
+ const user = userEvent.setup();
733
+ const options = [
734
+ { label: 'Alpha', value: 'alpha' },
735
+ { label: 'Beta', value: 'beta' },
736
+ { label: 'Avocado', value: 'avocado' },
737
+ ];
738
+ const changeSpy = vi.fn();
739
+
740
+ const ControlledSelect = () => {
741
+ const [value, setValue] = useState<string[]>([]);
742
+ return (
743
+ <MultiSelect
744
+ filterable
745
+ options={options}
746
+ value={value}
747
+ onValueChange={(next, ev) => {
748
+ setValue(next);
749
+ changeSpy(next, ev);
750
+ }}
751
+ />
752
+ );
753
+ };
754
+
755
+ const result = render(
756
+ <ThemeContextProvider>
757
+ <ControlledSelect />
758
+ </ThemeContextProvider>
759
+ );
760
+
761
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
762
+
763
+ await user.type(result.getByRole('searchbox'), 'av');
764
+ expect(result.getAllByTestId('ucl-uikit-multiselect__option')).toHaveLength(
765
+ 1
766
+ );
767
+
768
+ // Enter (with no option highlighted) commits the top visible option
769
+ await user.keyboard('{Enter}');
770
+
771
+ expect(changeSpy).toHaveBeenLastCalledWith(['avocado'], expect.any(Object));
772
+ // Filter should clear after committing but panel should be open to allow multiple selections
773
+ expect(result.getByRole('searchbox')).toHaveValue('');
774
+ expect(
775
+ result.getByTestId('ucl-uikit-multiselect__panel')
776
+ ).toBeInTheDocument();
777
+ });
778
+
779
+ test('filterable: selecting a second filtered option keeps the previous selection', async () => {
780
+ const user = userEvent.setup();
781
+ const options = [
782
+ { label: 'Alpha', value: 'alpha' },
783
+ { label: 'Beta', value: 'beta' },
784
+ { label: 'Gamma', value: 'gamma' },
785
+ ];
786
+ const changeSpy = vi.fn();
787
+
788
+ const ControlledSelect = () => {
789
+ const [value, setValue] = useState<string[]>([]);
790
+ return (
791
+ <MultiSelect
792
+ filterable
793
+ options={options}
794
+ value={value}
795
+ onValueChange={(next, ev) => {
796
+ setValue(next);
797
+ changeSpy(next, ev);
798
+ }}
799
+ />
800
+ );
801
+ };
802
+
803
+ const result = render(
804
+ <ThemeContextProvider>
805
+ <ControlledSelect />
806
+ </ThemeContextProvider>
807
+ );
808
+
809
+ await user.click(result.getByTestId('ucl-uikit-multiselect'));
810
+
811
+ await user.type(result.getByRole('searchbox'), 'alph');
812
+ await user.keyboard('{Enter}');
813
+ expect(changeSpy).toHaveBeenLastCalledWith(['alpha'], expect.any(Object));
814
+
815
+ expect(result.getByRole('searchbox')).toHaveValue('');
816
+ await user.type(result.getByRole('searchbox'), 'gam');
817
+ await user.keyboard('{Enter}');
818
+
819
+ // Previous selection should be preserved
820
+ expect(changeSpy).toHaveBeenLastCalledWith(
821
+ ['alpha', 'gamma'],
822
+ expect.any(Object)
823
+ );
824
+
825
+ expect(changeSpy.mock.calls.map(([val]) => val)).toEqual([
826
+ ['alpha'],
827
+ ['alpha', 'gamma'],
828
+ ]);
829
+ });
830
+
831
+ test('forwards id and title attributes to the combobox', () => {
832
+ const result = render(
833
+ <ThemeContextProvider>
834
+ <MultiSelect
835
+ id='my-select'
836
+ title='Select an option'
837
+ options={defaultOptions}
838
+ value={[]}
839
+ onValueChange={() => {}}
840
+ />
841
+ </ThemeContextProvider>
842
+ );
843
+
844
+ const combobox = result.getByRole('combobox');
845
+ expect(combobox).toHaveAttribute('id', 'my-select');
846
+ expect(combobox).toHaveAttribute('title', 'Select an option');
847
+ });
848
+
849
+ test('applies filterInputProps to the filter input', async () => {
850
+ const user = userEvent.setup();
851
+ const result = render(
852
+ <ThemeContextProvider>
853
+ <div>
854
+ <span id='filter-hint'>Filter options by text</span>
855
+ <MultiSelect
856
+ filterable
857
+ options={defaultOptions}
858
+ value={[]}
859
+ onValueChange={() => {}}
860
+ filterInputProps={{ 'aria-describedby': 'filter-hint' }}
861
+ />
862
+ </div>
863
+ </ThemeContextProvider>
864
+ );
865
+
866
+ await user.click(result.getByRole('combobox'));
867
+ const filterInput = result.getByRole('searchbox');
868
+ expect(filterInput).toHaveAttribute('aria-describedby', 'filter-hint');
869
+ });
870
+
871
+ test('keeps a filterable select closed on keyboard focus', async () => {
872
+ const user = userEvent.setup();
873
+ const result = render(
874
+ <ThemeContextProvider>
875
+ <MultiSelect
876
+ filterable
877
+ options={defaultOptions}
878
+ value={[]}
879
+ onValueChange={() => {}}
880
+ />
881
+ </ThemeContextProvider>
882
+ );
883
+
884
+ await user.tab();
885
+
886
+ const combobox = result.getByRole('combobox');
887
+ expect(combobox).toHaveFocus();
888
+ expect(combobox).toHaveAttribute('aria-expanded', 'false');
889
+ expect(result.queryByRole('searchbox')).not.toBeInTheDocument();
890
+ expect(result.queryByRole('listbox')).not.toBeInTheDocument();
891
+ });
892
+
893
+ test('does not duplicate the selected option text while closed', async () => {
894
+ const user = userEvent.setup();
895
+ const result = render(
896
+ <ThemeContextProvider>
897
+ <MultiSelect
898
+ filterable
899
+ options={defaultOptions}
900
+ value={[defaultOptions[1].value]}
901
+ onValueChange={() => {}}
902
+ />
903
+ </ThemeContextProvider>
904
+ );
905
+
906
+ await user.tab();
907
+
908
+ const combobox = result.getByRole('combobox');
909
+ expect(combobox).toHaveTextContent(defaultOptions[1].label);
910
+ expect(
911
+ result.queryByText(`${defaultOptions[1].label} selected`)
912
+ ).not.toBeInTheDocument();
913
+ });
914
+
915
+ test('opens a focused filterable select with ArrowDown and focuses its filter', async () => {
916
+ const user = userEvent.setup();
917
+ const result = render(
918
+ <ThemeContextProvider>
919
+ <MultiSelect
920
+ filterable
921
+ options={defaultOptions}
922
+ value={[]}
923
+ onValueChange={() => {}}
924
+ />
925
+ </ThemeContextProvider>
926
+ );
927
+
928
+ await user.tab();
929
+ await user.keyboard('{ArrowDown}');
930
+
931
+ expect(result.getByRole('searchbox')).toHaveFocus();
932
+ expect(result.getByRole('listbox')).toBeInTheDocument();
933
+ });
934
+
935
+ test('opens and filters when typing on a focused closed filterable select', async () => {
936
+ const user = userEvent.setup();
937
+ const result = render(
938
+ <ThemeContextProvider>
939
+ <MultiSelect
940
+ filterable
941
+ options={defaultOptions}
942
+ value={[]}
943
+ onValueChange={() => {}}
944
+ />
945
+ </ThemeContextProvider>
946
+ );
947
+
948
+ await user.tab();
949
+ await user.keyboard('2');
950
+
951
+ expect(result.getByRole('searchbox')).toHaveFocus();
952
+ expect(result.getByRole('searchbox')).toHaveValue('2');
953
+ expect(result.getAllByRole('option')).toHaveLength(1);
954
+ expect(
955
+ result.getByRole('option', { name: 'Option 2' })
956
+ ).toBeInTheDocument();
957
+ });
958
+
959
+ test('pressing Escape closes dropdown and clears filter', async () => {
960
+ const user = userEvent.setup();
961
+ const result = render(
962
+ <ThemeContextProvider>
963
+ <MultiSelect
964
+ filterable
965
+ options={defaultOptions}
966
+ value={[]}
967
+ onValueChange={() => {}}
968
+ />
969
+ </ThemeContextProvider>
970
+ );
971
+
972
+ const combobox = result.getByTestId('ucl-uikit-multiselect');
973
+ await user.click(combobox);
974
+ await user.type(result.getByRole('searchbox'), '1');
975
+ await user.keyboard('{Escape}');
976
+
977
+ expect(
978
+ result.queryByTestId('ucl-uikit-multiselect__panel')
979
+ ).not.toBeInTheDocument();
980
+ expect(combobox).toHaveFocus();
981
+
982
+ await user.click(combobox);
983
+ expect(result.getByRole('searchbox')).toHaveValue('');
984
+ });
985
+
986
+ test('announces active option and option position metadata', async () => {
987
+ const user = userEvent.setup();
988
+ const result = render(
989
+ <ThemeContextProvider>
990
+ <MultiSelect
991
+ options={defaultOptions}
992
+ value={[]}
993
+ onValueChange={() => {}}
994
+ />
995
+ </ThemeContextProvider>
996
+ );
997
+
998
+ const combobox = result.getByRole('combobox');
999
+ await user.click(combobox);
1000
+
1001
+ const options = result.getAllByRole('option');
1002
+ expect(options[0]).toHaveAttribute('aria-posinset', '1');
1003
+ expect(options[0]).toHaveAttribute('aria-setsize', '3');
1004
+ expect(options[1]).toHaveAttribute('aria-posinset', '2');
1005
+ expect(options[1]).toHaveAttribute('aria-setsize', '3');
1006
+
1007
+ await user.keyboard('{ArrowDown}');
1008
+ expect(combobox).toHaveAttribute('aria-activedescendant', options[0].id);
1009
+
1010
+ await user.keyboard('{ArrowDown}');
1011
+ expect(combobox).toHaveAttribute('aria-activedescendant', options[1].id);
1012
+ });
1013
+
1014
+ test('filterable input tracks active descendant while navigating options', async () => {
1015
+ const user = userEvent.setup();
1016
+ const result = render(
1017
+ <ThemeContextProvider>
1018
+ <MultiSelect
1019
+ filterable
1020
+ options={defaultOptions}
1021
+ value={[]}
1022
+ onValueChange={() => {}}
1023
+ />
1024
+ </ThemeContextProvider>
1025
+ );
1026
+
1027
+ await user.click(result.getByRole('combobox'));
1028
+ const filterInput = result.getByRole('searchbox');
1029
+ expect(filterInput).toHaveFocus();
1030
+
1031
+ const options = result.getAllByRole('option');
1032
+ await user.keyboard('{ArrowDown}');
1033
+ expect(filterInput).toHaveAttribute('aria-activedescendant', options[0].id);
1034
+
1035
+ await user.keyboard('{ArrowDown}');
1036
+ expect(filterInput).toHaveAttribute('aria-activedescendant', options[1].id);
1037
+ });
1038
+
1039
+ test('open dropdown closes when tabbing to the next element', async () => {
1040
+ const user = userEvent.setup();
1041
+ const result = render(
1042
+ <ThemeContextProvider>
1043
+ <div>
1044
+ <MultiSelect
1045
+ options={defaultOptions}
1046
+ value={[]}
1047
+ onValueChange={() => {}}
1048
+ />
1049
+ <button type='button'>After</button>
1050
+ </div>
1051
+ </ThemeContextProvider>
1052
+ );
1053
+
1054
+ await user.click(result.getByRole('combobox'));
1055
+ expect(result.getByRole('listbox')).toBeInTheDocument();
1056
+
1057
+ await user.tab();
1058
+
1059
+ expect(result.queryByRole('listbox')).not.toBeInTheDocument();
1060
+ expect(result.getByRole('button', { name: 'After' })).toHaveFocus();
1061
+ });
1062
+
1063
+ test('open filterable dropdown closes when tabbing to the next element', async () => {
1064
+ const user = userEvent.setup();
1065
+ const result = render(
1066
+ <ThemeContextProvider>
1067
+ <div>
1068
+ <MultiSelect
1069
+ filterable
1070
+ options={defaultOptions}
1071
+ value={[]}
1072
+ onValueChange={() => {}}
1073
+ />
1074
+ <button type='button'>After</button>
1075
+ </div>
1076
+ </ThemeContextProvider>
1077
+ );
1078
+
1079
+ await user.click(result.getByRole('combobox'));
1080
+ expect(result.getByRole('searchbox')).toHaveFocus();
1081
+ expect(result.getByRole('listbox')).toBeInTheDocument();
1082
+
1083
+ await user.tab();
1084
+
1085
+ expect(result.queryByRole('listbox')).not.toBeInTheDocument();
1086
+ expect(result.getByRole('button', { name: 'After' })).toHaveFocus();
1087
+ });
1088
+
1089
+ test('open dropdown closes when shift-tabbing to the previous element', async () => {
1090
+ const user = userEvent.setup();
1091
+ const result = render(
1092
+ <ThemeContextProvider>
1093
+ <div>
1094
+ <button type='button'>Before</button>
1095
+ <MultiSelect
1096
+ options={defaultOptions}
1097
+ value={[]}
1098
+ onValueChange={() => {}}
1099
+ />
1100
+ </div>
1101
+ </ThemeContextProvider>
1102
+ );
1103
+
1104
+ await user.tab();
1105
+ await user.tab();
1106
+ expect(result.getByRole('combobox')).toHaveFocus();
1107
+
1108
+ await user.keyboard('{Enter}');
1109
+ expect(result.getByRole('listbox')).toBeInTheDocument();
1110
+
1111
+ await user.tab({ shift: true });
1112
+
1113
+ expect(result.queryByRole('listbox')).not.toBeInTheDocument();
1114
+ expect(result.getByRole('button', { name: 'Before' })).toHaveFocus();
1115
+ });
1116
+
1117
+ test('open filterable dropdown closes when shift-tabbing to the previous element', async () => {
1118
+ const user = userEvent.setup();
1119
+ const result = render(
1120
+ <ThemeContextProvider>
1121
+ <div>
1122
+ <button type='button'>Before</button>
1123
+ <MultiSelect
1124
+ filterable
1125
+ options={defaultOptions}
1126
+ value={[]}
1127
+ onValueChange={() => {}}
1128
+ />
1129
+ </div>
1130
+ </ThemeContextProvider>
1131
+ );
1132
+
1133
+ await user.tab();
1134
+ await user.tab();
1135
+ expect(result.getByRole('combobox')).toHaveFocus();
1136
+
1137
+ await user.keyboard('{Enter}');
1138
+ expect(result.getByRole('searchbox')).toHaveFocus();
1139
+ expect(result.getByRole('listbox')).toBeInTheDocument();
1140
+
1141
+ await user.tab({ shift: true });
1142
+
1143
+ expect(result.queryByRole('listbox')).not.toBeInTheDocument();
1144
+ expect(result.getByRole('button', { name: 'Before' })).toHaveFocus();
1145
+ });
1146
+
1147
+ test('open filterable dropdown at end of tab order does not trap focus on Tab', async () => {
1148
+ const result = render(
1149
+ <ThemeContextProvider>
1150
+ <MultiSelect
1151
+ filterable
1152
+ options={defaultOptions}
1153
+ value={[]}
1154
+ onValueChange={() => {}}
1155
+ />
1156
+ </ThemeContextProvider>
1157
+ );
1158
+
1159
+ fireEvent.click(result.getByRole('combobox'));
1160
+ const searchbox = result.getByRole('searchbox');
1161
+ expect(result.getByRole('listbox')).toBeInTheDocument();
1162
+
1163
+ const tabEvent = createEvent.keyDown(searchbox, { key: 'Tab' });
1164
+ const dispatchResult = fireEvent(searchbox, tabEvent);
1165
+
1166
+ expect(dispatchResult).toBe(true);
1167
+ expect(result.queryByRole('listbox')).not.toBeInTheDocument();
1168
+ });
1169
+
1170
+ test('open filterable dropdown at start of tab order does not trap focus on shift-tab', async () => {
1171
+ const user = userEvent.setup();
1172
+ const result = render(
1173
+ <ThemeContextProvider>
1174
+ <MultiSelect
1175
+ filterable
1176
+ options={defaultOptions}
1177
+ value={[]}
1178
+ onValueChange={() => {}}
1179
+ />
1180
+ </ThemeContextProvider>
1181
+ );
1182
+
1183
+ await user.tab();
1184
+ expect(result.getByRole('combobox')).toHaveFocus();
1185
+
1186
+ await user.keyboard('{Enter}');
1187
+ const searchbox = result.getByRole('searchbox');
1188
+ expect(searchbox).toHaveFocus();
1189
+ expect(result.getByRole('listbox')).toBeInTheDocument();
1190
+
1191
+ const shiftTabEvent = createEvent.keyDown(searchbox, {
1192
+ key: 'Tab',
1193
+ shiftKey: true,
1194
+ });
1195
+ const dispatchResult = fireEvent(searchbox, shiftTabEvent);
1196
+
1197
+ expect(dispatchResult).toBe(true);
1198
+ expect(result.queryByRole('listbox')).not.toBeInTheDocument();
1199
+ });
1200
+
1201
+ test('clear button appears for clearable select and clears to null', async () => {
1202
+ const user = userEvent.setup();
1203
+ const changeSpy = vi.fn();
1204
+
1205
+ const ControlledSelect = () => {
1206
+ const [value, setValue] = useState<string[]>(['2']);
1207
+ return (
1208
+ <MultiSelect
1209
+ clearable
1210
+ options={defaultOptions}
1211
+ value={value}
1212
+ placeholder='MultiSelect an option'
1213
+ onValueChange={(next, ev) => {
1214
+ setValue(next);
1215
+ changeSpy(next, ev);
1216
+ }}
1217
+ />
1218
+ );
1219
+ };
1220
+
1221
+ const result = render(
1222
+ <ThemeContextProvider>
1223
+ <ControlledSelect />
1224
+ </ThemeContextProvider>
1225
+ );
1226
+
1227
+ const clearButton = result.getByTestId(
1228
+ 'ucl-uikit-multiselect__visible-field-clear-button'
1229
+ );
1230
+ expect(result.getByRole('combobox')).toHaveTextContent('Option 2');
1231
+
1232
+ await user.click(clearButton);
1233
+
1234
+ expect(changeSpy).toHaveBeenCalledWith([], expect.any(Object));
1235
+ expect(result.getByRole('combobox')).toHaveTextContent('Select an option');
1236
+ expect(
1237
+ result.queryByTestId('ucl-uikit-multiselect__visible-field-clear-button')
1238
+ ).not.toBeInTheDocument();
1239
+ });
1240
+
1241
+ test('aria-activedescendant is absent immediately after opening with a preselected value', async () => {
1242
+ const user = userEvent.setup();
1243
+ const result = render(
1244
+ <ThemeContextProvider>
1245
+ <MultiSelect
1246
+ options={defaultOptions}
1247
+ value={['2']}
1248
+ onValueChange={() => {}}
1249
+ />
1250
+ </ThemeContextProvider>
1251
+ );
1252
+
1253
+ const combobox = result.getByRole('combobox');
1254
+ await user.click(combobox);
1255
+
1256
+ expect(combobox).not.toHaveAttribute('aria-activedescendant');
1257
+
1258
+ await user.keyboard('{ArrowDown}');
1259
+ expect(combobox).toHaveAttribute('aria-activedescendant');
1260
+ });
1261
+
1262
+ test('filterable: aria-activedescendant is absent immediately after opening with a preselected value', async () => {
1263
+ const user = userEvent.setup();
1264
+ const result = render(
1265
+ <ThemeContextProvider>
1266
+ <MultiSelect
1267
+ filterable
1268
+ options={defaultOptions}
1269
+ value={['2']}
1270
+ onValueChange={() => {}}
1271
+ />
1272
+ </ThemeContextProvider>
1273
+ );
1274
+
1275
+ await user.click(result.getByRole('combobox'));
1276
+ const filterInput = result.getByRole('searchbox');
1277
+ expect(filterInput).not.toHaveAttribute('aria-activedescendant');
1278
+
1279
+ await user.keyboard('{ArrowDown}');
1280
+ expect(filterInput).toHaveAttribute('aria-activedescendant');
1281
+ });
1282
+
1283
+ test('disabled clearable select does not show clear button', () => {
1284
+ const result = render(
1285
+ <ThemeContextProvider>
1286
+ <MultiSelect
1287
+ clearable
1288
+ disabled
1289
+ options={defaultOptions}
1290
+ value={['1']}
1291
+ onValueChange={() => {}}
1292
+ />
1293
+ </ThemeContextProvider>
1294
+ );
1295
+
1296
+ expect(
1297
+ result.queryByTestId('ucl-uikit-multiselect__visible-field-clear-button')
1298
+ ).not.toBeInTheDocument();
1299
+ });
1300
+ });