uikit-react-public 0.51.0 → 0.52.1
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.
- package/dist/components/MultiSelect/MultiSelect.d.ts +4 -0
- package/dist/components/MultiSelect/MultiSelect.stories.d.ts +206 -0
- package/dist/components/MultiSelect/MultiSelect.types.d.ts +128 -0
- package/dist/components/MultiSelect/__tests__/MultiSelect.test.d.ts +1 -0
- package/dist/components/MultiSelect/index.d.ts +2 -0
- package/dist/components/MultiSelect/subcomponents/CustomOption.d.ts +3 -0
- package/dist/components/MultiSelect/subcomponents/CustomSelect.d.ts +4 -0
- package/dist/components/MultiSelect/subcomponents/FilterInput.d.ts +16 -0
- package/dist/components/MultiSelect/subcomponents/Panel.d.ts +8 -0
- package/dist/components/MultiSelect/subcomponents/VisibleField.d.ts +17 -0
- package/dist/components/MultiSelect/subcomponents/index.d.ts +5 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/index.js +1079 -525
- package/lib/components/Calendar/__tests__/__snapshots__/Calendar.test.tsx.snap +4 -4
- package/lib/components/MultiSelect/MultiSelect.mdx +151 -0
- package/lib/components/MultiSelect/MultiSelect.stories.tsx +308 -0
- package/lib/components/MultiSelect/MultiSelect.tsx +24 -0
- package/lib/components/MultiSelect/MultiSelect.types.ts +142 -0
- package/lib/components/MultiSelect/__tests__/MultiSelect.test.tsx +1300 -0
- package/lib/components/MultiSelect/__tests__/__snapshots__/MultiSelect.test.tsx.snap +44 -0
- package/lib/components/MultiSelect/index.ts +2 -0
- package/lib/components/MultiSelect/subcomponents/CustomOption.tsx +107 -0
- package/lib/components/MultiSelect/subcomponents/CustomSelect.tsx +701 -0
- package/lib/components/MultiSelect/subcomponents/FilterInput.tsx +90 -0
- package/lib/components/MultiSelect/subcomponents/Panel.tsx +69 -0
- package/lib/components/MultiSelect/subcomponents/VisibleField.tsx +147 -0
- package/lib/components/MultiSelect/subcomponents/index.tsx +5 -0
- package/lib/components/index.ts +3 -0
- package/lib/theme/Theme.mdx +2 -0
- package/lib/theme/light/lightColour.ts +3 -7
- package/lib/theme/original/originalColourNewStructure.ts +3 -7
- package/package.json +2 -2
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useState,
|
|
3
|
+
useRef,
|
|
4
|
+
useEffect,
|
|
5
|
+
useLayoutEffect,
|
|
6
|
+
useMemo,
|
|
7
|
+
useId,
|
|
8
|
+
useCallback,
|
|
9
|
+
} from 'react';
|
|
10
|
+
import { css, cx } from '@emotion/css';
|
|
11
|
+
import { VisibleField, Panel, CustomOption, FilterInput } from '.';
|
|
12
|
+
import { useTheme } from '../../../theme';
|
|
13
|
+
import Overlay from '../../Overlay';
|
|
14
|
+
import type { OverlaySize } from '../../Overlay';
|
|
15
|
+
import type { InternalMultiSelectProps } from '../MultiSelect.types';
|
|
16
|
+
|
|
17
|
+
const NAME = 'ucl-uikit-multiselect';
|
|
18
|
+
const FOCUSABLE_ELEMENTS = [
|
|
19
|
+
'a[href]',
|
|
20
|
+
'button:not([disabled])',
|
|
21
|
+
'textarea:not([disabled])',
|
|
22
|
+
'input:not([disabled])',
|
|
23
|
+
'select:not([disabled])',
|
|
24
|
+
'[tabindex]:not([tabindex="-1"])',
|
|
25
|
+
'[contenteditable="true"]',
|
|
26
|
+
'audio[controls]',
|
|
27
|
+
'video[controls]',
|
|
28
|
+
'details > summary:first-of-type',
|
|
29
|
+
].join(', ');
|
|
30
|
+
|
|
31
|
+
type CustomSelectProps<T> = InternalMultiSelectProps<T>;
|
|
32
|
+
|
|
33
|
+
const CustomSelect = <T extends string | number>({
|
|
34
|
+
value = [],
|
|
35
|
+
options = [],
|
|
36
|
+
onValueChange,
|
|
37
|
+
disabled,
|
|
38
|
+
width = 325,
|
|
39
|
+
clearable = false,
|
|
40
|
+
placeholder,
|
|
41
|
+
selectionCountLabel,
|
|
42
|
+
lineBreak = false,
|
|
43
|
+
dropdownWidth = 'content',
|
|
44
|
+
filterInputProps,
|
|
45
|
+
testId = NAME,
|
|
46
|
+
className,
|
|
47
|
+
panelClassName,
|
|
48
|
+
visibleFieldClassName,
|
|
49
|
+
optionClassName,
|
|
50
|
+
chevronIconClassName,
|
|
51
|
+
filterable = false,
|
|
52
|
+
ref,
|
|
53
|
+
...props
|
|
54
|
+
}: CustomSelectProps<T>) => {
|
|
55
|
+
const duplicateOptionValues = useMemo(() => {
|
|
56
|
+
const seen = new Set<T>();
|
|
57
|
+
const duplicates = new Set<T>();
|
|
58
|
+
|
|
59
|
+
for (const option of options) {
|
|
60
|
+
if (seen.has(option.value)) duplicates.add(option.value);
|
|
61
|
+
else seen.add(option.value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return Array.from(duplicates);
|
|
65
|
+
}, [options]);
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (duplicateOptionValues.length > 0) {
|
|
69
|
+
console.warn(
|
|
70
|
+
`Select options contain non-unique values: ${duplicateOptionValues
|
|
71
|
+
.map(String)
|
|
72
|
+
.join(', ')}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}, [duplicateOptionValues]);
|
|
76
|
+
|
|
77
|
+
const internalRef = useRef<HTMLDivElement | null>(null);
|
|
78
|
+
|
|
79
|
+
// Keep the anchor attached while the newly opened Overlay reads its ref.
|
|
80
|
+
const setRefs = useCallback(
|
|
81
|
+
(node: HTMLDivElement | null) => {
|
|
82
|
+
internalRef.current = node;
|
|
83
|
+
if (typeof ref === 'function') {
|
|
84
|
+
ref(node);
|
|
85
|
+
} else if (
|
|
86
|
+
ref &&
|
|
87
|
+
'current' in
|
|
88
|
+
(ref as React.RefObject<HTMLDivElement | HTMLSelectElement | null>)
|
|
89
|
+
) {
|
|
90
|
+
(
|
|
91
|
+
ref as React.RefObject<HTMLDivElement | HTMLSelectElement | null>
|
|
92
|
+
).current = node;
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
[ref]
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const [theme] = useTheme();
|
|
99
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
100
|
+
const [filterText, setFilterText] = useState('');
|
|
101
|
+
const [activeOptionIndex, setActiveOptionIndex] = useState<number | null>(
|
|
102
|
+
null
|
|
103
|
+
);
|
|
104
|
+
const activeOptionIndexRef = useRef<number | null>(null);
|
|
105
|
+
const [panelMaxWidth, setPanelMaxWidth] = useState<number | null>(null);
|
|
106
|
+
const [panelReferenceWidth, setPanelReferenceWidth] = useState<number | null>(
|
|
107
|
+
null
|
|
108
|
+
);
|
|
109
|
+
const [overlaySize, setOverlaySize] = useState<OverlaySize | null>(null);
|
|
110
|
+
// Message announced via an aria-live region when a selection is committed,
|
|
111
|
+
// so screen reader users hear confirmation even after the panel/filter closes.
|
|
112
|
+
const [selectionAnnouncement, setSelectionAnnouncement] = useState('');
|
|
113
|
+
const filterInputRef = useRef<HTMLInputElement | null>(null);
|
|
114
|
+
const clearButtonRef = useRef<HTMLButtonElement | null>(null);
|
|
115
|
+
const reactId = useId();
|
|
116
|
+
const wasOpenRef = useRef(false);
|
|
117
|
+
const idBase = props.id ?? `${testId}-${reactId.replace(/[:]/g, '')}`;
|
|
118
|
+
const listboxId = `${idBase}-listbox`;
|
|
119
|
+
const selectionStatusId = `${idBase}-selection-status`;
|
|
120
|
+
const overlayViewportPadding = parseFloat(String(theme.margin.m32));
|
|
121
|
+
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
activeOptionIndexRef.current = activeOptionIndex;
|
|
124
|
+
}, [activeOptionIndex]);
|
|
125
|
+
|
|
126
|
+
// Returns a list of indexes of options that are currently visible based on the filter text
|
|
127
|
+
const visibleOptionIndexes = useMemo(() => {
|
|
128
|
+
const normalizedFilterText = filterText.toLowerCase();
|
|
129
|
+
return filterable
|
|
130
|
+
? options.reduce<number[]>((visibleIndexes, option, index) => {
|
|
131
|
+
if (option.label.toLowerCase().includes(normalizedFilterText)) {
|
|
132
|
+
visibleIndexes.push(index);
|
|
133
|
+
}
|
|
134
|
+
return visibleIndexes;
|
|
135
|
+
}, [])
|
|
136
|
+
: options.map((_, index) => index);
|
|
137
|
+
}, [filterable, options, filterText]);
|
|
138
|
+
const visibleOptions = useMemo(
|
|
139
|
+
() => visibleOptionIndexes.map((index) => options[index]),
|
|
140
|
+
[visibleOptionIndexes, options]
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
if (!isOpen && filterText) setFilterText('');
|
|
145
|
+
}, [isOpen, filterText]);
|
|
146
|
+
|
|
147
|
+
// Reset the live-region text when the panel opens so committing the same
|
|
148
|
+
// option again produces a changed value that screen readers will re-announce.
|
|
149
|
+
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
if (isOpen && !wasOpenRef.current) setSelectionAnnouncement('');
|
|
152
|
+
wasOpenRef.current = isOpen;
|
|
153
|
+
}, [isOpen]);
|
|
154
|
+
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
const handleClickOutside = (event: MouseEvent) => {
|
|
157
|
+
if (
|
|
158
|
+
internalRef.current &&
|
|
159
|
+
!internalRef.current.contains(event.target as Node)
|
|
160
|
+
) {
|
|
161
|
+
setIsOpen(false);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
document.addEventListener('mousedown', handleClickOutside);
|
|
166
|
+
return () => {
|
|
167
|
+
document.removeEventListener('mousedown', handleClickOutside);
|
|
168
|
+
};
|
|
169
|
+
}, [internalRef]);
|
|
170
|
+
|
|
171
|
+
const handleBlur = (event: React.FocusEvent<HTMLElement>) => {
|
|
172
|
+
const nextFocusedElement = event.relatedTarget as Node | null;
|
|
173
|
+
if (
|
|
174
|
+
internalRef.current &&
|
|
175
|
+
nextFocusedElement &&
|
|
176
|
+
internalRef.current.contains(nextFocusedElement)
|
|
177
|
+
) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
closePanel();
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const isFocusableElement = (element: HTMLElement) => {
|
|
184
|
+
if (!element.isConnected) return false;
|
|
185
|
+
if (element instanceof HTMLInputElement && element.type === 'hidden') {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
if (element.tabIndex < 0) return false;
|
|
189
|
+
if ('disabled' in element && element.disabled) return false;
|
|
190
|
+
|
|
191
|
+
const style = window.getComputedStyle(element);
|
|
192
|
+
if (style.display === 'none' || style.visibility === 'hidden') {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return true;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const moveFocusOutsideSelect = (isReverse: boolean) => {
|
|
200
|
+
if (!internalRef.current) return false;
|
|
201
|
+
|
|
202
|
+
const focusableElements = Array.from(
|
|
203
|
+
document.querySelectorAll<HTMLElement>(FOCUSABLE_ELEMENTS)
|
|
204
|
+
).filter(isFocusableElement);
|
|
205
|
+
|
|
206
|
+
const currentElement = filterInputRef.current;
|
|
207
|
+
if (!currentElement) return false;
|
|
208
|
+
|
|
209
|
+
const currentIndex = focusableElements.indexOf(currentElement);
|
|
210
|
+
if (currentIndex === -1) return false;
|
|
211
|
+
|
|
212
|
+
const step = isReverse ? -1 : 1;
|
|
213
|
+
let nextIndex = currentIndex + step;
|
|
214
|
+
|
|
215
|
+
while (nextIndex >= 0 && nextIndex < focusableElements.length) {
|
|
216
|
+
const candidate = focusableElements[nextIndex];
|
|
217
|
+
if (!internalRef.current.contains(candidate)) {
|
|
218
|
+
candidate.focus();
|
|
219
|
+
if (document.activeElement === candidate) {
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
nextIndex += step;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return false;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const handleFilterTabOut = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
230
|
+
closePanel();
|
|
231
|
+
// If this widget gains more tabbable descendants, revisit this handoff.
|
|
232
|
+
if (!event.shiftKey && clearButtonRef.current) {
|
|
233
|
+
clearButtonRef.current.focus();
|
|
234
|
+
if (document.activeElement === clearButtonRef.current) {
|
|
235
|
+
event.preventDefault();
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (moveFocusOutsideSelect(event.shiftKey)) {
|
|
240
|
+
event.preventDefault();
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
useEffect(() => {
|
|
245
|
+
// Close the dropdown if it becomes disabled
|
|
246
|
+
if (disabled && isOpen) setIsOpen(false);
|
|
247
|
+
}, [disabled, isOpen]);
|
|
248
|
+
|
|
249
|
+
useEffect(() => {
|
|
250
|
+
if (filterable && isOpen && filterInputRef.current) {
|
|
251
|
+
filterInputRef.current.focus();
|
|
252
|
+
}
|
|
253
|
+
}, [filterable, isOpen]);
|
|
254
|
+
|
|
255
|
+
useLayoutEffect(() => {
|
|
256
|
+
if (!isOpen) {
|
|
257
|
+
setPanelMaxWidth(null);
|
|
258
|
+
setPanelReferenceWidth(null);
|
|
259
|
+
setOverlaySize(null);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const updatePanelMaxWidth = () => {
|
|
264
|
+
const referenceElement = internalRef.current;
|
|
265
|
+
const dialogElement = referenceElement?.closest('dialog');
|
|
266
|
+
const dialogBodyElement =
|
|
267
|
+
dialogElement?.querySelector<HTMLElement>('[role="document"]');
|
|
268
|
+
const dialogBodyRect = dialogBodyElement?.getBoundingClientRect();
|
|
269
|
+
const referenceRect = referenceElement?.getBoundingClientRect();
|
|
270
|
+
|
|
271
|
+
setPanelReferenceWidth(referenceRect?.width ?? null);
|
|
272
|
+
setPanelMaxWidth(
|
|
273
|
+
dialogBodyRect && referenceRect
|
|
274
|
+
? Math.max(0, dialogBodyRect.right - referenceRect.left)
|
|
275
|
+
: null
|
|
276
|
+
);
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
updatePanelMaxWidth();
|
|
280
|
+
window.addEventListener('resize', updatePanelMaxWidth);
|
|
281
|
+
return () => window.removeEventListener('resize', updatePanelMaxWidth);
|
|
282
|
+
}, [internalRef, isOpen]);
|
|
283
|
+
|
|
284
|
+
const togglePanel = () => {
|
|
285
|
+
if (!disabled) setIsOpen((prev) => !prev);
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const openPanel = () => {
|
|
289
|
+
if (!disabled) setIsOpen(true);
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const closePanel = () => {
|
|
293
|
+
if (!disabled) {
|
|
294
|
+
setIsOpen(false);
|
|
295
|
+
setActiveOptionIndex(null);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const handleOverlaySizeChange = (nextSize: OverlaySize) => {
|
|
300
|
+
setOverlaySize((prevSize) => {
|
|
301
|
+
if (
|
|
302
|
+
prevSize &&
|
|
303
|
+
prevSize.referenceWidth === nextSize.referenceWidth &&
|
|
304
|
+
prevSize.availableWidth === nextSize.availableWidth &&
|
|
305
|
+
prevSize.availableHeight === nextSize.availableHeight
|
|
306
|
+
) {
|
|
307
|
+
return prevSize;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return nextSize;
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const handleClick = (event: React.MouseEvent) => {
|
|
315
|
+
if (disabled) return;
|
|
316
|
+
if (!isOpen) {
|
|
317
|
+
openPanel();
|
|
318
|
+
if (filterable && filterInputRef.current) {
|
|
319
|
+
filterInputRef.current.focus();
|
|
320
|
+
}
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
// If filter is enabled and the click was on the input, keep it open
|
|
324
|
+
if (
|
|
325
|
+
filterable &&
|
|
326
|
+
filterInputRef.current &&
|
|
327
|
+
filterInputRef.current.contains(event.target as Node)
|
|
328
|
+
) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
closePanel();
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const handleClear = (event: React.MouseEvent | React.KeyboardEvent) => {
|
|
335
|
+
if (disabled) return;
|
|
336
|
+
event.stopPropagation();
|
|
337
|
+
// setSelectedOptionIndex(null);
|
|
338
|
+
setActiveOptionIndex(null);
|
|
339
|
+
setFilterText('');
|
|
340
|
+
closePanel();
|
|
341
|
+
internalRef.current?.focus();
|
|
342
|
+
onValueChange?.([], event);
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const toggleValue = (optionValue: T) =>
|
|
346
|
+
value.includes(optionValue)
|
|
347
|
+
? value.filter((v) => v !== optionValue)
|
|
348
|
+
: [...value, optionValue];
|
|
349
|
+
|
|
350
|
+
const handleSelect = (event: React.MouseEvent, optionValue: T) => {
|
|
351
|
+
onValueChange?.(toggleValue(optionValue), event);
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
// Get the currently selected option object from its index among the visible options
|
|
355
|
+
const selectedOptions = options.filter((opt) => value.includes(opt.value));
|
|
356
|
+
|
|
357
|
+
// Ensure the active option index is within bounds of the visible options
|
|
358
|
+
const effectiveActiveOptionIndex =
|
|
359
|
+
activeOptionIndex !== null &&
|
|
360
|
+
activeOptionIndex >= 0 &&
|
|
361
|
+
activeOptionIndex < visibleOptions.length
|
|
362
|
+
? activeOptionIndex
|
|
363
|
+
: null;
|
|
364
|
+
// Get the index of the currently highlighted option among the visible options, or null if none is highlighted
|
|
365
|
+
const highlightedVisibleIndex = effectiveActiveOptionIndex;
|
|
366
|
+
const activeDescendantId =
|
|
367
|
+
isOpen &&
|
|
368
|
+
effectiveActiveOptionIndex !== null &&
|
|
369
|
+
highlightedVisibleIndex !== null
|
|
370
|
+
? `${idBase}-option-${visibleOptionIndexes[highlightedVisibleIndex]}`
|
|
371
|
+
: undefined;
|
|
372
|
+
|
|
373
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
374
|
+
// Prevent scrolling the page when the select is open
|
|
375
|
+
if (
|
|
376
|
+
event.key === 'ArrowUp' ||
|
|
377
|
+
event.key === 'ArrowDown' ||
|
|
378
|
+
event.key === 'ArrowLeft' ||
|
|
379
|
+
event.key === 'ArrowRight'
|
|
380
|
+
)
|
|
381
|
+
event.preventDefault();
|
|
382
|
+
|
|
383
|
+
const getCurrentOptionIndex = () =>
|
|
384
|
+
activeOptionIndex !== null &&
|
|
385
|
+
activeOptionIndex >= 0 &&
|
|
386
|
+
activeOptionIndex < visibleOptions.length
|
|
387
|
+
? activeOptionIndex
|
|
388
|
+
: null;
|
|
389
|
+
|
|
390
|
+
if (disabled) return;
|
|
391
|
+
|
|
392
|
+
const isAltGraph = event.getModifierState('AltGraph');
|
|
393
|
+
const isPrintableCharacter =
|
|
394
|
+
event.key.length === 1 &&
|
|
395
|
+
event.key !== ' ' &&
|
|
396
|
+
!event.metaKey &&
|
|
397
|
+
((!event.altKey && !event.ctrlKey) || isAltGraph);
|
|
398
|
+
|
|
399
|
+
if (filterable && !isOpen && isPrintableCharacter) {
|
|
400
|
+
event.preventDefault();
|
|
401
|
+
setFilterText(event.key);
|
|
402
|
+
openPanel();
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (event.key === 'Enter') {
|
|
407
|
+
if (!isOpen) {
|
|
408
|
+
openPanel();
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (visibleOptions.length > 0) {
|
|
413
|
+
// Prefer the keyboard-highlighted option; otherwise, when filtering,
|
|
414
|
+
// commit the top (first) visible option.
|
|
415
|
+
const currentOptionIndex = getCurrentOptionIndex();
|
|
416
|
+
const targetIndex =
|
|
417
|
+
currentOptionIndex !== null ? currentOptionIndex : 0;
|
|
418
|
+
|
|
419
|
+
const committedOption = visibleOptions[targetIndex];
|
|
420
|
+
onValueChange?.(toggleValue(committedOption.value), event);
|
|
421
|
+
|
|
422
|
+
setSelectionAnnouncement(
|
|
423
|
+
`${committedOption.label} ${
|
|
424
|
+
value.includes(committedOption.value) ? 'deselected' : 'selected'
|
|
425
|
+
}`
|
|
426
|
+
);
|
|
427
|
+
setFilterText('');
|
|
428
|
+
event.preventDefault();
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
event.preventDefault();
|
|
432
|
+
togglePanel();
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (!isOpen && event.code === 'Space') {
|
|
436
|
+
event.preventDefault();
|
|
437
|
+
openPanel();
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (isOpen && event.key === 'Escape') {
|
|
441
|
+
closePanel();
|
|
442
|
+
event.currentTarget.focus();
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
// Select the previous option without deleting the current selection
|
|
446
|
+
if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
|
|
447
|
+
if (!isOpen) {
|
|
448
|
+
openPanel();
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (visibleOptions.length === 0) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const currentOptionIndex = getCurrentOptionIndex();
|
|
455
|
+
const previousOptionIndex =
|
|
456
|
+
currentOptionIndex === null
|
|
457
|
+
? visibleOptions.length - 1
|
|
458
|
+
: (currentOptionIndex - 1 + visibleOptions.length) %
|
|
459
|
+
visibleOptions.length;
|
|
460
|
+
setActiveOptionIndex(previousOptionIndex);
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// Select the next option without deleting the current selection
|
|
465
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
|
|
466
|
+
if (!isOpen) {
|
|
467
|
+
openPanel();
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const currentOptionIndex = getCurrentOptionIndex();
|
|
471
|
+
const nextOptionIndex =
|
|
472
|
+
currentOptionIndex === null
|
|
473
|
+
? 0
|
|
474
|
+
: (currentOptionIndex + 1) % visibleOptions.length;
|
|
475
|
+
setActiveOptionIndex(nextOptionIndex);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
const { md } = theme.typography.body;
|
|
481
|
+
|
|
482
|
+
const baseStyle = css`
|
|
483
|
+
display: inline-flex;
|
|
484
|
+
align-items: center;
|
|
485
|
+
justify-content: space-between;
|
|
486
|
+
position: relative;
|
|
487
|
+
min-width: 80px;
|
|
488
|
+
max-width: calc(100vw - 64px);
|
|
489
|
+
height: 48px;
|
|
490
|
+
box-sizing: border-box;
|
|
491
|
+
padding: 0 16px;
|
|
492
|
+
background-color: ${theme.colour.fill.inverse};
|
|
493
|
+
color: ${theme.colour.text.default};
|
|
494
|
+
font-family: ${md.fontFamily};
|
|
495
|
+
font-feature-settings: ${md.fontSettings};
|
|
496
|
+
font-size: ${md.fontSize}px;
|
|
497
|
+
font-weight: ${md.fontWeight};
|
|
498
|
+
line-height: ${md.lineHeight}%;
|
|
499
|
+
border: ${theme.border.b1} solid ${theme.colour.speciality.inputDefault};
|
|
500
|
+
border-radius: ${theme.radius.r4};
|
|
501
|
+
cursor: pointer;
|
|
502
|
+
user-select: none;
|
|
503
|
+
|
|
504
|
+
&:focus-visible,
|
|
505
|
+
&:focus-within {
|
|
506
|
+
outline: none;
|
|
507
|
+
box-shadow: ${theme.boxShadow.focus};
|
|
508
|
+
}
|
|
509
|
+
`;
|
|
510
|
+
|
|
511
|
+
const widthStyle = css`
|
|
512
|
+
width: ${width}px;
|
|
513
|
+
`;
|
|
514
|
+
|
|
515
|
+
const disabledStyle = css`
|
|
516
|
+
color: ${theme.colour.text.disabled};
|
|
517
|
+
border: ${theme.border.b1} solid ${theme.colour.border.disabled};
|
|
518
|
+
cursor: not-allowed;
|
|
519
|
+
|
|
520
|
+
&:hover {
|
|
521
|
+
background-color: ${theme.colour.fill.inverse};
|
|
522
|
+
}
|
|
523
|
+
`;
|
|
524
|
+
|
|
525
|
+
const noOptionsStyle = css`
|
|
526
|
+
padding: ${theme.padding.p8} ${theme.padding.p16};
|
|
527
|
+
color: ${theme.colour.text.secondary};
|
|
528
|
+
`;
|
|
529
|
+
|
|
530
|
+
// Visually hidden but available to assistive technology
|
|
531
|
+
const visuallyHiddenStyle = css`
|
|
532
|
+
position: absolute;
|
|
533
|
+
width: 1px;
|
|
534
|
+
height: 1px;
|
|
535
|
+
padding: 0;
|
|
536
|
+
margin: -1px;
|
|
537
|
+
overflow: hidden;
|
|
538
|
+
clip: rect(0, 0, 0, 0);
|
|
539
|
+
white-space: nowrap;
|
|
540
|
+
border: 0;
|
|
541
|
+
`;
|
|
542
|
+
|
|
543
|
+
const style = cx(
|
|
544
|
+
NAME,
|
|
545
|
+
baseStyle,
|
|
546
|
+
widthStyle,
|
|
547
|
+
disabled && disabledStyle,
|
|
548
|
+
className
|
|
549
|
+
);
|
|
550
|
+
|
|
551
|
+
// Forward the combobox's accessible name/description onto the filter input so
|
|
552
|
+
// assistive tech announces the field label (and current selection) while filtering.
|
|
553
|
+
const filterInputA11yProps = {
|
|
554
|
+
...(props['aria-label'] !== undefined
|
|
555
|
+
? { 'aria-label': props['aria-label'] }
|
|
556
|
+
: {}),
|
|
557
|
+
...(props['aria-labelledby'] !== undefined
|
|
558
|
+
? { 'aria-labelledby': props['aria-labelledby'] }
|
|
559
|
+
: {}),
|
|
560
|
+
'aria-describedby':
|
|
561
|
+
[
|
|
562
|
+
props['aria-describedby'],
|
|
563
|
+
selectedOptions.length > 0 ? selectionStatusId : undefined,
|
|
564
|
+
]
|
|
565
|
+
.filter(Boolean)
|
|
566
|
+
.join(' ') || undefined,
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
return (
|
|
570
|
+
<div
|
|
571
|
+
onClick={handleClick}
|
|
572
|
+
onKeyDown={handleKeyDown}
|
|
573
|
+
onBlur={handleBlur}
|
|
574
|
+
tabIndex={disabled || (filterable && isOpen) ? -1 : 0}
|
|
575
|
+
className={style}
|
|
576
|
+
data-testid={testId}
|
|
577
|
+
ref={setRefs}
|
|
578
|
+
role='combobox'
|
|
579
|
+
aria-haspopup='listbox'
|
|
580
|
+
aria-expanded={isOpen}
|
|
581
|
+
aria-disabled={disabled || undefined}
|
|
582
|
+
aria-controls={isOpen ? listboxId : undefined}
|
|
583
|
+
aria-activedescendant={activeDescendantId}
|
|
584
|
+
{...props}
|
|
585
|
+
>
|
|
586
|
+
<VisibleField
|
|
587
|
+
isOpen={isOpen}
|
|
588
|
+
selectedOptions={selectedOptions}
|
|
589
|
+
placeholder={placeholder}
|
|
590
|
+
selectionCountLabel={selectionCountLabel}
|
|
591
|
+
disabled={disabled}
|
|
592
|
+
filterable={filterable}
|
|
593
|
+
clearable={clearable}
|
|
594
|
+
onClear={handleClear}
|
|
595
|
+
clearButtonRef={clearButtonRef}
|
|
596
|
+
className={visibleFieldClassName}
|
|
597
|
+
chevronIconClassName={chevronIconClassName}
|
|
598
|
+
>
|
|
599
|
+
{filterable && (
|
|
600
|
+
<FilterInput
|
|
601
|
+
value={filterText}
|
|
602
|
+
onChange={setFilterText}
|
|
603
|
+
placeholder={placeholder}
|
|
604
|
+
disabled={disabled}
|
|
605
|
+
inputRef={filterInputRef}
|
|
606
|
+
onBlur={handleBlur}
|
|
607
|
+
onTabOut={handleFilterTabOut}
|
|
608
|
+
ariaControls={listboxId}
|
|
609
|
+
ariaExpanded={isOpen}
|
|
610
|
+
ariaActiveDescendant={activeDescendantId}
|
|
611
|
+
{...filterInputA11yProps}
|
|
612
|
+
{...filterInputProps}
|
|
613
|
+
/>
|
|
614
|
+
)}
|
|
615
|
+
</VisibleField>
|
|
616
|
+
{filterable && isOpen && selectedOptions.length > 0 && (
|
|
617
|
+
<span
|
|
618
|
+
id={selectionStatusId}
|
|
619
|
+
className={visuallyHiddenStyle}
|
|
620
|
+
>
|
|
621
|
+
{/*{`${selectedOptions.length} selected`}*/}
|
|
622
|
+
{`${selectedOptions.length}${selectionCountLabel ? ` ${selectionCountLabel}` : ''} selected`}
|
|
623
|
+
</span>
|
|
624
|
+
)}
|
|
625
|
+
<span
|
|
626
|
+
role='status'
|
|
627
|
+
aria-live='polite'
|
|
628
|
+
className={visuallyHiddenStyle}
|
|
629
|
+
>
|
|
630
|
+
{selectionAnnouncement}
|
|
631
|
+
</span>
|
|
632
|
+
{isOpen && (
|
|
633
|
+
<Overlay
|
|
634
|
+
reference={internalRef}
|
|
635
|
+
placement='bottom-start'
|
|
636
|
+
strategy='fixed'
|
|
637
|
+
offset={8}
|
|
638
|
+
flip={{ padding: overlayViewportPadding }}
|
|
639
|
+
shift={({ rects }) => ({
|
|
640
|
+
padding: overlayViewportPadding,
|
|
641
|
+
mainAxis:
|
|
642
|
+
dropdownWidth === 'content' &&
|
|
643
|
+
rects.floating.width > rects.reference.width,
|
|
644
|
+
crossAxis: false,
|
|
645
|
+
})}
|
|
646
|
+
size={{
|
|
647
|
+
matchReferenceWidth: true,
|
|
648
|
+
padding: overlayViewportPadding,
|
|
649
|
+
}}
|
|
650
|
+
onSizeChange={handleOverlaySizeChange}
|
|
651
|
+
>
|
|
652
|
+
<Panel
|
|
653
|
+
className={panelClassName}
|
|
654
|
+
dropdownWidth={dropdownWidth}
|
|
655
|
+
referenceWidth={
|
|
656
|
+
overlaySize?.referenceWidth ?? panelReferenceWidth ?? undefined
|
|
657
|
+
}
|
|
658
|
+
maxWidth={panelMaxWidth ?? undefined}
|
|
659
|
+
aria-multiselectable='true'
|
|
660
|
+
id={listboxId}
|
|
661
|
+
role='listbox'
|
|
662
|
+
>
|
|
663
|
+
{visibleOptions.map((option, index) => {
|
|
664
|
+
const { className: customOptionClassName, ...customOptionProps } =
|
|
665
|
+
option.optionProps ?? {};
|
|
666
|
+
const customOptionClass =
|
|
667
|
+
typeof customOptionClassName === 'string'
|
|
668
|
+
? customOptionClassName
|
|
669
|
+
: undefined;
|
|
670
|
+
|
|
671
|
+
return (
|
|
672
|
+
<CustomOption<T>
|
|
673
|
+
key={`${String(option.value)}-${visibleOptionIndexes[index]}`}
|
|
674
|
+
id={`${idBase}-option-${visibleOptionIndexes[index]}`}
|
|
675
|
+
value={option.value}
|
|
676
|
+
isSelected={value.includes(option.value)}
|
|
677
|
+
isActive={highlightedVisibleIndex === index}
|
|
678
|
+
onSelect={handleSelect}
|
|
679
|
+
lineBreak={lineBreak}
|
|
680
|
+
role='option'
|
|
681
|
+
aria-selected={value.includes(option.value)}
|
|
682
|
+
aria-posinset={index + 1}
|
|
683
|
+
aria-setsize={visibleOptions.length}
|
|
684
|
+
{...customOptionProps}
|
|
685
|
+
className={cx(optionClassName, customOptionClass)}
|
|
686
|
+
>
|
|
687
|
+
{option.label}
|
|
688
|
+
</CustomOption>
|
|
689
|
+
);
|
|
690
|
+
})}
|
|
691
|
+
{visibleOptions.length === 0 && (
|
|
692
|
+
<div className={noOptionsStyle}>No options</div>
|
|
693
|
+
)}
|
|
694
|
+
</Panel>
|
|
695
|
+
</Overlay>
|
|
696
|
+
)}
|
|
697
|
+
</div>
|
|
698
|
+
);
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
export default CustomSelect;
|