@db-ux/react-core-components 4.9.0 → 4.10.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 (166) hide show
  1. package/CHANGELOG.md +17 -1
  2. package/README.md +97 -35
  3. package/dist/components/accordion/accordion.js +102 -86
  4. package/dist/components/accordion/model.d.ts +2 -2
  5. package/dist/components/accordion/model.js +4 -2
  6. package/dist/components/accordion-item/accordion-item.d.ts +2 -1
  7. package/dist/components/accordion-item/accordion-item.js +89 -56
  8. package/dist/components/accordion-item/model.d.ts +5 -1
  9. package/dist/components/badge/badge.js +72 -32
  10. package/dist/components/badge/model.d.ts +1 -1
  11. package/dist/components/badge/model.js +12 -1
  12. package/dist/components/brand/brand.js +60 -13
  13. package/dist/components/brand/model.d.ts +1 -1
  14. package/dist/components/button/button.js +75 -21
  15. package/dist/components/button/model.d.ts +1 -1
  16. package/dist/components/button/model.js +13 -2
  17. package/dist/components/card/card.js +65 -15
  18. package/dist/components/card/model.d.ts +1 -1
  19. package/dist/components/card/model.js +8 -2
  20. package/dist/components/checkbox/checkbox.js +163 -157
  21. package/dist/components/checkbox/model.d.ts +1 -1
  22. package/dist/components/custom-button/custom-button.js +65 -10
  23. package/dist/components/custom-button/model.d.ts +2 -2
  24. package/dist/components/custom-select/custom-select.js +509 -727
  25. package/dist/components/custom-select/model.d.ts +4 -4
  26. package/dist/components/custom-select/model.js +8 -1
  27. package/dist/components/custom-select-dropdown/custom-select-dropdown.js +62 -11
  28. package/dist/components/custom-select-dropdown/model.d.ts +1 -1
  29. package/dist/components/custom-select-dropdown/model.js +8 -1
  30. package/dist/components/custom-select-form-field/custom-select-form-field.js +57 -10
  31. package/dist/components/custom-select-form-field/model.d.ts +1 -1
  32. package/dist/components/custom-select-list/custom-select-list.js +60 -11
  33. package/dist/components/custom-select-list/model.d.ts +1 -1
  34. package/dist/components/custom-select-list-item/custom-select-list-item.js +89 -32
  35. package/dist/components/custom-select-list-item/model.d.ts +1 -1
  36. package/dist/components/custom-select-list-item/model.js +4 -1
  37. package/dist/components/divider/divider.js +60 -10
  38. package/dist/components/divider/model.d.ts +1 -1
  39. package/dist/components/divider/model.js +4 -2
  40. package/dist/components/drawer/drawer.js +118 -97
  41. package/dist/components/drawer/model.d.ts +1 -1
  42. package/dist/components/drawer/model.js +14 -4
  43. package/dist/components/header/header.js +143 -65
  44. package/dist/components/header/model.d.ts +1 -1
  45. package/dist/components/icon/icon.js +61 -12
  46. package/dist/components/icon/model.d.ts +1 -1
  47. package/dist/components/icon/model.js +11 -1
  48. package/dist/components/infotext/infotext.js +62 -12
  49. package/dist/components/infotext/model.d.ts +1 -1
  50. package/dist/components/input/input.js +198 -167
  51. package/dist/components/input/model.d.ts +1 -1
  52. package/dist/components/input/model.js +21 -5
  53. package/dist/components/link/link.js +70 -12
  54. package/dist/components/link/model.d.ts +1 -1
  55. package/dist/components/link/model.js +8 -3
  56. package/dist/components/navigation/model.d.ts +1 -1
  57. package/dist/components/navigation/navigation.js +57 -11
  58. package/dist/components/navigation-item/model.d.ts +6 -2
  59. package/dist/components/navigation-item/navigation-item.js +117 -81
  60. package/dist/components/notification/model.d.ts +1 -1
  61. package/dist/components/notification/model.js +12 -3
  62. package/dist/components/notification/notification.js +100 -32
  63. package/dist/components/page/model.d.ts +1 -1
  64. package/dist/components/page/model.js +4 -2
  65. package/dist/components/page/page.js +74 -46
  66. package/dist/components/popover/model.d.ts +1 -1
  67. package/dist/components/popover/popover.js +126 -125
  68. package/dist/components/radio/model.d.ts +1 -1
  69. package/dist/components/radio/radio.js +124 -85
  70. package/dist/components/section/model.d.ts +1 -1
  71. package/dist/components/section/section.js +59 -10
  72. package/dist/components/select/model.d.ts +1 -1
  73. package/dist/components/select/select.js +188 -191
  74. package/dist/components/stack/model.d.ts +1 -1
  75. package/dist/components/stack/model.js +14 -4
  76. package/dist/components/stack/stack.js +63 -10
  77. package/dist/components/switch/model.d.ts +1 -1
  78. package/dist/components/switch/switch.js +182 -150
  79. package/dist/components/tab-item/model.d.ts +1 -1
  80. package/dist/components/tab-item/tab-item.js +105 -76
  81. package/dist/components/tab-list/model.d.ts +1 -1
  82. package/dist/components/tab-list/tab-list.js +60 -11
  83. package/dist/components/tab-panel/model.d.ts +1 -1
  84. package/dist/components/tab-panel/tab-panel.js +59 -13
  85. package/dist/components/tabs/model.d.ts +3 -3
  86. package/dist/components/tabs/model.js +4 -2
  87. package/dist/components/tabs/tabs.js +191 -178
  88. package/dist/components/tag/model.d.ts +1 -1
  89. package/dist/components/tag/model.js +4 -1
  90. package/dist/components/tag/tag.js +91 -32
  91. package/dist/components/textarea/model.d.ts +1 -1
  92. package/dist/components/textarea/model.js +13 -2
  93. package/dist/components/textarea/textarea.js +165 -150
  94. package/dist/components/tooltip/model.d.ts +1 -1
  95. package/dist/components/tooltip/model.js +4 -1
  96. package/dist/components/tooltip/tooltip.js +119 -115
  97. package/dist/index.js +64 -81
  98. package/dist/shared/constants.js +32 -94
  99. package/dist/shared/figma.d.ts +12 -6
  100. package/dist/shared/model.js +73 -22
  101. package/dist/utils/document-click-listener.js +26 -29
  102. package/dist/utils/document-scroll-listener.js +30 -38
  103. package/dist/utils/floating-components.js +107 -358
  104. package/dist/utils/form-components.js +34 -60
  105. package/dist/utils/index.js +49 -167
  106. package/dist/utils/navigation.js +68 -135
  107. package/dist/utils/react.js +10 -15
  108. package/package.json +11 -8
  109. package/dist/components/accordion/index.js +0 -1
  110. package/dist/components/accordion-item/index.js +0 -1
  111. package/dist/components/accordion-item/model.js +0 -1
  112. package/dist/components/badge/index.js +0 -1
  113. package/dist/components/brand/index.js +0 -1
  114. package/dist/components/brand/model.js +0 -1
  115. package/dist/components/button/index.js +0 -1
  116. package/dist/components/card/index.js +0 -1
  117. package/dist/components/checkbox/index.js +0 -1
  118. package/dist/components/checkbox/model.js +0 -1
  119. package/dist/components/custom-button/index.js +0 -1
  120. package/dist/components/custom-button/model.js +0 -1
  121. package/dist/components/custom-select/index.js +0 -1
  122. package/dist/components/custom-select-dropdown/index.js +0 -1
  123. package/dist/components/custom-select-form-field/index.js +0 -1
  124. package/dist/components/custom-select-form-field/model.js +0 -1
  125. package/dist/components/custom-select-list/index.js +0 -1
  126. package/dist/components/custom-select-list/model.js +0 -1
  127. package/dist/components/custom-select-list-item/index.js +0 -1
  128. package/dist/components/divider/index.js +0 -1
  129. package/dist/components/drawer/index.js +0 -1
  130. package/dist/components/header/index.js +0 -1
  131. package/dist/components/header/model.js +0 -1
  132. package/dist/components/icon/index.js +0 -1
  133. package/dist/components/infotext/index.js +0 -1
  134. package/dist/components/infotext/model.js +0 -1
  135. package/dist/components/input/index.js +0 -1
  136. package/dist/components/link/index.js +0 -1
  137. package/dist/components/navigation/index.js +0 -1
  138. package/dist/components/navigation/model.js +0 -1
  139. package/dist/components/navigation-item/index.js +0 -1
  140. package/dist/components/navigation-item/model.js +0 -1
  141. package/dist/components/notification/index.js +0 -1
  142. package/dist/components/page/index.js +0 -1
  143. package/dist/components/popover/index.js +0 -1
  144. package/dist/components/popover/model.js +0 -1
  145. package/dist/components/radio/index.js +0 -1
  146. package/dist/components/radio/model.js +0 -1
  147. package/dist/components/section/index.js +0 -1
  148. package/dist/components/section/model.js +0 -1
  149. package/dist/components/select/index.js +0 -1
  150. package/dist/components/select/model.js +0 -1
  151. package/dist/components/stack/index.js +0 -1
  152. package/dist/components/switch/index.js +0 -1
  153. package/dist/components/switch/model.js +0 -1
  154. package/dist/components/tab-item/index.js +0 -1
  155. package/dist/components/tab-item/model.js +0 -1
  156. package/dist/components/tab-list/index.js +0 -1
  157. package/dist/components/tab-list/model.js +0 -1
  158. package/dist/components/tab-panel/index.js +0 -1
  159. package/dist/components/tab-panel/model.js +0 -1
  160. package/dist/components/tabs/index.js +0 -1
  161. package/dist/components/tag/index.js +0 -1
  162. package/dist/components/textarea/index.js +0 -1
  163. package/dist/components/tooltip/index.js +0 -1
  164. package/dist/shared/examples/index.js +0 -4
  165. package/dist/shared/figma.js +0 -1
  166. package/dist/shared/showcase/show-code-link.js +0 -51
@@ -1,729 +1,511 @@
1
1
  "use client";
2
- import * as React from "react";
3
- import { filterPassingProps, getRootProps } from "../../utils/react";
4
- import { useState, useRef, useEffect, forwardRef } from "react";
5
- import { DEFAULT_CLOSE_BUTTON, DEFAULT_INVALID_MESSAGE, DEFAULT_INVALID_MESSAGE_ID_SUFFIX, DEFAULT_LABEL, DEFAULT_LABEL_ID_SUFFIX, DEFAULT_MESSAGE, DEFAULT_MESSAGE_ID_SUFFIX, DEFAULT_PLACEHOLDER_ID_SUFFIX, DEFAULT_REMOVE, DEFAULT_SELECT_ID_SUFFIX, DEFAULT_SELECTED, DEFAULT_VALID_MESSAGE, DEFAULT_VALID_MESSAGE_ID_SUFFIX, } from "../../shared/constants";
6
- import { cls, delay, getBoolean, getBooleanAsString, getHideProp, getOptionKey, getSearchInput, hasVoiceOver, stringPropVisible, } from "../../utils";
7
- import { DocumentClickListener } from "../../utils/document-click-listener";
8
- import { DocumentScrollListener } from "../../utils/document-scroll-listener";
9
- import { handleFixedDropdown } from "../../utils/floating-components";
10
- import { addResetEventListener, } from "../../utils/form-components";
11
- import DBButton from "../button/button";
12
- import DBCustomSelectDropdown from "../custom-select-dropdown/custom-select-dropdown";
13
- import DBCustomSelectListItem from "../custom-select-list-item/custom-select-list-item";
14
- import DBCustomSelectList from "../custom-select-list/custom-select-list";
15
- import DBInfotext from "../infotext/infotext";
16
- import DBInput from "../input/input";
17
- import DBTag from "../tag/tag";
18
- import DBTooltip from "../tooltip/tooltip";
19
- import { useId } from "react";
20
- function DBCustomSelectFn(props, component) {
21
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
22
- props = Object.assign({ clearSelectionText: "Clear selection", showClearSelection: true }, props);
23
- const uuid = useId();
24
- const _ref = component || useRef(component);
25
- const detailsRef = useRef(null);
26
- const selectRef = useRef(null);
27
- const selectAllRef = useRef(null);
28
- const searchInputRef = useRef(null);
29
- const [_id, set_id] = useState(() => undefined);
30
- const [_messageId, set_messageId] = useState(() => undefined);
31
- const [_validMessageId, set_validMessageId] = useState(() => undefined);
32
- const [_invalidMessageId, set_invalidMessageId] = useState(() => undefined);
33
- const [_invalidMessage, set_invalidMessage] = useState(() => undefined);
34
- const [_selectId, set_selectId] = useState(() => undefined);
35
- const [_labelId, set_labelId] = useState(() => undefined);
36
- const [_summaryId, set_summaryId] = useState(() => undefined);
37
- const [_placeholderId, set_placeholderId] = useState(() => undefined);
38
- const [_infoTextId, set_infoTextId] = useState(() => undefined);
39
- const [_validity, set_validity] = useState(() => "no-validation");
40
- const [_userInteraction, set_userInteraction] = useState(() => false);
41
- const [abortController, setAbortController] = useState(() => undefined);
42
- const [_descByIds, set_descByIds] = useState(() => undefined);
43
- const [_selectedLabels, set_selectedLabels] = useState(() => "");
44
- const [_selectedLabelsId, set_selectedLabelsId] = useState(() => undefined);
45
- const [_voiceOverFallback, set_voiceOverFallback] = useState(() => "");
46
- const [_selectedOptions, set_selectedOptions] = useState(() => []);
47
- const [selectAllEnabled, setSelectAllEnabled] = useState(() => false);
48
- const [searchEnabled, setSearchEnabled] = useState(() => false);
49
- const [amountOptions, setAmountOptions] = useState(() => 0);
50
- const [_values, set_values] = useState(() => []);
51
- const [_options, set_options] = useState(() => []);
52
- const [_hasNoOptions, set_hasNoOptions] = useState(() => false);
53
- const [_documentClickListenerCallbackId, set_documentClickListenerCallbackId,] = useState(() => undefined);
54
- const [_internalChangeTimestamp, set_internalChangeTimestamp] = useState(() => 0);
55
- const [_documentScrollListenerCallbackId, set_documentScrollListenerCallbackId,] = useState(() => undefined);
56
- const [_observer, set_observer] = useState(() => undefined);
57
- function handleDocumentScroll(event) {
58
- var _a, _b;
59
- if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.contains) &&
60
- ((_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.contains(detailsRef.current))) {
61
- handleAutoPlacement();
62
- }
63
- }
64
- const [_searchValue, set_searchValue] = useState(() => undefined);
65
- function hasValidState() {
66
- var _a;
67
- return !!((_a = props.validMessage) !== null && _a !== void 0 ? _a : props.validation === "valid");
68
- }
69
- function handleValidation() {
70
- var _a, _b, _c, _d, _e, _f, _g, _h;
71
- if (selectRef.current) {
72
- selectRef.current.value = getNativeSelectValue();
73
- }
74
- /* For a11y reasons we need to map the correct message with the select */
75
- if (!((_a = selectRef.current) === null || _a === void 0 ? void 0 : _a.validity.valid) || props.validation === "invalid") {
76
- setDescById(_invalidMessageId);
77
- set_invalidMessage(props.invalidMessage ||
78
- ((_b = selectRef.current) === null || _b === void 0 ? void 0 : _b.validationMessage) ||
79
- DEFAULT_INVALID_MESSAGE);
80
- if (hasVoiceOver()) {
81
- set_voiceOverFallback(_invalidMessage);
82
- delay(() => set_voiceOverFallback(""), 1000);
83
- }
84
- if (_userInteraction) {
85
- set_validity((_c = props.validation) !== null && _c !== void 0 ? _c : "invalid");
86
- }
87
- }
88
- else if (hasValidState() &&
89
- ((_d = selectRef.current) === null || _d === void 0 ? void 0 : _d.validity.valid) &&
90
- props.required) {
91
- setDescById(_validMessageId);
92
- if (hasVoiceOver()) {
93
- set_voiceOverFallback((_e = props.validMessage) !== null && _e !== void 0 ? _e : DEFAULT_VALID_MESSAGE);
94
- delay(() => set_voiceOverFallback(""), 1000);
95
- }
96
- set_validity((_f = props.validation) !== null && _f !== void 0 ? _f : "valid");
97
- }
98
- else if (stringPropVisible(props.message, props.showMessage)) {
99
- setDescById(_messageId);
100
- set_validity((_g = props.validation) !== null && _g !== void 0 ? _g : "no-validation");
101
- }
102
- else {
103
- setDescById(_placeholderId);
104
- set_validity((_h = props.validation) !== null && _h !== void 0 ? _h : "no-validation");
105
- }
106
- }
107
- function handleDropdownToggle(event) {
108
- if (props.onDropdownToggle) {
109
- event.stopPropagation();
110
- props.onDropdownToggle(event);
111
- }
112
- if (event.target instanceof HTMLDetailsElement && event.target.open) {
113
- set_documentClickListenerCallbackId(new DocumentClickListener().addCallback((event) => handleDocumentClose(event)));
114
- set_documentScrollListenerCallbackId(new DocumentScrollListener().addCallback((event) => handleDocumentScroll(event)));
115
- handleAutoPlacement();
116
- _observer === null || _observer === void 0 ? void 0 : _observer.observe(detailsRef.current);
117
- if (!event.target.dataset["test"]) {
118
- // We need this workaround for snapshot testing
119
- handleOpenByKeyboardFocus();
120
- }
121
- }
122
- else {
123
- if (_documentClickListenerCallbackId) {
124
- new DocumentClickListener().removeCallback(_documentClickListenerCallbackId);
125
- }
126
- if (_documentScrollListenerCallbackId) {
127
- new DocumentScrollListener().removeCallback(_documentScrollListenerCallbackId);
128
- }
129
- _observer === null || _observer === void 0 ? void 0 : _observer.unobserve(detailsRef.current);
130
- }
131
- }
132
- function getNativeSelectValue() {
133
- var _a;
134
- if (_values === null || _values === void 0 ? void 0 : _values.length) {
135
- return (_a = _values.at(0)) !== null && _a !== void 0 ? _a : "";
136
- }
137
- return "";
138
- }
139
- function setDescById(descId) {
140
- const descByIds = [];
141
- if (descId) {
142
- descByIds.push(descId);
143
- }
144
- if (_selectedLabelsId && (_selectedLabels === null || _selectedLabels === void 0 ? void 0 : _selectedLabels.length)) {
145
- descByIds.push(_selectedLabelsId);
146
- }
147
- set_descByIds(descByIds.join(" "));
148
- }
149
- function getSelectAllLabel() {
150
- var _a;
151
- return (_a = props.selectAllLabel) !== null && _a !== void 0 ? _a : DEFAULT_LABEL;
152
- }
153
- function getOptionLabel(option) {
154
- var _a, _b, _c;
155
- return (_c = (_a = option.label) !== null && _a !== void 0 ? _a : (_b = option.value) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : "";
156
- }
157
- function getOptionChecked(value) {
158
- if (value && (_values === null || _values === void 0 ? void 0 : _values.includes)) {
159
- return _values === null || _values === void 0 ? void 0 : _values.includes(value);
160
- }
161
- return false;
162
- }
163
- function getTagRemoveLabel(option) {
164
- const removeTexts = props.removeTagsTexts;
165
- const options = props.options;
166
- if (removeTexts && options) {
167
- // Find the index of the option in the original options array
168
- const optionIndex = options.findIndex((opt) => opt.value === option.value);
169
- if (optionIndex >= 0 && optionIndex < removeTexts.length) {
170
- return removeTexts[optionIndex];
171
- }
172
- }
173
- // Fallback to default behavior
174
- return `${DEFAULT_REMOVE} ${getOptionLabel(option)}`;
175
- }
176
- function handleTagRemove(option, event) {
177
- if (event) {
178
- event.stopPropagation();
179
- }
180
- handleSelect(option.value);
181
- handleSummaryFocus();
182
- }
183
- function handleAutoPlacement() {
184
- if (detailsRef.current) {
185
- const dropdown = detailsRef.current.querySelector("article");
186
- if (dropdown) {
187
- // This is a workaround for Angular
188
- delay(() => {
189
- var _a;
190
- handleFixedDropdown(dropdown, detailsRef.current, (_a = props.placement) !== null && _a !== void 0 ? _a : "bottom");
191
- }, 1);
192
- }
193
- }
194
- }
195
- function handleArrowDownUp(event) {
196
- var _a, _b, _c;
197
- if ((_a = detailsRef.current) === null || _a === void 0 ? void 0 : _a.open) {
198
- if (self.document) {
199
- const activeElement = self.document.activeElement;
200
- if (activeElement) {
201
- // 1. we check if we are currently focusing a checkbox in the dropdown
202
- const isCheckbox = activeElement.getAttribute("type") === "checkbox" ||
203
- activeElement.getAttribute("type") === "radio";
204
- if (isCheckbox) {
205
- const listElement = activeElement === null || activeElement === void 0 ? void 0 : activeElement.closest("li");
206
- if (event.key === "ArrowDown" || event.key === "ArrowRight") {
207
- // Find next element with input, skipping group titles
208
- let nextElement = listElement === null || listElement === void 0 ? void 0 : listElement.nextElementSibling;
209
- while (nextElement) {
210
- const nextInput = nextElement.querySelector("input");
211
- if (nextInput) {
212
- nextInput.focus();
213
- break;
214
- }
215
- nextElement = nextElement.nextElementSibling;
216
- }
217
- if (!nextElement) {
218
- // We are on the last checkbox we move to the top checkbox
219
- handleFocusFirstDropdownCheckbox(activeElement);
220
- }
221
- }
222
- else {
223
- // Find previous element with input, skipping group titles
224
- let prevElement = listElement === null || listElement === void 0 ? void 0 : listElement.previousElementSibling;
225
- while (prevElement) {
226
- const prevInput = prevElement.querySelector("input");
227
- if (prevInput) {
228
- prevInput.focus();
229
- break;
230
- }
231
- prevElement = prevElement.previousElementSibling;
232
- }
233
- if (!prevElement) {
234
- // Check if we have a "select all" checkbox (only relevant for multi-select)
235
- const selectAllCheckbox = detailsRef.current.querySelector(`input[type="checkbox"]`);
236
- if (selectAllCheckbox && selectAllCheckbox !== activeElement) {
237
- // We are on the top list checkbox but there is a select all checkbox as well
238
- handleFocusFirstDropdownCheckbox(activeElement);
239
- }
240
- else {
241
- // We are on the top checkbox, we need to move to the search
242
- // or to the last checkbox
243
- const search = getSearchInput(detailsRef.current);
244
- if (search) {
245
- delay(() => {
246
- search.focus();
247
- }, 100);
248
- }
249
- else {
250
- const checkboxList = Array.from((_b = detailsRef.current) === null || _b === void 0 ? void 0 : _b.querySelectorAll(`input[type="checkbox"],input[type="radio"]`));
251
- if (checkboxList.length) {
252
- (_c = checkboxList.at(-1)) === null || _c === void 0 ? void 0 : _c.focus();
253
- }
254
- }
255
- }
256
- }
257
- }
258
- }
259
- else {
260
- // 2. If we are on the search, and press up we go back to summary and close
261
- if (activeElement.getAttribute("type") === "search" &&
262
- (event.key === "ArrowUp" || event.key === "ArrowLeft")) {
263
- handleClose(undefined, true);
264
- handleSummaryFocus();
265
- }
266
- else {
267
- // 3. Otherwise, we need to move to the first checkbox
268
- handleFocusFirstDropdownCheckbox(activeElement);
269
- }
270
- }
271
- }
272
- }
273
- }
274
- else if (event.key === "ArrowDown" || event.key === "ArrowRight") {
275
- // Open dropdown with arrows see https://www.w3.org/WAI/ARIA/apg/patterns/combobox/#keyboardinteraction
276
- handleAutoPlacement();
277
- if (detailsRef.current) {
278
- detailsRef.current.open = true;
279
- }
280
- handleOpenByKeyboardFocus();
281
- }
282
- event.stopPropagation();
283
- event.preventDefault();
284
- }
285
- function handleKeyboardPress(event) {
286
- var _a, _b;
287
- event.stopPropagation();
288
- if (event.key === "Escape" && ((_a = detailsRef.current) === null || _a === void 0 ? void 0 : _a.open)) {
289
- handleClose(undefined, true);
290
- handleSummaryFocus();
291
- }
292
- else if (event.key === "Enter" && ((_b = detailsRef.current) === null || _b === void 0 ? void 0 : _b.open)) {
293
- // Handle Enter key to select option like Space key
294
- if (self.document) {
295
- const activeElement = self.document.activeElement;
296
- if (["checkbox", "radio"].includes(activeElement.getAttribute("type") || "")) {
297
- // Trigger click to simulate Space key behavior
298
- activeElement.click();
299
- event.preventDefault();
300
- }
301
- else if (activeElement.getAttribute("type") === "search") {
302
- // When Enter is pressed in search field, select the first available option
303
- const firstOption = _options === null || _options === void 0 ? void 0 : _options.find((opt) => !opt.isGroupTitle && !opt.disabled);
304
- if (firstOption === null || firstOption === void 0 ? void 0 : firstOption.value) {
305
- handleSelect(firstOption.value);
306
- event.preventDefault();
307
- }
308
- }
309
- }
310
- }
311
- else if (event.key === "ArrowDown" ||
312
- event.key === "ArrowUp" ||
313
- event.key === "ArrowLeft" ||
314
- event.key === "ArrowRight") {
315
- handleArrowDownUp(event);
316
- }
317
- }
318
- function handleClose(event, forceClose) {
319
- if (detailsRef.current) {
320
- if (forceClose) {
321
- detailsRef.current.open = false;
322
- handleSummaryFocus();
323
- }
324
- else if (detailsRef.current.open && event) {
325
- if (event.relatedTarget) {
326
- const relatedTarget = event.relatedTarget;
327
- // We close if the focus is on something like a <button> etc. which is not inside the <details> element
328
- // Inside a <dialog> there is some focus problem because of the top-layer
329
- // We do not want to focus <dialog> itself
330
- if (!detailsRef.current.contains(relatedTarget) &&
331
- relatedTarget.localName !== "dialog") {
332
- // We need to use delay here because the combination of `contains`
333
- // and changing the DOM element causes a race condition inside browser
334
- void delay(() => (detailsRef.current.open = false), 1);
335
- }
336
- }
337
- }
338
- }
339
- }
340
- function handleDocumentClose(event) {
341
- var _a;
342
- if (event) {
343
- // stencil is sending a custom event which wraps the pointer event into details
344
- const target = event.target;
345
- if (((_a = detailsRef.current) === null || _a === void 0 ? void 0 : _a.open) && !detailsRef.current.contains(target)) {
346
- detailsRef.current.open = false;
347
- }
348
- }
349
- }
350
- function handleOptionSelected(values) {
351
- const skip = new Date().getTime() - _internalChangeTimestamp < 200;
352
- if (skip)
353
- return;
354
- set_values(values);
355
- set_userInteraction(true);
356
- if (props.onOptionSelected) {
357
- props.onOptionSelected(values !== null && values !== void 0 ? values : []);
358
- }
359
- set_internalChangeTimestamp(new Date().getTime());
360
- }
361
- function handleSelect(value) {
362
- if (value) {
363
- if (props.multiple) {
364
- if (_values === null || _values === void 0 ? void 0 : _values.includes(value)) {
365
- handleOptionSelected(_values.filter((v) => v !== value));
366
- }
367
- else {
368
- handleOptionSelected([...(_values || []), value]);
369
- }
370
- }
371
- else {
372
- handleOptionSelected([value]);
373
- handleClose(undefined, true);
374
- }
375
- }
376
- }
377
- function handleSelectAll(event) {
378
- event.stopPropagation();
379
- if ((_values === null || _values === void 0 ? void 0 : _values.length) === amountOptions) {
380
- handleOptionSelected([]);
381
- }
382
- else {
383
- const searchValue = searchEnabled && searchInputRef.current
384
- ? searchInputRef.current.value
385
- : undefined;
386
- handleOptionSelected(props.options
387
- ? props
388
- .options.filter((option) => {
389
- var _a;
390
- return !option.isGroupTitle &&
391
- (!searchValue ||
392
- ((_a = option.value) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(searchValue.toLowerCase())));
393
- })
394
- .map((option) => { var _a; return (_a = option.value) !== null && _a !== void 0 ? _a : ""; })
395
- : []);
396
- }
397
- }
398
- function handleFocusFirstDropdownCheckbox(activeElement) {
399
- if (detailsRef.current) {
400
- const checkboxes = Array.from(detailsRef.current.querySelectorAll(`input[type="checkbox"],input[type="radio"]`));
401
- if (checkboxes.length) {
402
- const first = checkboxes.at(0);
403
- const checkbox = first === activeElement && checkboxes.length > 1
404
- ? checkboxes.at(1)
405
- : first;
406
- if (checkbox) {
407
- delay(() => {
408
- // Takes some time until element can be focused
409
- checkbox.focus();
410
- }, 1);
411
- }
412
- }
413
- }
414
- }
415
- function handleOpenByKeyboardFocus() {
416
- if (detailsRef.current) {
417
- // Focus search if possible
418
- const search = getSearchInput(detailsRef.current);
419
- if (search) {
420
- delay(() => {
421
- // Takes some time until element can be focused
422
- search.focus();
423
- }, 1);
424
- }
425
- else {
426
- // Focus first checkbox otherwise
427
- handleFocusFirstDropdownCheckbox();
428
- }
429
- }
430
- }
431
- function handleSearch(valueOrEvent) {
432
- if (valueOrEvent === undefined) {
433
- return;
434
- }
435
- let filterText;
436
- if (typeof valueOrEvent === "string") {
437
- filterText = valueOrEvent;
438
- }
439
- else {
440
- const event = valueOrEvent;
441
- event.stopPropagation();
442
- if (props.onSearch) {
443
- props.onSearch(event);
444
- }
445
- filterText = event.target.value;
446
- set_searchValue(filterText);
447
- }
448
- if (!props.options || !filterText || filterText.length === 0) {
449
- set_options(props.options);
450
- }
451
- else if (props.searchFilter) {
452
- set_options(props.options.filter((option) => props.searchFilter(option, filterText)));
453
- }
454
- else {
455
- set_options(props.options.filter((option) => !option.isGroupTitle &&
456
- getOptionLabel(option)
457
- .toLowerCase()
458
- .includes(filterText.toLowerCase())));
459
- }
460
- }
461
- function handleClearAll(event) {
462
- event.stopPropagation();
463
- handleOptionSelected([]);
464
- handleSummaryFocus();
465
- }
466
- function handleSummaryFocus() {
467
- var _a;
468
- if (detailsRef.current) {
469
- (_a = detailsRef.current
470
- .querySelector("summary")) === null || _a === void 0 ? void 0 : _a.focus();
471
- }
472
- }
473
- const [selectAllChecked, setSelectAllChecked] = useState(() => false);
474
- const [selectAllIndeterminate, setSelectAllIndeterminate] = useState(() => false);
475
- function resetIds() {
476
- var _a, _b, _c;
477
- const mId = (_c = (_a = props.id) !== null && _a !== void 0 ? _a : (_b = props.propOverrides) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : `custom-select-${uuid}`;
478
- set_id(mId);
479
- set_messageId(mId + DEFAULT_MESSAGE_ID_SUFFIX);
480
- set_validMessageId(mId + DEFAULT_VALID_MESSAGE_ID_SUFFIX);
481
- set_invalidMessageId(mId + DEFAULT_INVALID_MESSAGE_ID_SUFFIX);
482
- set_selectId(mId + DEFAULT_SELECT_ID_SUFFIX);
483
- set_labelId(mId + DEFAULT_LABEL_ID_SUFFIX);
484
- set_summaryId(mId + "-summary");
485
- set_placeholderId(mId + DEFAULT_PLACEHOLDER_ID_SUFFIX);
486
- set_selectedLabelsId(mId + "-selected-labels");
487
- set_infoTextId(mId + "-info");
488
- }
489
- function satisfyReact(event) {
490
- // This is a function to satisfy React
491
- event.stopPropagation();
492
- }
493
- useEffect(() => {
494
- resetIds();
495
- set_invalidMessage(props.invalidMessage || DEFAULT_INVALID_MESSAGE);
496
- if (typeof window !== "undefined" && "IntersectionObserver" in window) {
497
- set_observer(new IntersectionObserver((payload) => {
498
- if (detailsRef.current) {
499
- const entry = payload.find(({ target }) => target === detailsRef.current);
500
- if (entry && !entry.isIntersecting && detailsRef.current.open) {
501
- detailsRef.current.open = false;
502
- }
503
- }
504
- }));
505
- }
506
- }, []);
507
- useEffect(() => {
508
- var _a, _b;
509
- if ((_a = props.id) !== null && _a !== void 0 ? _a : (_b = props.propOverrides) === null || _b === void 0 ? void 0 : _b.id) {
510
- resetIds();
511
- }
512
- }, [props.id, (_a = props.propOverrides) === null || _a === void 0 ? void 0 : _a.id]);
513
- useEffect(() => {
514
- if (detailsRef.current) {
515
- detailsRef.current.addEventListener("focusout", (event) => handleClose(event));
516
- }
517
- }, [detailsRef.current]);
518
- useEffect(() => {
519
- if (_id) {
520
- const messageId = _id + DEFAULT_MESSAGE_ID_SUFFIX;
521
- set_labelId(_id + DEFAULT_LABEL_ID_SUFFIX);
522
- set_selectId(_id + DEFAULT_SELECT_ID_SUFFIX);
523
- set_validMessageId(_id + DEFAULT_VALID_MESSAGE_ID_SUFFIX);
524
- set_invalidMessageId(_id + DEFAULT_INVALID_MESSAGE_ID_SUFFIX);
525
- set_placeholderId(_id + DEFAULT_PLACEHOLDER_ID_SUFFIX);
526
- if (stringPropVisible(props.message, props.showMessage)) {
527
- setDescById(messageId);
528
- }
529
- else {
530
- setDescById();
531
- }
532
- }
533
- }, [_id]);
534
- useEffect(() => {
535
- var _a;
536
- if (detailsRef.current) {
537
- const summary = detailsRef.current.querySelector("summary");
538
- if (summary) {
539
- summary.setAttribute("aria-describedby", (_a = props.ariaDescribedBy) !== null && _a !== void 0 ? _a : (_descByIds || ""));
540
- }
541
- }
542
- }, [detailsRef.current, _descByIds]);
543
- useEffect(() => {
544
- if (props.showNoResults !== undefined) {
545
- set_hasNoOptions(props.showNoResults);
546
- }
547
- else if (_options) {
548
- set_hasNoOptions(_options.length === 0);
549
- }
550
- }, [props.showNoResults, props.showLoading, _options]);
551
- useEffect(() => {
552
- var _a;
553
- setSelectAllEnabled(Boolean(props.multiple && ((_a = props.showSelectAll) !== null && _a !== void 0 ? _a : amountOptions > 5)));
554
- }, [props.showSelectAll, amountOptions, props.multiple]);
555
- useEffect(() => {
556
- var _a;
557
- setSearchEnabled((_a = props.showSearch) !== null && _a !== void 0 ? _a : amountOptions > 9);
558
- }, [props.showSearch, amountOptions]);
559
- useEffect(() => {
560
- const v = props.values;
561
- if (Array.isArray(v)) {
562
- if (_values !== v) {
563
- set_values(v);
564
- }
565
- }
566
- else if (v == null && (_values === null || _values === void 0 ? void 0 : _values.length) !== 0) {
567
- set_values([]);
568
- }
569
- }, [props.values]);
570
- useEffect(() => {
571
- if (selectRef.current) {
572
- handleValidation();
573
- }
574
- }, [_values, selectRef.current]);
575
- useEffect(() => {
576
- if (selectRef.current) {
577
- let controller = abortController;
578
- if (!controller) {
579
- controller = new AbortController();
580
- setAbortController(controller);
581
- }
582
- const initialValues = props.values;
583
- addResetEventListener(selectRef.current, () => {
584
- const resetValue = initialValues
585
- ? initialValues
586
- : selectRef.current.value
587
- ? [selectRef.current.value]
588
- : [];
589
- handleOptionSelected(resetValue);
590
- handleValidation();
591
- }, controller.signal);
592
- }
593
- }, [selectRef.current]);
594
- useEffect(() => {
595
- set_validity(props.validation);
596
- }, [props.validation]);
597
- useEffect(() => {
598
- if ((_values === null || _values === void 0 ? void 0 : _values.length) === 0) {
599
- setSelectAllChecked(false);
600
- setSelectAllIndeterminate(false);
601
- }
602
- else if ((_values === null || _values === void 0 ? void 0 : _values.length) === amountOptions) {
603
- setSelectAllIndeterminate(false);
604
- setSelectAllChecked(true);
605
- }
606
- else if (_values) {
607
- setSelectAllIndeterminate(true);
608
- }
609
- }, [_values, amountOptions]);
610
- useEffect(() => {
611
- var _a, _b;
612
- set_options(props.options);
613
- setAmountOptions((_b = (_a = props.options) === null || _a === void 0 ? void 0 : _a.filter((option) => !option.isGroupTitle).length) !== null && _b !== void 0 ? _b : 0);
614
- }, [props.options]);
615
- useEffect(() => {
616
- set_searchValue(props.searchValue);
617
- if (props.searchValue) {
618
- const sValue = props.searchValue; // <- workaround for Angular
619
- handleSearch(sValue);
620
- }
621
- }, [props.searchValue]);
622
- useEffect(() => {
623
- var _a, _b;
624
- if ((_a = props.options) === null || _a === void 0 ? void 0 : _a.length) {
625
- set_selectedOptions((_b = props.options) === null || _b === void 0 ? void 0 : _b.filter((option) => {
626
- if (!option.value || !(_values === null || _values === void 0 ? void 0 : _values["includes"])) {
627
- return false;
628
- }
629
- return !option.isGroupTitle && (_values === null || _values === void 0 ? void 0 : _values.includes(option.value));
630
- }));
631
- }
632
- }, [props.options, _values]);
633
- useEffect(() => {
634
- if (props.selectedLabels) {
635
- set_selectedLabels(props.selectedLabels);
636
- return;
637
- }
638
- if (_selectedOptions === null || _selectedOptions === void 0 ? void 0 : _selectedOptions.length) {
639
- if (props.transformSelectedLabels) {
640
- // We need to add this to another ``const`` for Angular generated output to work
641
- const selectedOptions = _selectedOptions;
642
- const transformFn = props.transformSelectedLabels;
643
- set_selectedLabels(transformFn(selectedOptions));
644
- return;
645
- }
646
- if (props.selectedType === "amount") {
647
- set_selectedLabels(props.amountText
648
- ? props.amountText
649
- : `${_selectedOptions === null || _selectedOptions === void 0 ? void 0 : _selectedOptions.length} ${DEFAULT_SELECTED}`);
650
- }
651
- else {
652
- set_selectedLabels(_selectedOptions === null || _selectedOptions === void 0 ? void 0 : _selectedOptions.map((option) => getOptionLabel(option)).join(", "));
653
- }
654
- }
655
- else {
656
- set_selectedLabels("");
657
- }
658
- }, [
659
- _selectedOptions,
660
- props.selectedType,
661
- props.amountText,
662
- props.selectedLabels,
663
- props.transformSelectedLabels,
664
- ]);
665
- useEffect(() => {
666
- var _a;
667
- if (props.onAmountChange) {
668
- props.onAmountChange((_a = _selectedOptions === null || _selectedOptions === void 0 ? void 0 : _selectedOptions.length) !== null && _a !== void 0 ? _a : 0);
669
- }
670
- }, [_selectedOptions]);
671
- useEffect(() => {
672
- if (selectAllRef.current) {
673
- selectAllRef.current.indeterminate = Boolean(selectAllIndeterminate);
674
- }
675
- }, [selectAllIndeterminate, selectAllRef.current]);
676
- useEffect(() => {
677
- var _a;
678
- set_invalidMessage(props.invalidMessage ||
679
- ((_a = selectRef.current) === null || _a === void 0 ? void 0 : _a.validationMessage) ||
680
- DEFAULT_INVALID_MESSAGE);
681
- }, [selectRef.current, props.invalidMessage]);
682
- useEffect(() => {
683
- return () => {
684
- abortController === null || abortController === void 0 ? void 0 : abortController.abort();
685
- };
686
- }, []);
687
- return (React.createElement("div", Object.assign({ id: _id, ref: _ref }, filterPassingProps(props, ["data-icon-variant", "data-icon-variant-before", "data-icon-variant-after", "data-icon-weight", "data-icon-weight-before", "data-icon-weight-after", "data-interactive", "data-force-mobile", "data-color", "data-container-color", "data-bg-color", "data-on-bg-color", "data-color-scheme", "data-font-size", "data-headline-size", "data-divider", "data-focus", "data-font", "data-density", "onOptionSelected", "onAmountChange", "onDropdownToggle"]), getRootProps(props, ["data-icon-variant", "data-icon-variant-before", "data-icon-variant-after", "data-icon-weight", "data-icon-weight-before", "data-icon-weight-after", "data-interactive", "data-force-mobile", "data-color", "data-container-color", "data-bg-color", "data-on-bg-color", "data-color-scheme", "data-font-size", "data-headline-size", "data-divider", "data-focus", "data-font", "data-density"]), { className: cls("db-custom-select", props.className), "aria-invalid": _validity === "invalid", "data-custom-validity": _validity, "data-width": props.formFieldWidth, "data-variant": props.variant === "floating" &&
688
- props.selectedType === "tag" &&
689
- props.multiple
690
- ? "above"
691
- : props.variant, "data-required": getBooleanAsString(props.required), "data-hide-asterisk": getHideProp(props.showRequiredAsterisk), "data-placement": props.placement, "data-selected-type": props.multiple ? props.selectedType : "text", "data-hide-label": getHideProp(props.showLabel), "data-icon": props.icon, "data-show-icon": getBooleanAsString(props.showIcon) }),
692
- React.createElement("label", { id: _labelId }, (_b = props.label) !== null && _b !== void 0 ? _b : DEFAULT_LABEL,
693
- React.createElement("select", { role: "none", hidden: true, id: _selectId, tabIndex: -1, ref: selectRef, form: props.form, name: props.name, "data-custom-validity": _validity, multiple: getBoolean(props.multiple, "multiple"), disabled: getBoolean(props.disabled, "disabled"), required: getBoolean(props.required, "required"), onChange: (event) => satisfyReact(event) }, ((_c = props.options) === null || _c === void 0 ? void 0 : _c.length) ? (React.createElement(React.Fragment, null, (_d = props.options) === null || _d === void 0 ? void 0 : _d.map((option) => (React.createElement("option", { disabled: option.disabled, value: option.value, key: getOptionKey(option, "native-select-option-") }, getOptionLabel(option)))))) : null)),
694
- React.createElement("details", { ref: detailsRef, open: props.open, onToggle: (event) => handleDropdownToggle(event), onKeyDown: (event) => handleKeyboardPress(event) },
695
- props.children,
696
- props.options ? (React.createElement(React.Fragment, null,
697
- React.createElement("summary", { className: "db-custom-select-form-field", id: _summaryId, "aria-disabled": getBooleanAsString(props.disabled), tabIndex: props.disabled ? -1 : undefined, "aria-labelledby": _labelId },
698
- (_selectedLabels === null || _selectedLabels === void 0 ? void 0 : _selectedLabels.length) ? (React.createElement("span", { "data-visually-hidden": getBooleanAsString(props.selectedType === "tag"), id: _selectedLabelsId },
699
- props.selectedPrefix ? (React.createElement("span", { "data-visually-hidden": "true" }, props.selectedPrefix)) : null,
700
- _selectedLabels)) : null,
701
- props.selectedType === "tag" ? (React.createElement("div", null, _selectedOptions === null || _selectedOptions === void 0 ? void 0 : _selectedOptions.map((option) => (React.createElement(DBTag, { emphasis: "strong", behavior: "removable", removeButton: getTagRemoveLabel(option), onRemove: (event) => handleTagRemove(option, event), key: getOptionKey(option, "tag-") }, getOptionLabel(option)))))) : null),
702
- React.createElement(DBCustomSelectDropdown, { width: props.dropdownWidth },
703
- searchEnabled ? (React.createElement("div", null,
704
- React.createElement(DBInput, { type: "search", ref: searchInputRef, name: _id, form: _id, showLabel: false, value: _searchValue, label: (_e = props.searchLabel) !== null && _e !== void 0 ? _e : DEFAULT_LABEL, placeholder: (_f = props.searchPlaceholder) !== null && _f !== void 0 ? _f : props.searchLabel, ariaDescribedBy: _hasNoOptions || props.showLoading
705
- ? _infoTextId
706
- : undefined, onInput: (event) => handleSearch(event) }))) : null,
707
- _hasNoOptions || props.showLoading ? (React.createElement(DBInfotext, { id: _infoTextId, icon: props.showLoading ? "circular_arrows" : undefined, semantic: props.showLoading ? "informational" : "warning" }, (_g = (props.showLoading
708
- ? props.loadingText
709
- : props.noResultsText)) !== null && _g !== void 0 ? _g : DEFAULT_MESSAGE)) : (React.createElement(React.Fragment, null,
710
- React.createElement(React.Fragment, null,
711
- selectAllEnabled ? (React.createElement("div", null,
712
- React.createElement("div", { className: "db-checkbox db-custom-select-list-item" },
713
- React.createElement("label", null,
714
- React.createElement("input", { type: "checkbox", value: "select-all", ref: selectAllRef, form: _id, checked: selectAllChecked, onChange: (event) => handleSelectAll(event) }),
715
- getSelectAllLabel())))) : null,
716
- React.createElement(DBCustomSelectList, { multiple: getBoolean(props.multiple, "multiple"), label: (_j = (_h = props.listLabel) !== null && _h !== void 0 ? _h : props.label) !== null && _j !== void 0 ? _j : DEFAULT_LABEL }, _options === null || _options === void 0 ? void 0 : _options.map((option) => (React.createElement(DBCustomSelectListItem, { type: props.multiple ? "checkbox" : "radio", showDivider: option.showDivider, icon: option.icon, isGroupTitle: option.isGroupTitle, groupTitle: getOptionLabel(option), name: _id, checked: getOptionChecked(option.value), disabled: option.disabled, value: option.value, onChange: (event) => handleSelect(option.value), key: getOptionKey(option, "custom-select-list-item-") }, !option.isGroupTitle ? (React.createElement(React.Fragment, null, getOptionLabel(option))) : null))))))),
717
- React.createElement("div", null,
718
- React.createElement(DBButton, { variant: "ghost", width: "full", icon: "cross", size: "small", name: _id, form: _id, onClick: (event) => handleClose(undefined, true) }, (_k = props.mobileCloseButtonText) !== null && _k !== void 0 ? _k : DEFAULT_CLOSE_BUTTON))))) : null),
719
- ((_l = props.showClearSelection) !== null && _l !== void 0 ? _l : true) && (_values === null || _values === void 0 ? void 0 : _values.length) ? (React.createElement(DBButton, { icon: "cross", variant: "ghost", size: "small", noText: true, name: _id, form: _id, disabled: getBoolean(props.disabled, "disabled"), onClick: (event) => handleClearAll(event) },
720
- props.clearSelectionText,
721
- React.createElement(DBTooltip, { placement: "top" }, props.clearSelectionText))) : null,
722
- React.createElement("span", { className: "db-custom-select-placeholder", "aria-hidden": getBooleanAsString(true), id: _placeholderId }, (_m = props.placeholder) !== null && _m !== void 0 ? _m : props.label),
723
- stringPropVisible(props.message, props.showMessage) ? (React.createElement(DBInfotext, { size: "small", icon: props.messageIcon, id: _messageId }, props.message)) : null,
724
- hasValidState() ? (React.createElement(DBInfotext, { size: "small", semantic: "successful", id: _validMessageId }, props.validMessage || DEFAULT_VALID_MESSAGE)) : null,
725
- React.createElement(DBInfotext, { size: "small", semantic: "critical", id: _invalidMessageId }, _invalidMessage),
726
- React.createElement("span", { "data-visually-hidden": "true", role: "status" }, _voiceOverFallback)));
2
+ import { filterPassingProps as e, getRootProps as t } from "../../utils/react.js";
3
+ import { cls as n, delay as r, getBoolean as i, getBooleanAsString as a, getHideProp as ee, getOptionKey as o, getSearchInput as te, hasVoiceOver as ne, stringPropVisible as s } from "../../utils/index.js";
4
+ import { DEFAULT_INVALID_MESSAGE_ID_SUFFIX as re, DEFAULT_LABEL_ID_SUFFIX as ie, DEFAULT_MESSAGE_ID_SUFFIX as ae, DEFAULT_PLACEHOLDER_ID_SUFFIX as c, DEFAULT_REMOVE as oe, DEFAULT_SELECTED as se, DEFAULT_SELECT_ID_SUFFIX as ce, DEFAULT_VALID_MESSAGE_ID_SUFFIX as le } from "../../shared/constants.js";
5
+ import ue from "../button/button.js";
6
+ import { addResetEventListener as de } from "../../utils/form-components.js";
7
+ import l from "../infotext/infotext.js";
8
+ import { DocumentClickListener as fe } from "../../utils/document-click-listener.js";
9
+ import { DocumentScrollListener as u } from "../../utils/document-scroll-listener.js";
10
+ import { handleFixedDropdown as pe } from "../../utils/floating-components.js";
11
+ import me from "../custom-select-dropdown/custom-select-dropdown.js";
12
+ import he from "../custom-select-list-item/custom-select-list-item.js";
13
+ import ge from "../custom-select-list/custom-select-list.js";
14
+ import _e from "../input/input.js";
15
+ import ve from "../tooltip/tooltip.js";
16
+ import ye from "../tag/tag.js";
17
+ import { forwardRef as d, useEffect as f, useId as be, useRef as p, useState as m } from "react";
18
+ import { Fragment as h, jsx as g, jsxs as _ } from "react/jsx-runtime";
19
+ //#region src/components/custom-select/custom-select.tsx
20
+ function v(d, v) {
21
+ d = {
22
+ clearSelectionText: "Clear selection",
23
+ showClearSelection: !0,
24
+ ...d
25
+ };
26
+ let xe = be(), Se = v || p(v), y = p(null), b = p(null), x = p(null), S = p(null), [C, Ce] = m(() => void 0), [w, we] = m(() => void 0), [T, E] = m(() => void 0), [D, O] = m(() => void 0), [k, A] = m(() => void 0), [Te, j] = m(() => void 0), [M, Ee] = m(() => void 0), [De, Oe] = m(() => void 0), [ke, Ae] = m(() => void 0), [je, Me] = m(() => void 0), [N, P] = m(() => "no-validation"), [Ne, Pe] = m(() => !1), [Fe, Ie] = m(() => void 0), [Le, Re] = m(() => void 0), [F, I] = m(() => ""), [L, ze] = m(() => void 0), [Be, R] = m(() => ""), [z, Ve] = m(() => []), [He, Ue] = m(() => !1), [We, Ge] = m(() => !1), [B, Ke] = m(() => 0), [V, H] = m(() => []), [U, W] = m(() => []), [qe, Je] = m(() => !1), [Ye, Xe] = m(() => void 0), [Ze, Qe] = m(() => 0), [$e, et] = m(() => void 0), [tt, nt] = m(() => void 0);
27
+ function rt(e) {
28
+ e?.target?.contains && e?.target?.contains(y.current) && q();
29
+ }
30
+ let [it, at] = m(() => void 0);
31
+ function ot() {
32
+ return !!(d.validMessage ?? d.validation === "valid");
33
+ }
34
+ function st() {
35
+ b.current && (b.current.value = lt()), !b.current?.validity.valid || d.validation === "invalid" ? (G(D), A(d.invalidMessage || b.current?.validationMessage || "TODO: Add an invalidMessage"), ne() && (R(k), r(() => R(""), 1e3)), Ne && P(d.validation ?? "invalid")) : ot() && b.current?.validity.valid && d.required ? (G(T), ne() && (R(d.validMessage ?? "TODO: Add a validMessage"), r(() => R(""), 1e3)), P(d.validation ?? "valid")) : s(d.message, d.showMessage) ? (G(w), P(d.validation ?? "no-validation")) : (G(ke), P(d.validation ?? "no-validation"));
36
+ }
37
+ function ct(e) {
38
+ d.onDropdownToggle && (e.stopPropagation(), d.onDropdownToggle(e)), e.target instanceof HTMLDetailsElement && e.target.open ? (Xe(new fe().addCallback((e) => gt(e))), et(new u().addCallback((e) => rt(e))), q(), tt?.observe(y.current), e.target.dataset.test || vt()) : (Ye && new fe().removeCallback(Ye), $e && new u().removeCallback($e), tt?.unobserve(y.current));
39
+ }
40
+ function lt() {
41
+ return V?.length ? V.at(0) ?? "" : "";
42
+ }
43
+ function G(e) {
44
+ let t = [];
45
+ e && t.push(e), L && F?.length && t.push(L), Re(t.join(" "));
46
+ }
47
+ function ut() {
48
+ return d.selectAllLabel ?? "LABEL SHOULD BE SET";
49
+ }
50
+ function K(e) {
51
+ return e.label ?? e.value?.toString() ?? "";
52
+ }
53
+ function dt(e) {
54
+ return e && V?.includes ? V?.includes(e) : !1;
55
+ }
56
+ function ft(e) {
57
+ let t = d.removeTagsTexts, n = d.options;
58
+ if (t && n) {
59
+ let r = n.findIndex((t) => t.value === e.value);
60
+ if (r >= 0 && r < t.length) return t[r];
61
+ }
62
+ return `${oe} ${K(e)}`;
63
+ }
64
+ function pt(e, t) {
65
+ t && t.stopPropagation(), X(e.value), Q();
66
+ }
67
+ function q() {
68
+ if (y.current) {
69
+ let e = y.current.querySelector("article");
70
+ e && r(() => {
71
+ pe(e, y.current, d.placement ?? "bottom");
72
+ }, 1);
73
+ }
74
+ }
75
+ function mt(e) {
76
+ if (y.current?.open) {
77
+ if (self.document) {
78
+ let t = self.document.activeElement;
79
+ if (t) if (t.getAttribute("type") === "checkbox" || t.getAttribute("type") === "radio") {
80
+ let n = t?.closest("li");
81
+ if (e.key === "ArrowDown" || e.key === "ArrowRight") {
82
+ let e = n?.nextElementSibling;
83
+ for (; e;) {
84
+ let t = e.querySelector("input");
85
+ if (t) {
86
+ t.focus();
87
+ break;
88
+ }
89
+ e = e.nextElementSibling;
90
+ }
91
+ e || Z(t);
92
+ } else {
93
+ let e = n?.previousElementSibling;
94
+ for (; e;) {
95
+ let t = e.querySelector("input");
96
+ if (t) {
97
+ t.focus();
98
+ break;
99
+ }
100
+ e = e.previousElementSibling;
101
+ }
102
+ if (!e) {
103
+ let e = y.current.querySelector("input[type=\"checkbox\"]");
104
+ if (e && e !== t) Z(t);
105
+ else {
106
+ let e = te(y.current);
107
+ if (e) r(() => {
108
+ e.focus();
109
+ }, 100);
110
+ else {
111
+ let e = Array.from(y.current?.querySelectorAll("input[type=\"checkbox\"],input[type=\"radio\"]"));
112
+ e.length && e.at(-1)?.focus();
113
+ }
114
+ }
115
+ }
116
+ }
117
+ } else t.getAttribute("type") === "search" && (e.key === "ArrowUp" || e.key === "ArrowLeft") ? (J(void 0, !0), Q()) : Z(t);
118
+ }
119
+ } else (e.key === "ArrowDown" || e.key === "ArrowRight") && (q(), y.current && (y.current.open = !0), vt());
120
+ e.stopPropagation(), e.preventDefault();
121
+ }
122
+ function ht(e) {
123
+ if (e.stopPropagation(), e.key === "Escape" && y.current?.open) J(void 0, !0), Q();
124
+ else if (e.key === "Enter" && y.current?.open) {
125
+ if (self.document) {
126
+ let t = self.document.activeElement;
127
+ if (["checkbox", "radio"].includes(t.getAttribute("type") || "")) t.click(), e.preventDefault();
128
+ else if (t.getAttribute("type") === "search") {
129
+ let t = U?.find((e) => !e.isGroupTitle && !e.disabled);
130
+ t?.value && (X(t.value), e.preventDefault());
131
+ }
132
+ }
133
+ } else (e.key === "ArrowDown" || e.key === "ArrowUp" || e.key === "ArrowLeft" || e.key === "ArrowRight") && mt(e);
134
+ }
135
+ function J(e, t) {
136
+ if (y.current) {
137
+ if (t) y.current.open = !1, Q();
138
+ else if (y.current.open && e && e.relatedTarget) {
139
+ let t = e.relatedTarget;
140
+ !y.current.contains(t) && t.localName !== "dialog" && r(() => y.current.open = !1, 1);
141
+ }
142
+ }
143
+ }
144
+ function gt(e) {
145
+ if (e) {
146
+ let t = e.target;
147
+ y.current?.open && !y.current.contains(t) && (y.current.open = !1);
148
+ }
149
+ }
150
+ function Y(e) {
151
+ (/* @__PURE__ */ new Date()).getTime() - Ze < 200 || (H(e), Pe(!0), d.onOptionSelected && d.onOptionSelected(e ?? []), Qe((/* @__PURE__ */ new Date()).getTime()));
152
+ }
153
+ function X(e) {
154
+ e && (d.multiple ? V?.includes(e) ? Y(V.filter((t) => t !== e)) : Y([...V || [], e]) : (Y([e]), J(void 0, !0)));
155
+ }
156
+ function _t(e) {
157
+ if (e.stopPropagation(), V?.length === B) Y([]);
158
+ else {
159
+ let e = We && S.current ? S.current.value : void 0;
160
+ Y(d.options ? d.options.filter((t) => !t.isGroupTitle && (!e || t.value?.toLowerCase().includes(e.toLowerCase()))).map((e) => e.value ?? "") : []);
161
+ }
162
+ }
163
+ function Z(e) {
164
+ if (y.current) {
165
+ let t = Array.from(y.current.querySelectorAll("input[type=\"checkbox\"],input[type=\"radio\"]"));
166
+ if (t.length) {
167
+ let n = t.at(0), i = n === e && t.length > 1 ? t.at(1) : n;
168
+ i && r(() => {
169
+ i.focus();
170
+ }, 1);
171
+ }
172
+ }
173
+ }
174
+ function vt() {
175
+ if (y.current) {
176
+ let e = te(y.current);
177
+ e ? r(() => {
178
+ e.focus();
179
+ }, 1) : Z();
180
+ }
181
+ }
182
+ function yt(e) {
183
+ if (e === void 0) return;
184
+ let t;
185
+ if (typeof e == "string") t = e;
186
+ else {
187
+ let n = e;
188
+ n.stopPropagation(), d.onSearch && d.onSearch(n), t = n.target.value, at(t);
189
+ }
190
+ !d.options || !t || t.length === 0 ? W(d.options) : d.searchFilter ? W(d.options.filter((e) => d.searchFilter(e, t))) : W(d.options.filter((e) => !e.isGroupTitle && K(e).toLowerCase().includes(t.toLowerCase())));
191
+ }
192
+ function bt(e) {
193
+ e.stopPropagation(), Y([]), Q();
194
+ }
195
+ function Q() {
196
+ y.current && y.current.querySelector("summary")?.focus();
197
+ }
198
+ let [xt, St] = m(() => !1), [Ct, $] = m(() => !1);
199
+ function wt() {
200
+ let e = d.id ?? d.propOverrides?.id ?? `custom-select-${xe}`;
201
+ Ce(e), we(e + ae), E(e + le), O(e + re), j(e + ce), Ee(e + ie), Oe(e + "-summary"), Ae(e + c), ze(e + "-selected-labels"), Me(e + "-info");
202
+ }
203
+ function Tt(e) {
204
+ e.stopPropagation();
205
+ }
206
+ return f(() => {
207
+ wt(), A(d.invalidMessage || "TODO: Add an invalidMessage"), typeof window < "u" && "IntersectionObserver" in window && nt(new IntersectionObserver((e) => {
208
+ if (y.current) {
209
+ let t = e.find(({ target: e }) => e === y.current);
210
+ t && !t.isIntersecting && y.current.open && (y.current.open = !1);
211
+ }
212
+ }));
213
+ }, []), f(() => {
214
+ (d.id ?? d.propOverrides?.id) && wt();
215
+ }, [d.id, d.propOverrides?.id]), f(() => {
216
+ y.current && y.current.addEventListener("focusout", (e) => J(e));
217
+ }, [y.current]), f(() => {
218
+ if (C) {
219
+ let e = C + ae;
220
+ Ee(C + ie), j(C + ce), E(C + le), O(C + re), Ae(C + c), s(d.message, d.showMessage) ? G(e) : G();
221
+ }
222
+ }, [C]), f(() => {
223
+ if (y.current) {
224
+ let e = y.current.querySelector("summary");
225
+ e && e.setAttribute("aria-describedby", d.ariaDescribedBy ?? (Le || ""));
226
+ }
227
+ }, [y.current, Le]), f(() => {
228
+ d.showNoResults === void 0 ? U && Je(U.length === 0) : Je(d.showNoResults);
229
+ }, [
230
+ d.showNoResults,
231
+ d.showLoading,
232
+ U
233
+ ]), f(() => {
234
+ Ue(!!(d.multiple && (d.showSelectAll ?? B > 5)));
235
+ }, [
236
+ d.showSelectAll,
237
+ B,
238
+ d.multiple
239
+ ]), f(() => {
240
+ Ge(d.showSearch ?? B > 9);
241
+ }, [d.showSearch, B]), f(() => {
242
+ let e = d.values;
243
+ Array.isArray(e) ? V !== e && H(e) : e == null && V?.length !== 0 && H([]);
244
+ }, [d.values]), f(() => {
245
+ b.current && st();
246
+ }, [V, b.current]), f(() => {
247
+ if (b.current) {
248
+ let e = Fe;
249
+ e || (e = new AbortController(), Ie(e));
250
+ let t = d.values;
251
+ de(b.current, () => {
252
+ Y(t || (b.current.value ? [b.current.value] : [])), st();
253
+ }, e.signal);
254
+ }
255
+ }, [b.current]), f(() => {
256
+ P(d.validation);
257
+ }, [d.validation]), f(() => {
258
+ V?.length === 0 ? (St(!1), $(!1)) : V?.length === B ? ($(!1), St(!0)) : V && $(!0);
259
+ }, [V, B]), f(() => {
260
+ W(d.options), Ke(d.options?.filter((e) => !e.isGroupTitle).length ?? 0);
261
+ }, [d.options]), f(() => {
262
+ if (at(d.searchValue), d.searchValue) {
263
+ let e = d.searchValue;
264
+ yt(e);
265
+ }
266
+ }, [d.searchValue]), f(() => {
267
+ d.options?.length && Ve(d.options?.filter((e) => !e.value || !V?.includes ? !1 : !e.isGroupTitle && V?.includes(e.value)));
268
+ }, [d.options, V]), f(() => {
269
+ if (d.selectedLabels) {
270
+ I(d.selectedLabels);
271
+ return;
272
+ }
273
+ if (z?.length) {
274
+ if (d.transformSelectedLabels) {
275
+ let e = z, t = d.transformSelectedLabels;
276
+ I(t(e));
277
+ return;
278
+ }
279
+ d.selectedType === "amount" ? I(d.amountText ? d.amountText : `${z?.length} ${se}`) : I(z?.map((e) => K(e)).join(", "));
280
+ } else I("");
281
+ }, [
282
+ z,
283
+ d.selectedType,
284
+ d.amountText,
285
+ d.selectedLabels,
286
+ d.transformSelectedLabels
287
+ ]), f(() => {
288
+ d.onAmountChange && d.onAmountChange(z?.length ?? 0);
289
+ }, [z]), f(() => {
290
+ x.current && (x.current.indeterminate = !!Ct);
291
+ }, [Ct, x.current]), f(() => {
292
+ A(d.invalidMessage || b.current?.validationMessage || "TODO: Add an invalidMessage");
293
+ }, [b.current, d.invalidMessage]), f(() => () => {
294
+ Fe?.abort();
295
+ }, []), /* @__PURE__ */ _("div", {
296
+ id: C,
297
+ ref: Se,
298
+ ...e(d, [
299
+ "data-icon-variant",
300
+ "data-icon-variant-before",
301
+ "data-icon-variant-after",
302
+ "data-icon-weight",
303
+ "data-icon-weight-before",
304
+ "data-icon-weight-after",
305
+ "data-interactive",
306
+ "data-force-mobile",
307
+ "data-color",
308
+ "data-container-color",
309
+ "data-bg-color",
310
+ "data-on-bg-color",
311
+ "data-color-scheme",
312
+ "data-font-size",
313
+ "data-headline-size",
314
+ "data-divider",
315
+ "data-focus",
316
+ "data-font",
317
+ "data-density",
318
+ "onOptionSelected",
319
+ "onAmountChange",
320
+ "onDropdownToggle"
321
+ ]),
322
+ ...t(d, [
323
+ "data-icon-variant",
324
+ "data-icon-variant-before",
325
+ "data-icon-variant-after",
326
+ "data-icon-weight",
327
+ "data-icon-weight-before",
328
+ "data-icon-weight-after",
329
+ "data-interactive",
330
+ "data-force-mobile",
331
+ "data-color",
332
+ "data-container-color",
333
+ "data-bg-color",
334
+ "data-on-bg-color",
335
+ "data-color-scheme",
336
+ "data-font-size",
337
+ "data-headline-size",
338
+ "data-divider",
339
+ "data-focus",
340
+ "data-font",
341
+ "data-density"
342
+ ]),
343
+ className: n("db-custom-select", d.className),
344
+ "aria-invalid": N === "invalid",
345
+ "data-custom-validity": N,
346
+ "data-width": d.formFieldWidth,
347
+ "data-variant": d.variant === "floating" && d.selectedType === "tag" && d.multiple ? "above" : d.variant,
348
+ "data-required": a(d.required),
349
+ "data-hide-asterisk": ee(d.showRequiredAsterisk),
350
+ "data-placement": d.placement,
351
+ "data-selected-type": d.multiple ? d.selectedType : "text",
352
+ "data-hide-label": ee(d.showLabel),
353
+ "data-icon": d.icon,
354
+ "data-show-icon": a(d.showIcon),
355
+ children: [
356
+ /* @__PURE__ */ _("label", {
357
+ id: M,
358
+ children: [d.label ?? "LABEL SHOULD BE SET", /* @__PURE__ */ g("select", {
359
+ role: "none",
360
+ hidden: !0,
361
+ id: Te,
362
+ tabIndex: -1,
363
+ ref: b,
364
+ form: d.form,
365
+ name: d.name,
366
+ "data-custom-validity": N,
367
+ multiple: i(d.multiple, "multiple"),
368
+ disabled: i(d.disabled, "disabled"),
369
+ required: i(d.required, "required"),
370
+ onChange: (e) => Tt(e),
371
+ children: d.options?.length ? /* @__PURE__ */ g(h, { children: d.options?.map((e) => /* @__PURE__ */ g("option", {
372
+ disabled: e.disabled,
373
+ value: e.value,
374
+ children: K(e)
375
+ }, o(e, "native-select-option-"))) }) : null
376
+ })]
377
+ }),
378
+ /* @__PURE__ */ _("details", {
379
+ ref: y,
380
+ open: d.open,
381
+ onToggle: (e) => ct(e),
382
+ onKeyDown: (e) => ht(e),
383
+ children: [d.children, d.options ? /* @__PURE__ */ _(h, { children: [/* @__PURE__ */ _("summary", {
384
+ className: "db-custom-select-form-field",
385
+ id: De,
386
+ "aria-disabled": a(d.disabled),
387
+ tabIndex: d.disabled ? -1 : void 0,
388
+ "aria-labelledby": M,
389
+ children: [F?.length ? /* @__PURE__ */ _("span", {
390
+ "data-visually-hidden": a(d.selectedType === "tag"),
391
+ id: L,
392
+ children: [d.selectedPrefix ? /* @__PURE__ */ g("span", {
393
+ "data-visually-hidden": "true",
394
+ children: d.selectedPrefix
395
+ }) : null, F]
396
+ }) : null, d.selectedType === "tag" ? /* @__PURE__ */ g("div", { children: z?.map((e) => /* @__PURE__ */ g(ye, {
397
+ emphasis: "strong",
398
+ behavior: "removable",
399
+ removeButton: ft(e),
400
+ onRemove: (t) => pt(e, t),
401
+ children: K(e)
402
+ }, o(e, "tag-"))) }) : null]
403
+ }), /* @__PURE__ */ _(me, {
404
+ width: d.dropdownWidth,
405
+ children: [
406
+ We ? /* @__PURE__ */ g("div", { children: /* @__PURE__ */ g(_e, {
407
+ type: "search",
408
+ ref: S,
409
+ name: C,
410
+ form: C,
411
+ showLabel: !1,
412
+ value: it,
413
+ label: d.searchLabel ?? "LABEL SHOULD BE SET",
414
+ placeholder: d.searchPlaceholder ?? d.searchLabel,
415
+ ariaDescribedBy: qe || d.showLoading ? je : void 0,
416
+ onInput: (e) => yt(e)
417
+ }) }) : null,
418
+ qe || d.showLoading ? /* @__PURE__ */ g(l, {
419
+ id: je,
420
+ icon: d.showLoading ? "circular_arrows" : void 0,
421
+ semantic: d.showLoading ? "informational" : "warning",
422
+ children: (d.showLoading ? d.loadingText : d.noResultsText) ?? "MESSAGE SHOULD BE SET"
423
+ }) : /* @__PURE__ */ g(h, { children: /* @__PURE__ */ _(h, { children: [He ? /* @__PURE__ */ g("div", { children: /* @__PURE__ */ g("div", {
424
+ className: "db-checkbox db-custom-select-list-item",
425
+ children: /* @__PURE__ */ _("label", { children: [/* @__PURE__ */ g("input", {
426
+ type: "checkbox",
427
+ value: "select-all",
428
+ ref: x,
429
+ form: C,
430
+ checked: xt,
431
+ onChange: (e) => _t(e)
432
+ }), ut()] })
433
+ }) }) : null, /* @__PURE__ */ g(ge, {
434
+ multiple: i(d.multiple, "multiple"),
435
+ label: d.listLabel ?? d.label ?? "LABEL SHOULD BE SET",
436
+ children: U?.map((e) => /* @__PURE__ */ g(he, {
437
+ type: d.multiple ? "checkbox" : "radio",
438
+ showDivider: e.showDivider,
439
+ icon: e.icon,
440
+ isGroupTitle: e.isGroupTitle,
441
+ groupTitle: K(e),
442
+ name: C,
443
+ checked: dt(e.value),
444
+ disabled: e.disabled,
445
+ value: e.value,
446
+ onChange: (t) => X(e.value),
447
+ children: e.isGroupTitle ? null : /* @__PURE__ */ g(h, { children: K(e) })
448
+ }, o(e, "custom-select-list-item-")))
449
+ })] }) }),
450
+ /* @__PURE__ */ g("div", { children: /* @__PURE__ */ g(ue, {
451
+ variant: "ghost",
452
+ width: "full",
453
+ icon: "cross",
454
+ size: "small",
455
+ name: C,
456
+ form: C,
457
+ onClick: (e) => J(void 0, !0),
458
+ children: d.mobileCloseButtonText ?? "Close"
459
+ }) })
460
+ ]
461
+ })] }) : null]
462
+ }),
463
+ (d.showClearSelection ?? !0) && V?.length ? /* @__PURE__ */ _(ue, {
464
+ icon: "cross",
465
+ variant: "ghost",
466
+ size: "small",
467
+ noText: !0,
468
+ name: C,
469
+ form: C,
470
+ disabled: i(d.disabled, "disabled"),
471
+ onClick: (e) => bt(e),
472
+ children: [d.clearSelectionText, /* @__PURE__ */ g(ve, {
473
+ placement: "top",
474
+ children: d.clearSelectionText
475
+ })]
476
+ }) : null,
477
+ /* @__PURE__ */ g("span", {
478
+ className: "db-custom-select-placeholder",
479
+ "aria-hidden": a(!0),
480
+ id: ke,
481
+ children: d.placeholder ?? d.label
482
+ }),
483
+ s(d.message, d.showMessage) ? /* @__PURE__ */ g(l, {
484
+ size: "small",
485
+ icon: d.messageIcon,
486
+ id: w,
487
+ children: d.message
488
+ }) : null,
489
+ ot() ? /* @__PURE__ */ g(l, {
490
+ size: "small",
491
+ semantic: "successful",
492
+ id: T,
493
+ children: d.validMessage || "TODO: Add a validMessage"
494
+ }) : null,
495
+ /* @__PURE__ */ g(l, {
496
+ size: "small",
497
+ semantic: "critical",
498
+ id: D,
499
+ children: k
500
+ }),
501
+ /* @__PURE__ */ g("span", {
502
+ "data-visually-hidden": "true",
503
+ role: "status",
504
+ children: Be
505
+ })
506
+ ]
507
+ });
727
508
  }
728
- const DBCustomSelect = forwardRef(DBCustomSelectFn);
729
- export default DBCustomSelect;
509
+ var xe = d(v);
510
+ //#endregion
511
+ export { xe as default };