@ultraviolet/ui 1.47.0 → 1.48.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.
- package/dist/index.d.ts +127 -3
- package/dist/src/components/Checkbox/index.js +4 -2
- package/dist/src/components/MenuV2/index.js +2 -2
- package/dist/src/components/SelectInput/index.js +1 -0
- package/dist/src/components/SelectInputV2/Dropdown.js +595 -0
- package/dist/src/components/SelectInputV2/DropdownOption.js +135 -0
- package/dist/src/components/SelectInputV2/SearchBarDropdown.js +119 -0
- package/dist/src/components/SelectInputV2/SelectBar.js +269 -0
- package/dist/src/components/SelectInputV2/SelectInputProvider.js +153 -0
- package/dist/src/components/SelectInputV2/index.js +138 -0
- package/dist/src/components/SelectInputV2/types.js +11 -0
- package/dist/src/components/TextInputV2/index.js +4 -2
- package/dist/src/index.js +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import _styled from '@emotion/styled/base';
|
|
2
|
+
import { useState, useRef, useEffect, useMemo } from 'react';
|
|
3
|
+
import { Checkbox } from '../Checkbox/index.js';
|
|
4
|
+
import { Popup } from '../Popup/index.js';
|
|
5
|
+
import { Skeleton } from '../Skeleton/index.js';
|
|
6
|
+
import { Stack } from '../Stack/index.js';
|
|
7
|
+
import { Text } from '../Text/index.js';
|
|
8
|
+
import { DisplayOption } from './DropdownOption.js';
|
|
9
|
+
import { SearchBarDropdown } from './SearchBarDropdown.js';
|
|
10
|
+
import { useSelectInput } from './SelectInputProvider.js';
|
|
11
|
+
import { jsx, jsxs, Fragment } from '@emotion/react/jsx-runtime';
|
|
12
|
+
|
|
13
|
+
function _EMOTION_STRINGIFIED_CSS_ERROR__() { return "You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop)."; }
|
|
14
|
+
const NON_SEARCHABLE_KEYS = ['Tab', ' ', 'Enter', 'CapsLock', 'Shift', 'ArrowDown', 'ArrowUp', 'ArrowLeft', 'ArrowRight', 'Escape'];
|
|
15
|
+
const StyledPopup = /*#__PURE__*/_styled(Popup, {
|
|
16
|
+
target: "eeucz6c8"
|
|
17
|
+
})("width:100%;background-color:", ({
|
|
18
|
+
theme
|
|
19
|
+
}) => theme.colors.neutral.background, ";color:", ({
|
|
20
|
+
theme
|
|
21
|
+
}) => theme.colors.neutral.text, ";box-shadow:", ({
|
|
22
|
+
theme
|
|
23
|
+
}) => theme.shadows.dropdown, ";padding:", ({
|
|
24
|
+
theme
|
|
25
|
+
}) => theme.space[0], ";");
|
|
26
|
+
const DropdownContainer = /*#__PURE__*/_styled(Stack, {
|
|
27
|
+
target: "eeucz6c7"
|
|
28
|
+
})("max-height:256px;overflow-y:scroll;padding:", ({
|
|
29
|
+
theme
|
|
30
|
+
}) => theme.space[0], ";padding-bottom:", ({
|
|
31
|
+
theme
|
|
32
|
+
}) => theme.space[0.5], ";padding-top:", ({
|
|
33
|
+
theme
|
|
34
|
+
}) => theme.space[0.5], ";&[data-grouped='true']{padding-top:", ({
|
|
35
|
+
theme
|
|
36
|
+
}) => theme.space[0], ";}");
|
|
37
|
+
const DropdownGroup = /*#__PURE__*/_styled("button", {
|
|
38
|
+
target: "eeucz6c6"
|
|
39
|
+
})("display:flex;width:100%;justify-content:left;align-items:center;border:none;background-color:", ({
|
|
40
|
+
theme
|
|
41
|
+
}) => theme.colors.neutral.backgroundWeak, ";position:sticky;top:0px;padding-right:", ({
|
|
42
|
+
theme
|
|
43
|
+
}) => theme.space[2], ";padding-left:", ({
|
|
44
|
+
theme
|
|
45
|
+
}) => theme.space[2], ";height:", ({
|
|
46
|
+
theme
|
|
47
|
+
}) => theme.space[4], ";text-align:left;margin-bottom:", ({
|
|
48
|
+
theme
|
|
49
|
+
}) => theme.space['0.25'], ";&:focus{background-color:", ({
|
|
50
|
+
theme
|
|
51
|
+
}) => theme.colors.neutral.backgroundWeak, ";outline:none;}&[data-selectgroup='true']{padding-left:", ({
|
|
52
|
+
theme
|
|
53
|
+
}) => theme.space[2], ";border-left:", ({
|
|
54
|
+
theme
|
|
55
|
+
}) => theme.space[0.5], " solid ", ({
|
|
56
|
+
theme
|
|
57
|
+
}) => theme.colors.neutral.backgroundWeak, ";}&[data-selectgroup='true']:focus{background-color:", ({
|
|
58
|
+
theme
|
|
59
|
+
}) => theme.colors.neutral.backgroundHover, ";}");
|
|
60
|
+
const DropdownGroupWrapper = /*#__PURE__*/_styled("div", {
|
|
61
|
+
target: "eeucz6c5"
|
|
62
|
+
})(process.env.NODE_ENV === "production" ? {
|
|
63
|
+
name: "vkf8h3",
|
|
64
|
+
styles: "position:sticky;top:0px"
|
|
65
|
+
} : {
|
|
66
|
+
name: "vkf8h3",
|
|
67
|
+
styles: "position:sticky;top:0px",
|
|
68
|
+
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
|
|
69
|
+
});
|
|
70
|
+
const DropdownItem = /*#__PURE__*/_styled("button", {
|
|
71
|
+
target: "eeucz6c4"
|
|
72
|
+
})("text-align:left;border:none;background-color:", ({
|
|
73
|
+
theme
|
|
74
|
+
}) => theme.colors.neutral.background, ";padding:", ({
|
|
75
|
+
theme
|
|
76
|
+
}) => theme.space['1.5'], " ", ({
|
|
77
|
+
theme
|
|
78
|
+
}) => theme.space['2'], " ", ({
|
|
79
|
+
theme
|
|
80
|
+
}) => theme.space['1.5'], " ", ({
|
|
81
|
+
theme
|
|
82
|
+
}) => theme.space['2'], ";margin-left:", ({
|
|
83
|
+
theme
|
|
84
|
+
}) => theme.space['0.5'], ";margin-right:", ({
|
|
85
|
+
theme
|
|
86
|
+
}) => theme.space['0.5'], ";color:", ({
|
|
87
|
+
theme
|
|
88
|
+
}) => theme.colors.neutral.text, ";border-radius:", ({
|
|
89
|
+
theme
|
|
90
|
+
}) => theme.radii.default, ";&:hover,:focus{background-color:", ({
|
|
91
|
+
theme
|
|
92
|
+
}) => theme.colors.primary.background, ";color:", ({
|
|
93
|
+
theme
|
|
94
|
+
}) => theme.colors.primary.text, ";cursor:'pointer';outline:none;}&[data-selected='true']{background-color:", ({
|
|
95
|
+
theme
|
|
96
|
+
}) => theme.colors.primary.background, ";}&[disabled]{background-color:", ({
|
|
97
|
+
theme
|
|
98
|
+
}) => theme.colors.neutral.backgroundDisabled, ";color:", ({
|
|
99
|
+
theme
|
|
100
|
+
}) => theme.colors.neutral.textDisabled, ";}&[disabled]:hover,[disabled]:focus{background-color:", ({
|
|
101
|
+
theme
|
|
102
|
+
}) => theme.colors.neutral.backgroundStrongDisabled, ";color:", ({
|
|
103
|
+
theme
|
|
104
|
+
}) => theme.colors.neutral.textStrongDisabled, ";cursor:not-allowed;outline:none;}");
|
|
105
|
+
const PopupFooter = /*#__PURE__*/_styled("div", {
|
|
106
|
+
target: "eeucz6c3"
|
|
107
|
+
})("width:100%;padding:", ({
|
|
108
|
+
theme
|
|
109
|
+
}) => theme.space[1.5], " ", ({
|
|
110
|
+
theme
|
|
111
|
+
}) => theme.space[2], " ", ({
|
|
112
|
+
theme
|
|
113
|
+
}) => theme.space[1.5], " ", ({
|
|
114
|
+
theme
|
|
115
|
+
}) => theme.space[2], ";box-shadow:", ({
|
|
116
|
+
theme
|
|
117
|
+
}) => theme.shadows.dropdown, ";");
|
|
118
|
+
const StyledCheckbox = /*#__PURE__*/_styled(Checkbox, {
|
|
119
|
+
target: "eeucz6c2"
|
|
120
|
+
})(process.env.NODE_ENV === "production" ? {
|
|
121
|
+
name: "10y07ze",
|
|
122
|
+
styles: "width:100%;position:static;text-align:left;align-items:center"
|
|
123
|
+
} : {
|
|
124
|
+
name: "10y07ze",
|
|
125
|
+
styles: "width:100%;position:static;text-align:left;align-items:center",
|
|
126
|
+
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
|
|
127
|
+
});
|
|
128
|
+
const EmptyState = /*#__PURE__*/_styled(Stack, {
|
|
129
|
+
target: "eeucz6c1"
|
|
130
|
+
})("padding:", ({
|
|
131
|
+
theme
|
|
132
|
+
}) => theme.space[2], ";");
|
|
133
|
+
const LoadMore = /*#__PURE__*/_styled(Stack, {
|
|
134
|
+
target: "eeucz6c0"
|
|
135
|
+
})("padding:", ({
|
|
136
|
+
theme
|
|
137
|
+
}) => theme.space[0.5], ";");
|
|
138
|
+
const moveFocusDown = () => {
|
|
139
|
+
const options = document.querySelectorAll('#items > button[role="option"]:not([disabled])');
|
|
140
|
+
const activeItem = document.activeElement;
|
|
141
|
+
if (options) {
|
|
142
|
+
for (let i = 0; i < options?.length; i += 1) {
|
|
143
|
+
const listLength = options.length;
|
|
144
|
+
if (activeItem === options[i] && activeItem !== options[listLength - 1]) {
|
|
145
|
+
options[i + 1].focus();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const moveFocusUp = () => {
|
|
151
|
+
const options = document.querySelectorAll('#items > button[role="option"]:not([disabled])');
|
|
152
|
+
const activeItem = document.activeElement;
|
|
153
|
+
if (options) {
|
|
154
|
+
for (let i = 0; i < options.length; i += 1) {
|
|
155
|
+
if (activeItem === options[i] && activeItem !== options[0]) {
|
|
156
|
+
options[i - 1].focus();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const handleKeyDownSelect = key => {
|
|
162
|
+
if (key === 'ArrowDown') {
|
|
163
|
+
moveFocusDown();
|
|
164
|
+
}
|
|
165
|
+
if (key === 'ArrowUp') {
|
|
166
|
+
moveFocusUp();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
const handleClickOutside = (event, ref, setIsDropdownVisibile, refSelect, onSearch, options) => {
|
|
170
|
+
if (ref.current && !ref.current.contains(event.target) && !refSelect.current?.contains(event.target)) {
|
|
171
|
+
setIsDropdownVisibile(false); // hide dropdown when clicking outside of the dropdown
|
|
172
|
+
onSearch(options); // reset displayed options to default when dropdown is hidden
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const handleKeyDown = (event, ref, options, searchBarActive, setSearch, setDefaultSearch, search) => {
|
|
176
|
+
// Deals with default search
|
|
177
|
+
if (ref.current && !searchBarActive && !NON_SEARCHABLE_KEYS.includes(event.key) && document.activeElement?.ariaLabel !== 'search-bar') {
|
|
178
|
+
const currentSearch = search + event.key;
|
|
179
|
+
setSearch(currentSearch);
|
|
180
|
+
ref.current.focus();
|
|
181
|
+
if (!Array.isArray(options)) {
|
|
182
|
+
const closestOptions = {
|
|
183
|
+
...options
|
|
184
|
+
};
|
|
185
|
+
Object.keys(closestOptions).map(group => {
|
|
186
|
+
closestOptions[group] = closestOptions[group].filter(option => option.searchText ? option.searchText.toLocaleLowerCase().startsWith(currentSearch) : option.value.toLocaleLowerCase().startsWith(currentSearch));
|
|
187
|
+
return null;
|
|
188
|
+
});
|
|
189
|
+
const closestOption = closestOptions[Object.keys(closestOptions)[0]][0];
|
|
190
|
+
if (closestOption) {
|
|
191
|
+
setDefaultSearch(closestOption.searchText ?? closestOption.value);
|
|
192
|
+
} else {
|
|
193
|
+
setDefaultSearch(null);
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
const closestOption = [...options].filter(option => option.searchText ? option.searchText.toLocaleLowerCase().startsWith(currentSearch) : option.value.toLocaleLowerCase().startsWith(currentSearch))[0];
|
|
197
|
+
if (closestOption) {
|
|
198
|
+
setDefaultSearch(closestOption.searchText ?? closestOption.value);
|
|
199
|
+
} else {
|
|
200
|
+
setDefaultSearch(null);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const CreateDropdown = ({
|
|
206
|
+
isEmpty,
|
|
207
|
+
emptyState,
|
|
208
|
+
descriptionDirection,
|
|
209
|
+
onChange,
|
|
210
|
+
loadMore,
|
|
211
|
+
optionalInfoPlacement,
|
|
212
|
+
defaultSearchValue,
|
|
213
|
+
isLoading
|
|
214
|
+
}) => {
|
|
215
|
+
const {
|
|
216
|
+
setIsDropdownVisible,
|
|
217
|
+
options,
|
|
218
|
+
multiselect,
|
|
219
|
+
selectAll,
|
|
220
|
+
selectAllGroup,
|
|
221
|
+
displayedOptions,
|
|
222
|
+
setSelectedData,
|
|
223
|
+
selectedData
|
|
224
|
+
} = useSelectInput();
|
|
225
|
+
const focusedItemRef = useRef(null);
|
|
226
|
+
useEffect(() => {
|
|
227
|
+
if (defaultSearchValue && focusedItemRef?.current) {
|
|
228
|
+
focusedItemRef.current.focus();
|
|
229
|
+
}
|
|
230
|
+
}, [defaultSearchValue]);
|
|
231
|
+
if (isEmpty) {
|
|
232
|
+
return jsx(EmptyState, {
|
|
233
|
+
gap: 2,
|
|
234
|
+
alignItems: "center",
|
|
235
|
+
children: emptyState ?? jsx(Text, {
|
|
236
|
+
variant: "bodyStrong",
|
|
237
|
+
as: "p",
|
|
238
|
+
children: "No options"
|
|
239
|
+
})
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
const handleClick = (clickedOption, group) => {
|
|
243
|
+
setSelectedData({
|
|
244
|
+
type: 'selectOption',
|
|
245
|
+
clickedOption,
|
|
246
|
+
group
|
|
247
|
+
});
|
|
248
|
+
if (multiselect) {
|
|
249
|
+
if (selectedData.selectedValues.includes(clickedOption)) {
|
|
250
|
+
onChange?.(selectedData.selectedValues.filter(val => val !== clickedOption).map(val => val?.value));
|
|
251
|
+
} else {
|
|
252
|
+
onChange?.([...selectedData.selectedValues, clickedOption].map(val => val?.value));
|
|
253
|
+
}
|
|
254
|
+
} else {
|
|
255
|
+
onChange?.([clickedOption.value]);
|
|
256
|
+
}
|
|
257
|
+
setIsDropdownVisible(multiselect); // hide the dropdown on click when single select only
|
|
258
|
+
};
|
|
259
|
+
const selectAllOptions = () => {
|
|
260
|
+
setSelectedData({
|
|
261
|
+
type: 'selectAll'
|
|
262
|
+
});
|
|
263
|
+
if (selectedData.allSelected && onChange) {
|
|
264
|
+
onChange?.([]);
|
|
265
|
+
} else {
|
|
266
|
+
const allValues = [];
|
|
267
|
+
if (!Array.isArray(options)) {
|
|
268
|
+
Object.keys(options).map(group => options[group].map(option => {
|
|
269
|
+
if (!option.disabled) {
|
|
270
|
+
allValues.push(option);
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
}));
|
|
274
|
+
} else {
|
|
275
|
+
options.map(option => allValues.push(option));
|
|
276
|
+
}
|
|
277
|
+
onChange?.(allValues.map(value => value.value));
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
const handleSelectGroup = group => {
|
|
281
|
+
setSelectedData({
|
|
282
|
+
type: 'selectGroup',
|
|
283
|
+
selectedGroup: group
|
|
284
|
+
});
|
|
285
|
+
if (!Array.isArray(options)) {
|
|
286
|
+
if (selectedData.selectedGroups.includes(group)) {
|
|
287
|
+
const newSelectedValues = [...selectedData.selectedValues].filter(selectedValue => !options[group].includes(selectedValue));
|
|
288
|
+
onChange?.(newSelectedValues.map(value => value.value));
|
|
289
|
+
} else {
|
|
290
|
+
const newSelectedValues = [...selectedData.selectedValues];
|
|
291
|
+
options[group].map(option => newSelectedValues.includes(option) || option.disabled ? null : newSelectedValues.push(option));
|
|
292
|
+
onChange?.(newSelectedValues.map(value => value.value));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
return !Array.isArray(displayedOptions) ? jsx(DropdownContainer, {
|
|
297
|
+
role: "listbox",
|
|
298
|
+
id: "select-dropdown",
|
|
299
|
+
onKeyDown: event => {
|
|
300
|
+
event.preventDefault();
|
|
301
|
+
handleKeyDownSelect(event.key);
|
|
302
|
+
},
|
|
303
|
+
"data-grouped": true,
|
|
304
|
+
children: isLoading ? jsx(Skeleton, {
|
|
305
|
+
variant: "block"
|
|
306
|
+
}) : jsxs(Fragment, {
|
|
307
|
+
children: [selectAll && multiselect ? jsx(Stack, {
|
|
308
|
+
id: "items",
|
|
309
|
+
children: jsx(DropdownItem, {
|
|
310
|
+
disabled: false,
|
|
311
|
+
"data-selected": selectedData.allSelected,
|
|
312
|
+
"aria-label": "select-all",
|
|
313
|
+
"data-testid": "select-all",
|
|
314
|
+
id: "select-all",
|
|
315
|
+
role: "option",
|
|
316
|
+
onKeyDown: event => [' ', 'Enter'].includes(event.key) ? selectAllOptions() : null,
|
|
317
|
+
children: jsx(StyledCheckbox, {
|
|
318
|
+
checked: selectedData.allSelected,
|
|
319
|
+
disabled: false,
|
|
320
|
+
value: "select-all",
|
|
321
|
+
"data-testid": "select-all-checkbox",
|
|
322
|
+
onChange: selectAllOptions,
|
|
323
|
+
tabIndex: -1,
|
|
324
|
+
children: jsxs(Stack, {
|
|
325
|
+
direction: "column",
|
|
326
|
+
children: [jsx(Text, {
|
|
327
|
+
as: "span",
|
|
328
|
+
variant: "body",
|
|
329
|
+
placement: "left",
|
|
330
|
+
children: selectAll.label
|
|
331
|
+
}), jsx(Text, {
|
|
332
|
+
as: "span",
|
|
333
|
+
variant: "bodySmall",
|
|
334
|
+
sentiment: "neutral",
|
|
335
|
+
placement: "left",
|
|
336
|
+
prominence: "weak",
|
|
337
|
+
children: selectAll.description
|
|
338
|
+
})]
|
|
339
|
+
})
|
|
340
|
+
})
|
|
341
|
+
})
|
|
342
|
+
}) : null, Object.keys(displayedOptions).map(group => jsxs(Stack, {
|
|
343
|
+
gap: 0.25,
|
|
344
|
+
children: [displayedOptions[group].length > 0 ? jsx(DropdownGroupWrapper, {
|
|
345
|
+
id: selectAllGroup ? 'items' : undefined,
|
|
346
|
+
children: jsx(DropdownGroup, {
|
|
347
|
+
tabIndex: selectAllGroup ? 0 : -1,
|
|
348
|
+
onKeyDown: event => {
|
|
349
|
+
if ([' ', 'Enter'].includes(event.key)) {
|
|
350
|
+
event.preventDefault();
|
|
351
|
+
handleSelectGroup(group);
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
"data-selectgroup": selectAllGroup,
|
|
355
|
+
role: "group",
|
|
356
|
+
children: selectAllGroup ? jsx(StyledCheckbox, {
|
|
357
|
+
checked: selectedData.selectedGroups.includes(group),
|
|
358
|
+
disabled: false,
|
|
359
|
+
value: group,
|
|
360
|
+
onChange: () => handleSelectGroup(group),
|
|
361
|
+
"data-testid": "select-group",
|
|
362
|
+
tabIndex: -1,
|
|
363
|
+
children: jsx(Text, {
|
|
364
|
+
variant: "caption",
|
|
365
|
+
as: "span",
|
|
366
|
+
placement: "left",
|
|
367
|
+
children: group.toUpperCase()
|
|
368
|
+
})
|
|
369
|
+
}) : jsx(Text, {
|
|
370
|
+
variant: "caption",
|
|
371
|
+
as: "span",
|
|
372
|
+
placement: "left",
|
|
373
|
+
children: group.toUpperCase()
|
|
374
|
+
})
|
|
375
|
+
}, group)
|
|
376
|
+
}) : null, jsxs(Stack, {
|
|
377
|
+
id: "items",
|
|
378
|
+
gap: "0.25",
|
|
379
|
+
children: [displayedOptions[group].map((option, index) => jsx(DropdownItem, {
|
|
380
|
+
disabled: option.disabled,
|
|
381
|
+
"data-selected": selectedData.selectedValues.includes(option) && !option.disabled,
|
|
382
|
+
"aria-label": option.value,
|
|
383
|
+
"data-testid": `option-${index}`,
|
|
384
|
+
id: `option-${index}`,
|
|
385
|
+
role: "option",
|
|
386
|
+
onClick: () => {
|
|
387
|
+
if (!option.disabled) {
|
|
388
|
+
handleClick(option, group);
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
onKeyDown: event => [' ', 'Enter'].includes(event.key) ? handleClick(option, group) : null,
|
|
392
|
+
ref: option.value === defaultSearchValue || option.searchText === defaultSearchValue ? focusedItemRef : null,
|
|
393
|
+
children: multiselect ? jsx(StyledCheckbox, {
|
|
394
|
+
checked: selectedData.selectedValues.includes(option) && !option.disabled,
|
|
395
|
+
disabled: option.disabled,
|
|
396
|
+
value: option.value,
|
|
397
|
+
tabIndex: -1,
|
|
398
|
+
children: jsx(DisplayOption, {
|
|
399
|
+
option: option,
|
|
400
|
+
multiselect: multiselect,
|
|
401
|
+
descriptionDirection: descriptionDirection,
|
|
402
|
+
optionalInfoPlacement: optionalInfoPlacement
|
|
403
|
+
})
|
|
404
|
+
}) : jsx(DisplayOption, {
|
|
405
|
+
option: option,
|
|
406
|
+
multiselect: multiselect,
|
|
407
|
+
descriptionDirection: descriptionDirection,
|
|
408
|
+
optionalInfoPlacement: optionalInfoPlacement
|
|
409
|
+
})
|
|
410
|
+
}, option.value)), loadMore ? jsx(LoadMore, {
|
|
411
|
+
children: loadMore
|
|
412
|
+
}) : null]
|
|
413
|
+
})]
|
|
414
|
+
}, group))]
|
|
415
|
+
})
|
|
416
|
+
}) : jsxs(DropdownContainer, {
|
|
417
|
+
role: "listbox",
|
|
418
|
+
id: "select-dropdown",
|
|
419
|
+
onKeyDown: event => {
|
|
420
|
+
event.preventDefault();
|
|
421
|
+
handleKeyDownSelect(event.key);
|
|
422
|
+
},
|
|
423
|
+
gap: 0.25,
|
|
424
|
+
"data-grouped": false,
|
|
425
|
+
children: [selectAll && multiselect ? jsx(Stack, {
|
|
426
|
+
id: "items",
|
|
427
|
+
gap: 0.25,
|
|
428
|
+
children: jsx(DropdownItem, {
|
|
429
|
+
disabled: false,
|
|
430
|
+
"data-selected": selectedData.allSelected,
|
|
431
|
+
"aria-label": "select-all",
|
|
432
|
+
"data-testid": "select-all",
|
|
433
|
+
role: "option",
|
|
434
|
+
onKeyDown: event => [' ', 'Enter'].includes(event.key) ? selectAllOptions() : null,
|
|
435
|
+
children: jsx(StyledCheckbox, {
|
|
436
|
+
checked: selectedData.allSelected,
|
|
437
|
+
disabled: false,
|
|
438
|
+
value: "select-all",
|
|
439
|
+
"data-testid": "select-all-checkbox",
|
|
440
|
+
onChange: selectAllOptions,
|
|
441
|
+
tabIndex: -1,
|
|
442
|
+
children: jsxs(Stack, {
|
|
443
|
+
direction: "column",
|
|
444
|
+
children: [jsx(Text, {
|
|
445
|
+
as: "span",
|
|
446
|
+
variant: "body",
|
|
447
|
+
placement: "left",
|
|
448
|
+
children: selectAll.label
|
|
449
|
+
}), jsx(Text, {
|
|
450
|
+
as: "span",
|
|
451
|
+
variant: "bodySmall",
|
|
452
|
+
sentiment: "neutral",
|
|
453
|
+
placement: "left",
|
|
454
|
+
prominence: "weak",
|
|
455
|
+
children: selectAll.description
|
|
456
|
+
})]
|
|
457
|
+
})
|
|
458
|
+
})
|
|
459
|
+
})
|
|
460
|
+
}) : null, jsxs(Stack, {
|
|
461
|
+
id: "items",
|
|
462
|
+
gap: 0.25,
|
|
463
|
+
children: [isLoading ? jsx(Skeleton, {
|
|
464
|
+
variant: "block"
|
|
465
|
+
}) : displayedOptions.map((option, index) => jsx(DropdownItem, {
|
|
466
|
+
disabled: option.disabled,
|
|
467
|
+
"data-selected": selectedData.selectedValues.includes(option) && !option.disabled,
|
|
468
|
+
onClick: () => {
|
|
469
|
+
if (!option.disabled) {
|
|
470
|
+
handleClick(option);
|
|
471
|
+
}
|
|
472
|
+
},
|
|
473
|
+
"aria-label": option.value,
|
|
474
|
+
"data-testid": `option-${index}`,
|
|
475
|
+
id: `option-${index}`,
|
|
476
|
+
role: "option",
|
|
477
|
+
ref: option.value === defaultSearchValue || option.searchText === defaultSearchValue ? focusedItemRef : null,
|
|
478
|
+
onKeyDown: event => [' ', 'Enter'].includes(event.key) ? handleClick(option) : null,
|
|
479
|
+
children: multiselect ? jsx(StyledCheckbox, {
|
|
480
|
+
checked: selectedData.selectedValues.includes(option) && !option.disabled,
|
|
481
|
+
disabled: option.disabled,
|
|
482
|
+
value: option.value,
|
|
483
|
+
tabIndex: -1,
|
|
484
|
+
children: jsx(DisplayOption, {
|
|
485
|
+
option: option,
|
|
486
|
+
multiselect: multiselect,
|
|
487
|
+
descriptionDirection: descriptionDirection,
|
|
488
|
+
optionalInfoPlacement: optionalInfoPlacement
|
|
489
|
+
})
|
|
490
|
+
}) : jsx(DisplayOption, {
|
|
491
|
+
option: option,
|
|
492
|
+
multiselect: multiselect,
|
|
493
|
+
descriptionDirection: descriptionDirection,
|
|
494
|
+
optionalInfoPlacement: optionalInfoPlacement
|
|
495
|
+
})
|
|
496
|
+
}, option.value)), loadMore ? jsx(LoadMore, {
|
|
497
|
+
children: loadMore
|
|
498
|
+
}) : null]
|
|
499
|
+
})]
|
|
500
|
+
});
|
|
501
|
+
};
|
|
502
|
+
const Dropdown = ({
|
|
503
|
+
children,
|
|
504
|
+
emptyState,
|
|
505
|
+
descriptionDirection,
|
|
506
|
+
searchable,
|
|
507
|
+
placeholder,
|
|
508
|
+
footer,
|
|
509
|
+
onChange,
|
|
510
|
+
refSelect,
|
|
511
|
+
loadMore,
|
|
512
|
+
optionalInfoPlacement,
|
|
513
|
+
isLoading
|
|
514
|
+
}) => {
|
|
515
|
+
const {
|
|
516
|
+
setIsDropdownVisible,
|
|
517
|
+
isDropdownVisible,
|
|
518
|
+
onSearch,
|
|
519
|
+
searchInput,
|
|
520
|
+
options,
|
|
521
|
+
displayedOptions
|
|
522
|
+
} = useSelectInput();
|
|
523
|
+
const [searchBarActive, setSearchBarActive] = useState(false);
|
|
524
|
+
const [defaultSearchValue, setDefaultSearch] = useState(null);
|
|
525
|
+
const maxWidth = refSelect.current?.offsetWidth;
|
|
526
|
+
const ref = useRef(null);
|
|
527
|
+
const [search, setSearch] = useState('');
|
|
528
|
+
useEffect(() => {
|
|
529
|
+
if (!searchInput) {
|
|
530
|
+
onSearch(options);
|
|
531
|
+
}
|
|
532
|
+
}, [onSearch, options, searchInput]);
|
|
533
|
+
useEffect(() => {
|
|
534
|
+
if (!isDropdownVisible) {
|
|
535
|
+
setDefaultSearch(null);
|
|
536
|
+
setSearch('');
|
|
537
|
+
}
|
|
538
|
+
document.addEventListener('mousedown', event => handleClickOutside(event, ref, setIsDropdownVisible, refSelect, onSearch, options));
|
|
539
|
+
if (!searchable) {
|
|
540
|
+
document.addEventListener('keydown', event => handleKeyDown(event, ref, options, searchBarActive, setSearch, setDefaultSearch, search));
|
|
541
|
+
}
|
|
542
|
+
return () => {
|
|
543
|
+
document.removeEventListener('mousedown', event => handleClickOutside(event, ref, setIsDropdownVisible, refSelect, onSearch, options));
|
|
544
|
+
if (!searchable) {
|
|
545
|
+
document.removeEventListener('keydown', event => handleKeyDown(event, ref, options, searchBarActive, setSearch, setDefaultSearch, search));
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
}, [isDropdownVisible, searchBarActive, options, onSearch, search, refSelect, setDefaultSearch, setIsDropdownVisible, searchable]);
|
|
549
|
+
const isEmpty = useMemo(() => {
|
|
550
|
+
if (Array.isArray(displayedOptions)) {
|
|
551
|
+
return !(displayedOptions.length > 0);
|
|
552
|
+
}
|
|
553
|
+
const groups = Object.keys(displayedOptions);
|
|
554
|
+
for (const group of groups) {
|
|
555
|
+
if (displayedOptions[group].length !== 0) {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return true;
|
|
560
|
+
}, [displayedOptions]);
|
|
561
|
+
return jsx(StyledPopup, {
|
|
562
|
+
visible: isDropdownVisible,
|
|
563
|
+
text: jsxs(Stack, {
|
|
564
|
+
children: [searchable && !isLoading ? jsx(SearchBarDropdown, {
|
|
565
|
+
placeholder: placeholder,
|
|
566
|
+
displayedOptions: displayedOptions,
|
|
567
|
+
setSearchBarActive: setSearchBarActive,
|
|
568
|
+
onChange: onChange
|
|
569
|
+
}) : null, jsx(CreateDropdown, {
|
|
570
|
+
isEmpty: isEmpty,
|
|
571
|
+
emptyState: emptyState,
|
|
572
|
+
descriptionDirection: descriptionDirection,
|
|
573
|
+
onChange: onChange,
|
|
574
|
+
loadMore: loadMore,
|
|
575
|
+
optionalInfoPlacement: optionalInfoPlacement,
|
|
576
|
+
defaultSearchValue: defaultSearchValue,
|
|
577
|
+
isLoading: isLoading
|
|
578
|
+
}), footer ? jsx(PopupFooter, {
|
|
579
|
+
children: footer
|
|
580
|
+
}) : null]
|
|
581
|
+
}),
|
|
582
|
+
placement: "bottom",
|
|
583
|
+
containerFullWidth: true,
|
|
584
|
+
disableAnimation: true,
|
|
585
|
+
maxWidth: maxWidth,
|
|
586
|
+
hasArrow: false,
|
|
587
|
+
ref: ref,
|
|
588
|
+
tabIndex: 0,
|
|
589
|
+
role: "dialog",
|
|
590
|
+
debounceDelay: 0,
|
|
591
|
+
children: children
|
|
592
|
+
});
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
export { Dropdown };
|