@jobber/components 6.85.2 → 6.86.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/Autocomplete/Autocomplete.d.ts +3 -3
- package/dist/Autocomplete/Autocomplete.rebuilt.d.ts +8 -0
- package/dist/Autocomplete/Autocomplete.types.d.ts +389 -4
- package/dist/Autocomplete/components/MenuList.d.ts +37 -0
- package/dist/Autocomplete/components/PersistentRegion.d.ts +18 -0
- package/dist/Autocomplete/hooks/useAutocompleteListNav.d.ts +22 -0
- package/dist/Autocomplete/index.cjs +1330 -37
- package/dist/Autocomplete/index.d.ts +31 -1
- package/dist/Autocomplete/index.mjs +1322 -21
- package/dist/Autocomplete/tests/Autocomplete.setup.d.ts +38 -0
- package/dist/Autocomplete/useAutocomplete.d.ts +66 -0
- package/dist/Autocomplete/utils/menuModel.d.ts +14 -0
- package/dist/floating-ui.react-cjs.js +988 -0
- package/dist/floating-ui.react-es.js +987 -1
- package/dist/index.cjs +16 -15
- package/dist/index.mjs +12 -11
- package/dist/styles.css +443 -315
- package/package.json +2 -2
- package/dist/Autocomplete-cjs.js +0 -357
- package/dist/Autocomplete-es.js +0 -344
|
@@ -1,28 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
import '../
|
|
3
|
-
import '
|
|
4
|
-
import '
|
|
5
|
-
import '
|
|
6
|
-
import '../
|
|
7
|
-
import '../
|
|
8
|
-
import '../
|
|
1
|
+
import React__default, { useState, useRef, useEffect, useCallback, useMemo, forwardRef } from 'react';
|
|
2
|
+
import { u as useFloating, b as autoUpdate, o as offset, f as flip, e as size, r as useListNavigation, c as useDismiss, d as useInteractions, t as useTransitionStyles, F as FloatingPortal, q as FloatingFocusManager } from '../floating-ui.react-es.js';
|
|
3
|
+
import classnames from 'classnames';
|
|
4
|
+
import { tokens } from '@jobber/design';
|
|
5
|
+
import { useCallbackRef, useDebounce } from '@jobber/hooks';
|
|
6
|
+
import { c as calculateMaxHeight } from '../maxHeight-es.js';
|
|
7
|
+
import { H as Heading } from '../Heading-es.js';
|
|
8
|
+
import { T as Text } from '../Text-es.js';
|
|
9
|
+
import { T as Typography } from '../Typography-es.js';
|
|
10
|
+
import { I as Icon } from '../Icon-es.js';
|
|
11
|
+
import { InputText } from '../InputText/index.mjs';
|
|
12
|
+
import { G as Glimmer } from '../Glimmer-es.js';
|
|
13
|
+
import { m as mergeRefs } from '../FormField-es.js';
|
|
14
|
+
import { _ as __rest, a as __awaiter } from '../tslib.es6-es.js';
|
|
15
|
+
import { u as useDebounce_2 } from '../useDebounce-es.js';
|
|
16
|
+
import { u as useIsMounted_2 } from '../useIsMounted-es.js';
|
|
17
|
+
import { u as useSafeLayoutEffect_1 } from '../useSafeLayoutEffect-es.js';
|
|
18
|
+
import { u as useOnKeyDown_2 } from '../useOnKeyDown-es.js';
|
|
9
19
|
import 'react-dom';
|
|
10
|
-
import '
|
|
11
|
-
import '@jobber/design';
|
|
12
|
-
import '../Heading-es.js';
|
|
13
|
-
import '../Typography-es.js';
|
|
14
|
-
import '../Text-es.js';
|
|
15
|
-
import '../Icon-es.js';
|
|
16
|
-
import '../useOnKeyDown-es.js';
|
|
17
|
-
import '../InputText/index.mjs';
|
|
18
|
-
import '../FormField-es.js';
|
|
20
|
+
import 'react-hook-form';
|
|
19
21
|
import 'framer-motion';
|
|
20
22
|
import '../Button-es.js';
|
|
21
23
|
import 'react-router-dom';
|
|
22
|
-
import '../useFormFieldFocus-es.js';
|
|
23
|
-
import '../InputValidation-es.js';
|
|
24
|
-
import '../Spinner-es.js';
|
|
25
|
-
import 'react-hook-form';
|
|
26
24
|
import '../omit-es.js';
|
|
27
25
|
import '../_commonjsHelpers-es.js';
|
|
28
26
|
import '../_baseGet-es.js';
|
|
@@ -35,3 +33,1306 @@ import '../keysIn-es.js';
|
|
|
35
33
|
import '../_baseAssignValue-es.js';
|
|
36
34
|
import '../_baseFlatten-es.js';
|
|
37
35
|
import '../_setToString-es.js';
|
|
36
|
+
import '../useFormFieldFocus-es.js';
|
|
37
|
+
import '../InputValidation-es.js';
|
|
38
|
+
import '../Spinner-es.js';
|
|
39
|
+
import '../Content-es.js';
|
|
40
|
+
|
|
41
|
+
var styles$1 = {"list":"_37kZB-nYE08-","loadingList":"ULib3TUQja0-","option":"h5-1Pp0eRyo-","defaultOptionContent":"iBkyj85vd-E-","icon":"I6wAbSJJHNQ-","optionActive":"_4yhnonWAWRY-","actionActive":"oGLMF6n8C74-","action":"LBrwH6TEUYA-","section":"DDOv4DR8bJQ-","emptyStateMessage":"Twgjn26oldE-","stickyTop":"mc1-CEwZtHE-","scrollRegion":"kOR88SFNOn0-","persistentHeader":"_0O-kEf3h9ZI-","persistentFooter":"rQ9ZS7Rb7Z4-","textPersistent":"vxk57ZhP8GU-","spinning":"_0d8hyvaCPAw-"};
|
|
42
|
+
|
|
43
|
+
function flattenMenu(menu) {
|
|
44
|
+
const optionItems = [];
|
|
45
|
+
const sections = [];
|
|
46
|
+
const persistentsHeaders = [];
|
|
47
|
+
const persistentsFooters = [];
|
|
48
|
+
menu.forEach(item => {
|
|
49
|
+
if (item.type === "header") {
|
|
50
|
+
persistentsHeaders.push(item);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (item.type === "footer") {
|
|
54
|
+
persistentsFooters.push(item);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const group = item;
|
|
58
|
+
sections.push(group);
|
|
59
|
+
optionItems.push(...group.options);
|
|
60
|
+
});
|
|
61
|
+
return { optionItems, sections, persistentsHeaders, persistentsFooters };
|
|
62
|
+
}
|
|
63
|
+
function buildItemsForGroup(group, optionsFilter) {
|
|
64
|
+
var _a;
|
|
65
|
+
const isSection = group.type === "section";
|
|
66
|
+
const filtered = optionsFilter ? optionsFilter(group.options) : group.options;
|
|
67
|
+
const actions = (_a = group.actions) !== null && _a !== void 0 ? _a : [];
|
|
68
|
+
const result = [];
|
|
69
|
+
const sectionHasContent = isSection && (filtered.length > 0 || actions.length > 0);
|
|
70
|
+
if (sectionHasContent) {
|
|
71
|
+
result.push({
|
|
72
|
+
kind: "section",
|
|
73
|
+
section: group,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (filtered.length > 0) {
|
|
77
|
+
result.push(...filtered.map(o => ({
|
|
78
|
+
kind: "option",
|
|
79
|
+
value: o,
|
|
80
|
+
})));
|
|
81
|
+
}
|
|
82
|
+
if (actions.length > 0) {
|
|
83
|
+
result.push(...actions.map(action => ({
|
|
84
|
+
kind: "action",
|
|
85
|
+
action,
|
|
86
|
+
origin: "menu",
|
|
87
|
+
})));
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
function buildRenderableList(sections, optionsFilter) {
|
|
92
|
+
const items = [];
|
|
93
|
+
for (const group of sections) {
|
|
94
|
+
items.push(...buildItemsForGroup(group, optionsFilter));
|
|
95
|
+
}
|
|
96
|
+
return items;
|
|
97
|
+
}
|
|
98
|
+
function getNavigableItemAtIndex(activeIndex, renderable) {
|
|
99
|
+
if (activeIndex == null)
|
|
100
|
+
return null;
|
|
101
|
+
let navigableIndex = -1;
|
|
102
|
+
for (const item of renderable) {
|
|
103
|
+
// Ignore sections
|
|
104
|
+
if (item.kind === "section")
|
|
105
|
+
continue;
|
|
106
|
+
navigableIndex += 1;
|
|
107
|
+
if (navigableIndex === activeIndex)
|
|
108
|
+
return item;
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
function findNavigableIndexForValue(renderable, equals, selectedValue) {
|
|
113
|
+
let navigableIndex = -1;
|
|
114
|
+
for (const item of renderable) {
|
|
115
|
+
// Ignore sections
|
|
116
|
+
if (item.kind === "section")
|
|
117
|
+
continue;
|
|
118
|
+
navigableIndex += 1;
|
|
119
|
+
if (item.kind === "option" && equals(item.value, selectedValue)) {
|
|
120
|
+
return navigableIndex;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
function invokeActiveItemOnEnter(event, activeIndex, renderable, onSelect, onAction) {
|
|
126
|
+
const activeItem = getNavigableItemAtIndex(activeIndex, renderable);
|
|
127
|
+
if (!activeItem)
|
|
128
|
+
return;
|
|
129
|
+
event.preventDefault();
|
|
130
|
+
if (activeItem.kind === "option") {
|
|
131
|
+
onSelect(activeItem.value);
|
|
132
|
+
}
|
|
133
|
+
else if (activeItem.kind === "action") {
|
|
134
|
+
onAction({
|
|
135
|
+
run: activeItem.action.onClick,
|
|
136
|
+
closeOnRun: activeItem.action.shouldClose,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const MENU_OFFSET = tokens["space-small"];
|
|
142
|
+
const AUTOCOMPLETE_MAX_HEIGHT$1 = 300;
|
|
143
|
+
function useAutocompleteListNav({ navigableCount, shouldResetActiveIndexOnClose, onMenuClose, selectedIndex, }) {
|
|
144
|
+
const [open, setOpen] = useState(false);
|
|
145
|
+
const [activeIndex, setActiveIndex] = useState(null);
|
|
146
|
+
const listRef = useRef([]);
|
|
147
|
+
const { refs, floatingStyles, context } = useFloating({
|
|
148
|
+
placement: "bottom",
|
|
149
|
+
whileElementsMounted: autoUpdate,
|
|
150
|
+
open,
|
|
151
|
+
onOpenChange: (isOpen, _event, reason) => {
|
|
152
|
+
setOpen(isOpen);
|
|
153
|
+
if (isOpen === false) {
|
|
154
|
+
if (shouldResetActiveIndexOnClose === null || shouldResetActiveIndexOnClose === void 0 ? void 0 : shouldResetActiveIndexOnClose()) {
|
|
155
|
+
setActiveIndex(null);
|
|
156
|
+
}
|
|
157
|
+
onMenuClose === null || onMenuClose === void 0 ? void 0 : onMenuClose(String(reason !== null && reason !== void 0 ? reason : ""));
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
middleware: [
|
|
161
|
+
offset(MENU_OFFSET),
|
|
162
|
+
flip({ fallbackPlacements: ["top"] }),
|
|
163
|
+
size({
|
|
164
|
+
apply({ availableHeight, elements }) {
|
|
165
|
+
const maxHeight = calculateMaxHeight(availableHeight, {
|
|
166
|
+
maxHeight: AUTOCOMPLETE_MAX_HEIGHT$1,
|
|
167
|
+
});
|
|
168
|
+
Object.assign(elements.floating.style, {
|
|
169
|
+
maxHeight: `${maxHeight}px`,
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
}),
|
|
173
|
+
],
|
|
174
|
+
});
|
|
175
|
+
const listNav = useListNavigation(context, {
|
|
176
|
+
listRef,
|
|
177
|
+
activeIndex,
|
|
178
|
+
selectedIndex,
|
|
179
|
+
scrollItemIntoView: {
|
|
180
|
+
behavior: "smooth",
|
|
181
|
+
block: "end",
|
|
182
|
+
},
|
|
183
|
+
loop: true,
|
|
184
|
+
onNavigate: setActiveIndex,
|
|
185
|
+
virtual: true,
|
|
186
|
+
enabled: open,
|
|
187
|
+
openOnArrowKeyDown: false,
|
|
188
|
+
focusItemOnOpen: "auto",
|
|
189
|
+
focusItemOnHover: false,
|
|
190
|
+
});
|
|
191
|
+
const dismiss = useDismiss(context, {
|
|
192
|
+
outsidePress: true,
|
|
193
|
+
escapeKey: true,
|
|
194
|
+
outsidePressEvent: "click",
|
|
195
|
+
});
|
|
196
|
+
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([listNav, dismiss]);
|
|
197
|
+
useEffect(() => {
|
|
198
|
+
listRef.current.length = navigableCount;
|
|
199
|
+
setActiveIndex(prev => {
|
|
200
|
+
if (navigableCount <= 0)
|
|
201
|
+
return null;
|
|
202
|
+
if (prev == null)
|
|
203
|
+
return null;
|
|
204
|
+
return prev >= navigableCount ? navigableCount - 1 : prev;
|
|
205
|
+
});
|
|
206
|
+
}, [navigableCount, setActiveIndex, listRef]);
|
|
207
|
+
return {
|
|
208
|
+
refs,
|
|
209
|
+
floatingStyles,
|
|
210
|
+
context,
|
|
211
|
+
getReferenceProps,
|
|
212
|
+
getFloatingProps,
|
|
213
|
+
getItemProps,
|
|
214
|
+
activeIndex,
|
|
215
|
+
setActiveIndex,
|
|
216
|
+
listRef,
|
|
217
|
+
open,
|
|
218
|
+
setOpen,
|
|
219
|
+
setReferenceElement: (el) => refs.setReference(el),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Keeping this hook cohesive improves readability by centralizing related
|
|
224
|
+
// interactions and state transitions.
|
|
225
|
+
// eslint-disable-next-line max-statements
|
|
226
|
+
function useAutocomplete(props) {
|
|
227
|
+
const { menu, emptyActions, getOptionLabel: getOptionLabelProp, isOptionEqualToValue, inputValue, onInputChange, value, onChange, multiple, openOnFocus = true, readOnly = false, debounce: debounceMs = 300, } = props;
|
|
228
|
+
// TODO: Clean up the types in these refs by enhancing the type system in useCallbackRef
|
|
229
|
+
const getOptionLabelPropRef = useCallbackRef((opt) => getOptionLabelProp === null || getOptionLabelProp === void 0 ? void 0 : getOptionLabelProp(opt));
|
|
230
|
+
const getOptionLabel = useCallback((opt) => {
|
|
231
|
+
const maybe = getOptionLabelPropRef(opt);
|
|
232
|
+
return maybe !== null && maybe !== void 0 ? maybe : opt.label;
|
|
233
|
+
}, [getOptionLabelPropRef]);
|
|
234
|
+
const isOptionEqualToValueRef = useCallbackRef((a, b) => isOptionEqualToValue === null || isOptionEqualToValue === void 0 ? void 0 : isOptionEqualToValue(a, b));
|
|
235
|
+
const equals = useCallback((a, b) => {
|
|
236
|
+
const custom = isOptionEqualToValueRef(a, b);
|
|
237
|
+
return custom != null ? custom : getOptionLabel(a) === getOptionLabel(b);
|
|
238
|
+
}, [isOptionEqualToValueRef, getOptionLabel]);
|
|
239
|
+
const isOptionSelected = useCallback((opt) => {
|
|
240
|
+
var _a;
|
|
241
|
+
if (multiple) {
|
|
242
|
+
const current = (_a = value) !== null && _a !== void 0 ? _a : [];
|
|
243
|
+
return current.some(v => equals(v, opt));
|
|
244
|
+
}
|
|
245
|
+
const current = value;
|
|
246
|
+
return current != null ? equals(current, opt) : false;
|
|
247
|
+
}, [multiple, value, equals]);
|
|
248
|
+
const flatInitial = useMemo(() => flattenMenu(menu), [menu]);
|
|
249
|
+
const sections = flatInitial.sections;
|
|
250
|
+
const optionItems = flatInitial.optionItems;
|
|
251
|
+
const persistentsHeaders = flatInitial.persistentsHeaders;
|
|
252
|
+
const persistentsFooters = flatInitial.persistentsFooters;
|
|
253
|
+
// Stable wrappers for function props
|
|
254
|
+
const inputEqualsOptionRef = useCallbackRef((text, o) => {
|
|
255
|
+
const fn = props.inputEqualsOption;
|
|
256
|
+
return fn ? fn(text, o) : undefined;
|
|
257
|
+
});
|
|
258
|
+
const inputEquals = useCallback((text, o) => {
|
|
259
|
+
const custom = inputEqualsOptionRef(text, o);
|
|
260
|
+
return custom != null ? custom : getOptionLabel(o) === text;
|
|
261
|
+
}, [inputEqualsOptionRef, getOptionLabel]);
|
|
262
|
+
// inputValue changes very often, is this worth memoizing?
|
|
263
|
+
const exactLabelMatch = useMemo(() => {
|
|
264
|
+
return optionItems.some(o => inputEquals(inputValue, o));
|
|
265
|
+
}, [optionItems, inputEquals, inputValue]);
|
|
266
|
+
const lastInputWasUser = useRef(false);
|
|
267
|
+
const [debouncedInputValue, setDebouncedInputValue] = useState(inputValue);
|
|
268
|
+
const debouncedSetter = useDebounce(setDebouncedInputValue, debounceMs);
|
|
269
|
+
useEffect(() => {
|
|
270
|
+
if (debounceMs === 0) {
|
|
271
|
+
setDebouncedInputValue(inputValue);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
debouncedSetter(inputValue);
|
|
275
|
+
}, [inputValue, debounceMs, debouncedSetter]);
|
|
276
|
+
const filterOptionsRef = useCallbackRef((opts, term) => {
|
|
277
|
+
const fn = (typeof props.filterOptions === "function"
|
|
278
|
+
? props.filterOptions
|
|
279
|
+
: undefined) || undefined;
|
|
280
|
+
return fn ? fn(opts, term) : undefined;
|
|
281
|
+
});
|
|
282
|
+
const applyFilter = useCallback((opts, term) => {
|
|
283
|
+
if (props.filterOptions === false)
|
|
284
|
+
return opts;
|
|
285
|
+
const override = filterOptionsRef(opts, term);
|
|
286
|
+
if (override)
|
|
287
|
+
return override;
|
|
288
|
+
const lowered = term.toLowerCase();
|
|
289
|
+
return opts.filter(opt => getOptionLabel(opt).toLowerCase().includes(lowered));
|
|
290
|
+
}, [props.filterOptions, filterOptionsRef, getOptionLabel]);
|
|
291
|
+
const renderable = useMemo(() => {
|
|
292
|
+
const filter = (opts) => {
|
|
293
|
+
if (exactLabelMatch && !lastInputWasUser.current)
|
|
294
|
+
return opts;
|
|
295
|
+
return applyFilter(opts, debouncedInputValue);
|
|
296
|
+
};
|
|
297
|
+
const items = buildRenderableList(sections, filter);
|
|
298
|
+
const hasAnyOptions = items.some(i => i.kind === "option");
|
|
299
|
+
if (!hasAnyOptions) {
|
|
300
|
+
if (emptyActions) {
|
|
301
|
+
const derived = typeof emptyActions === "function"
|
|
302
|
+
? emptyActions({ inputValue })
|
|
303
|
+
: emptyActions;
|
|
304
|
+
return derived.map(act => ({
|
|
305
|
+
kind: "action",
|
|
306
|
+
action: act,
|
|
307
|
+
origin: "empty",
|
|
308
|
+
}));
|
|
309
|
+
}
|
|
310
|
+
// No options and no emptyActions: render empty state
|
|
311
|
+
return [];
|
|
312
|
+
}
|
|
313
|
+
return items;
|
|
314
|
+
}, [
|
|
315
|
+
sections,
|
|
316
|
+
applyFilter,
|
|
317
|
+
debouncedInputValue,
|
|
318
|
+
exactLabelMatch,
|
|
319
|
+
emptyActions,
|
|
320
|
+
inputValue,
|
|
321
|
+
]);
|
|
322
|
+
// This is only options
|
|
323
|
+
const optionCount = renderable.reduce((count, item) => count + (item.kind === "option" ? 1 : 0), 0);
|
|
324
|
+
const hasSelection = useMemo(() => {
|
|
325
|
+
var _a;
|
|
326
|
+
if (multiple) {
|
|
327
|
+
const current = (_a = value) !== null && _a !== void 0 ? _a : [];
|
|
328
|
+
return Array.isArray(current) && current.length > 0;
|
|
329
|
+
}
|
|
330
|
+
return value != null;
|
|
331
|
+
}, [multiple, value]);
|
|
332
|
+
const headerInteractivePersistents = persistentsHeaders.filter(p => Boolean(p.onClick));
|
|
333
|
+
const footerInteractivePersistents = persistentsFooters.filter(p => Boolean(p.onClick));
|
|
334
|
+
const mainNavigableCount = renderable.reduce((c, i) => c + (i.kind === "section" ? 0 : 1), 0);
|
|
335
|
+
const totalNavigableCount = headerInteractivePersistents.length +
|
|
336
|
+
mainNavigableCount +
|
|
337
|
+
footerInteractivePersistents.length;
|
|
338
|
+
// Compute the currently selected index in the global navigable list (header -> middle -> footer)
|
|
339
|
+
const selectedIndex = useMemo(() => {
|
|
340
|
+
const selectedValue = multiple
|
|
341
|
+
? value === null || value === void 0 ? void 0 : value[0]
|
|
342
|
+
: value;
|
|
343
|
+
if (!selectedValue)
|
|
344
|
+
return null;
|
|
345
|
+
const middleIndex = findNavigableIndexForValue(renderable, equals, selectedValue);
|
|
346
|
+
if (middleIndex == null)
|
|
347
|
+
return null;
|
|
348
|
+
return headerInteractivePersistents.length + middleIndex;
|
|
349
|
+
}, [
|
|
350
|
+
multiple,
|
|
351
|
+
value,
|
|
352
|
+
renderable,
|
|
353
|
+
equals,
|
|
354
|
+
headerInteractivePersistents.length,
|
|
355
|
+
]);
|
|
356
|
+
const { refs, floatingStyles, context, getReferenceProps, getFloatingProps, getItemProps, activeIndex, setActiveIndex, listRef, open, setOpen, setReferenceElement, } = useAutocompleteListNav({
|
|
357
|
+
navigableCount: totalNavigableCount,
|
|
358
|
+
shouldResetActiveIndexOnClose: () => !hasSelection,
|
|
359
|
+
selectedIndex,
|
|
360
|
+
onMenuClose: () => {
|
|
361
|
+
if (props.allowFreeForm !== true) {
|
|
362
|
+
const hasText = inputValue.trim().length > 0;
|
|
363
|
+
if (hasText && !hasSelection) {
|
|
364
|
+
lastInputWasUser.current = false;
|
|
365
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange("");
|
|
366
|
+
setActiveIndex(null);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
});
|
|
371
|
+
const [inputFocused, setInputFocused] = useState(false);
|
|
372
|
+
// Handles activeIndex reset and, in single-select mode only, clearing selection when input is empty
|
|
373
|
+
useEffect(() => {
|
|
374
|
+
const hasText = inputValue.trim().length > 0;
|
|
375
|
+
if (hasText)
|
|
376
|
+
return;
|
|
377
|
+
// Always reset highlight when input is empty
|
|
378
|
+
setActiveIndex(null);
|
|
379
|
+
// In multiple mode, clearing the input should NOT clear the selection
|
|
380
|
+
if (multiple)
|
|
381
|
+
return;
|
|
382
|
+
// For single-select, treat clearing input as clearing the selection
|
|
383
|
+
if (hasSelection) {
|
|
384
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(undefined);
|
|
385
|
+
}
|
|
386
|
+
}, [inputValue, multiple, hasSelection, setActiveIndex, onChange, open]);
|
|
387
|
+
function selectOption(option) {
|
|
388
|
+
var _a;
|
|
389
|
+
if (multiple) {
|
|
390
|
+
const current = (_a = value) !== null && _a !== void 0 ? _a : [];
|
|
391
|
+
const exists = current.some(v => equals(v, option));
|
|
392
|
+
const next = exists
|
|
393
|
+
? current.filter(v => !equals(v, option))
|
|
394
|
+
: [...current, option];
|
|
395
|
+
onChange(next);
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
onChange(option);
|
|
399
|
+
lastInputWasUser.current = false;
|
|
400
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange(getOptionLabel(option));
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function tryCommitFreeFormOnEnter() {
|
|
404
|
+
if (props.allowFreeForm !== true)
|
|
405
|
+
return false;
|
|
406
|
+
if (open && activeIndex != null)
|
|
407
|
+
return false;
|
|
408
|
+
const inputText = inputValue.trim();
|
|
409
|
+
if (inputText.length === 0)
|
|
410
|
+
return false;
|
|
411
|
+
commitFromInputText(inputText);
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
// Keep the selected item highlighted when deleting characters from the input
|
|
415
|
+
const prevInputLengthRef = useRef(inputValue.length);
|
|
416
|
+
useEffect(() => {
|
|
417
|
+
const previousLength = prevInputLengthRef.current;
|
|
418
|
+
prevInputLengthRef.current = inputValue.length;
|
|
419
|
+
if (!open)
|
|
420
|
+
return;
|
|
421
|
+
if (!lastInputWasUser.current)
|
|
422
|
+
return;
|
|
423
|
+
if (previousLength <= inputValue.length)
|
|
424
|
+
return; // only on deletion
|
|
425
|
+
if (!hasSelection)
|
|
426
|
+
return;
|
|
427
|
+
const selectedValue = multiple
|
|
428
|
+
? value === null || value === void 0 ? void 0 : value[0]
|
|
429
|
+
: value;
|
|
430
|
+
if (!selectedValue)
|
|
431
|
+
return;
|
|
432
|
+
const idx = findNavigableIndexForValue(renderable, equals, selectedValue);
|
|
433
|
+
if (idx != null)
|
|
434
|
+
setActiveIndex(idx);
|
|
435
|
+
}, [
|
|
436
|
+
inputValue,
|
|
437
|
+
renderable,
|
|
438
|
+
equals,
|
|
439
|
+
value,
|
|
440
|
+
open,
|
|
441
|
+
hasSelection,
|
|
442
|
+
multiple,
|
|
443
|
+
setActiveIndex,
|
|
444
|
+
]);
|
|
445
|
+
useEffect(() => {
|
|
446
|
+
if (!open)
|
|
447
|
+
return;
|
|
448
|
+
// When opening the menu, initialize the highlight consistently:
|
|
449
|
+
// - If there is a current selection, highlight that option
|
|
450
|
+
// - Otherwise, leave the highlight unset (null)
|
|
451
|
+
const selectedValue = multiple
|
|
452
|
+
? value === null || value === void 0 ? void 0 : value[0]
|
|
453
|
+
: value;
|
|
454
|
+
if (selectedValue) {
|
|
455
|
+
const selectedNavigableIndex = findNavigableIndexForValue(renderable, equals, selectedValue);
|
|
456
|
+
if (selectedNavigableIndex != null) {
|
|
457
|
+
setActiveIndex(selectedNavigableIndex);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
setActiveIndex(null);
|
|
462
|
+
}, [open, multiple, value, renderable, equals, setActiveIndex]);
|
|
463
|
+
const onSelection = useCallback((option) => {
|
|
464
|
+
selectOption(option);
|
|
465
|
+
// Might not always want to close on selection. Multi for example.
|
|
466
|
+
setOpen(false);
|
|
467
|
+
}, [selectOption, setOpen]);
|
|
468
|
+
const onAction = useCallback((action) => {
|
|
469
|
+
action.run();
|
|
470
|
+
setActiveIndex(null);
|
|
471
|
+
if (action.closeOnRun !== false)
|
|
472
|
+
setOpen(false);
|
|
473
|
+
}, [setOpen, setActiveIndex]);
|
|
474
|
+
function commitFromInputText(inputText) {
|
|
475
|
+
var _a;
|
|
476
|
+
if (inputText.length === 0)
|
|
477
|
+
return false;
|
|
478
|
+
const match = optionItems.find(o => inputEquals(inputText, o));
|
|
479
|
+
if (match) {
|
|
480
|
+
onSelection(match);
|
|
481
|
+
return true;
|
|
482
|
+
}
|
|
483
|
+
setOpen(false);
|
|
484
|
+
if (props.allowFreeForm !== true)
|
|
485
|
+
return false;
|
|
486
|
+
const freeFormCreated = (_a = props.createFreeFormValue) === null || _a === void 0 ? void 0 : _a.call(props, inputText);
|
|
487
|
+
if (!freeFormCreated)
|
|
488
|
+
return false;
|
|
489
|
+
props.onChange(freeFormCreated);
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
const tryRestoreInputToSelectedLabel = useCallback(() => {
|
|
493
|
+
if (props.allowFreeForm === true)
|
|
494
|
+
return;
|
|
495
|
+
const selectedValue = multiple
|
|
496
|
+
? value === null || value === void 0 ? void 0 : value[0]
|
|
497
|
+
: value;
|
|
498
|
+
if (!selectedValue)
|
|
499
|
+
return;
|
|
500
|
+
const selectedLabel = getOptionLabel(selectedValue);
|
|
501
|
+
if (inputValue === selectedLabel)
|
|
502
|
+
return;
|
|
503
|
+
lastInputWasUser.current = false;
|
|
504
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange(selectedLabel);
|
|
505
|
+
}, [
|
|
506
|
+
props.allowFreeForm,
|
|
507
|
+
getOptionLabel,
|
|
508
|
+
inputValue,
|
|
509
|
+
onInputChange,
|
|
510
|
+
multiple,
|
|
511
|
+
value,
|
|
512
|
+
]);
|
|
513
|
+
const onInputFocus = useCallback(() => {
|
|
514
|
+
var _a;
|
|
515
|
+
setInputFocused(true);
|
|
516
|
+
if (!readOnly && openOnFocus)
|
|
517
|
+
setOpen(true);
|
|
518
|
+
(_a = props.onFocus) === null || _a === void 0 ? void 0 : _a.call(props);
|
|
519
|
+
}, [props.onFocus, readOnly, openOnFocus, setOpen]);
|
|
520
|
+
const onInputBlur = useCallback(() => {
|
|
521
|
+
var _a, _b;
|
|
522
|
+
setInputFocused(false);
|
|
523
|
+
if (readOnly) {
|
|
524
|
+
(_a = props.onBlur) === null || _a === void 0 ? void 0 : _a.call(props);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (props.allowFreeForm === true) {
|
|
528
|
+
const inputText = inputValue.trim();
|
|
529
|
+
if (inputText.length > 0)
|
|
530
|
+
commitFromInputText(inputText);
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
tryRestoreInputToSelectedLabel();
|
|
534
|
+
}
|
|
535
|
+
lastInputWasUser.current = false;
|
|
536
|
+
(_b = props.onBlur) === null || _b === void 0 ? void 0 : _b.call(props);
|
|
537
|
+
}, [
|
|
538
|
+
readOnly,
|
|
539
|
+
props.allowFreeForm,
|
|
540
|
+
inputValue,
|
|
541
|
+
props.onBlur,
|
|
542
|
+
tryRestoreInputToSelectedLabel,
|
|
543
|
+
setOpen,
|
|
544
|
+
]);
|
|
545
|
+
function getRegionByActiveIndex(index) {
|
|
546
|
+
const headerCount = headerInteractivePersistents.length;
|
|
547
|
+
const mainCount = mainNavigableCount;
|
|
548
|
+
if (index < headerCount)
|
|
549
|
+
return { region: "header", regionIndex: index };
|
|
550
|
+
const middleIndex = index - headerCount;
|
|
551
|
+
if (middleIndex < mainCount) {
|
|
552
|
+
return { region: "middle", regionIndex: middleIndex };
|
|
553
|
+
}
|
|
554
|
+
return {
|
|
555
|
+
region: "footer",
|
|
556
|
+
regionIndex: middleIndex - mainCount,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
function computeInitialIndexForArrowUp() {
|
|
560
|
+
// If there are interactive footers, prefer the very last navigable item
|
|
561
|
+
if (footerInteractivePersistents.length > 0) {
|
|
562
|
+
return totalNavigableCount > 0 ? totalNavigableCount - 1 : null;
|
|
563
|
+
}
|
|
564
|
+
// Otherwise, prefer the last OPTION (not action), matching legacy behavior
|
|
565
|
+
let navigable = -1;
|
|
566
|
+
let lastOptionIdx = -1;
|
|
567
|
+
for (const item of renderable) {
|
|
568
|
+
if (item.kind === "section")
|
|
569
|
+
continue;
|
|
570
|
+
navigable += 1;
|
|
571
|
+
if (item.kind === "option")
|
|
572
|
+
lastOptionIdx = navigable;
|
|
573
|
+
}
|
|
574
|
+
if (lastOptionIdx >= 0) {
|
|
575
|
+
return headerInteractivePersistents.length + lastOptionIdx;
|
|
576
|
+
}
|
|
577
|
+
return totalNavigableCount > 0 ? totalNavigableCount - 1 : null;
|
|
578
|
+
}
|
|
579
|
+
function handleArrowNavigation(key, event) {
|
|
580
|
+
if (!open) {
|
|
581
|
+
setOpen(true);
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
if (activeIndex == null) {
|
|
585
|
+
setActiveIndex(key === "ArrowDown" ? 0 : computeInitialIndexForArrowUp());
|
|
586
|
+
event.preventDefault();
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function handleEnterKey(event) {
|
|
590
|
+
if (tryCommitFreeFormOnEnter()) {
|
|
591
|
+
event.preventDefault();
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
if (!open || activeIndex == null)
|
|
595
|
+
return;
|
|
596
|
+
const { region, regionIndex } = getRegionByActiveIndex(activeIndex);
|
|
597
|
+
if (region === "middle") {
|
|
598
|
+
invokeActiveItemOnEnter(event, regionIndex, renderable, onSelection, onAction);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
const persistent = region === "header"
|
|
602
|
+
? headerInteractivePersistents[regionIndex]
|
|
603
|
+
: footerInteractivePersistents[regionIndex];
|
|
604
|
+
if (persistent === null || persistent === void 0 ? void 0 : persistent.onClick) {
|
|
605
|
+
onAction({
|
|
606
|
+
run: persistent.onClick,
|
|
607
|
+
closeOnRun: persistent.shouldClose,
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const onInputKeyDown = useCallback((event) => {
|
|
612
|
+
const key = event.key;
|
|
613
|
+
if (key !== "ArrowDown" && key !== "ArrowUp" && key !== "Enter")
|
|
614
|
+
return;
|
|
615
|
+
if (key === "ArrowDown" || key === "ArrowUp") {
|
|
616
|
+
handleArrowNavigation(key, event);
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
handleEnterKey(event);
|
|
620
|
+
}, [open, onSelection, onAction]);
|
|
621
|
+
const onInputChangeFromUser = useCallback((val) => {
|
|
622
|
+
lastInputWasUser.current = true;
|
|
623
|
+
// Reset highlight (activeIndex) on additions to the search term
|
|
624
|
+
if (val.length > inputValue.length) {
|
|
625
|
+
setActiveIndex(null);
|
|
626
|
+
}
|
|
627
|
+
// Important: update open state before propagating the change so that downstream effects
|
|
628
|
+
// don’t see an intermediate state where inputValue changed but open was stale
|
|
629
|
+
if (!readOnly) {
|
|
630
|
+
const hasText = val.trim().length > 0;
|
|
631
|
+
const mustSelectFromOptions = hasText && !props.allowFreeForm;
|
|
632
|
+
const keepOpenOnEmpty = openOnFocus && inputFocused;
|
|
633
|
+
setOpen(mustSelectFromOptions || keepOpenOnEmpty);
|
|
634
|
+
}
|
|
635
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange(val);
|
|
636
|
+
}, [
|
|
637
|
+
onInputChange,
|
|
638
|
+
inputValue,
|
|
639
|
+
setActiveIndex,
|
|
640
|
+
readOnly,
|
|
641
|
+
props.allowFreeForm,
|
|
642
|
+
openOnFocus,
|
|
643
|
+
inputFocused,
|
|
644
|
+
setOpen,
|
|
645
|
+
]);
|
|
646
|
+
return {
|
|
647
|
+
// rendering data
|
|
648
|
+
renderable,
|
|
649
|
+
optionCount,
|
|
650
|
+
persistentsHeaders,
|
|
651
|
+
persistentsFooters,
|
|
652
|
+
headerInteractiveCount: headerInteractivePersistents.length,
|
|
653
|
+
middleNavigableCount: mainNavigableCount,
|
|
654
|
+
getOptionLabel,
|
|
655
|
+
isOptionSelected,
|
|
656
|
+
// floating-ui
|
|
657
|
+
refs,
|
|
658
|
+
floatingStyles,
|
|
659
|
+
context,
|
|
660
|
+
getReferenceProps,
|
|
661
|
+
getFloatingProps,
|
|
662
|
+
getItemProps,
|
|
663
|
+
// state
|
|
664
|
+
open,
|
|
665
|
+
setOpen,
|
|
666
|
+
activeIndex,
|
|
667
|
+
setActiveIndex,
|
|
668
|
+
listRef,
|
|
669
|
+
// actions
|
|
670
|
+
onSelection,
|
|
671
|
+
onAction,
|
|
672
|
+
// input handlers
|
|
673
|
+
onInputChangeFromUser,
|
|
674
|
+
onInputBlur,
|
|
675
|
+
onInputFocus,
|
|
676
|
+
onInputKeyDown,
|
|
677
|
+
// ref attachment
|
|
678
|
+
setReferenceElement,
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function MenuList({ items, activeIndex, indexOffset = 0, getItemProps, listRef, listboxId, customRenderOption, customRenderSection, customRenderAction, getOptionLabel, onSelect, onAction, isOptionSelected, slotOverrides, }) {
|
|
683
|
+
let navigableIndex = -1;
|
|
684
|
+
function renderItemNode(item) {
|
|
685
|
+
var _a, _b, _c, _d, _e, _f;
|
|
686
|
+
if (item.kind === "section") {
|
|
687
|
+
return handleSectionRendering({
|
|
688
|
+
section: item.section,
|
|
689
|
+
customRenderSection,
|
|
690
|
+
sectionClassName: (_a = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.section) === null || _a === void 0 ? void 0 : _a.className,
|
|
691
|
+
sectionStyle: (_b = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.section) === null || _b === void 0 ? void 0 : _b.style,
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
if (item.kind === "option") {
|
|
695
|
+
const result = handleOptionRendering({
|
|
696
|
+
option: item.value,
|
|
697
|
+
activeIndex,
|
|
698
|
+
navigableIndex,
|
|
699
|
+
getItemProps,
|
|
700
|
+
listRef,
|
|
701
|
+
listboxId,
|
|
702
|
+
isOptionSelected,
|
|
703
|
+
customRenderOption,
|
|
704
|
+
getOptionLabel,
|
|
705
|
+
onSelect,
|
|
706
|
+
indexOffset,
|
|
707
|
+
optionClassName: (_c = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.option) === null || _c === void 0 ? void 0 : _c.className,
|
|
708
|
+
optionStyle: (_d = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.option) === null || _d === void 0 ? void 0 : _d.style,
|
|
709
|
+
});
|
|
710
|
+
navigableIndex = result.nextNavigableIndex;
|
|
711
|
+
return result.node;
|
|
712
|
+
}
|
|
713
|
+
const result = handleActionRendering({
|
|
714
|
+
action: item.action,
|
|
715
|
+
activeIndex,
|
|
716
|
+
navigableIndex,
|
|
717
|
+
getItemProps,
|
|
718
|
+
listRef,
|
|
719
|
+
listboxId,
|
|
720
|
+
customRenderAction,
|
|
721
|
+
onAction,
|
|
722
|
+
indexOffset,
|
|
723
|
+
actionClassName: (_e = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.action) === null || _e === void 0 ? void 0 : _e.className,
|
|
724
|
+
actionStyle: (_f = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.action) === null || _f === void 0 ? void 0 : _f.style,
|
|
725
|
+
origin: item.origin,
|
|
726
|
+
});
|
|
727
|
+
navigableIndex = result.nextNavigableIndex;
|
|
728
|
+
return result.node;
|
|
729
|
+
}
|
|
730
|
+
return React__default.createElement(React__default.Fragment, null, items.map(renderItemNode));
|
|
731
|
+
}
|
|
732
|
+
function handleSectionRendering({ customRenderSection, section, sectionClassName, sectionStyle, }) {
|
|
733
|
+
var _a;
|
|
734
|
+
const headerContent = customRenderSection ? (customRenderSection(section)) : (React__default.createElement(DefaultSectionContent, { section: section }));
|
|
735
|
+
return (React__default.createElement("div", { key: `section-${String((_a = section.key) !== null && _a !== void 0 ? _a : section.label)}`, role: "presentation", tabIndex: -1, "data-testid": "ATL-AutocompleteRebuilt-Section", className: classnames(styles$1.section, styles$1.stickyTop, sectionClassName), style: sectionStyle }, headerContent));
|
|
736
|
+
}
|
|
737
|
+
function DefaultSectionContent({ section, }) {
|
|
738
|
+
return React__default.createElement(Heading, { level: 5 }, section.label);
|
|
739
|
+
}
|
|
740
|
+
function handleOptionRendering({ option, activeIndex, navigableIndex, getItemProps, listRef, listboxId, isOptionSelected, customRenderOption, getOptionLabel, onSelect, indexOffset = 0, optionClassName, optionStyle, }) {
|
|
741
|
+
var _a;
|
|
742
|
+
const nextNavigableIndex = navigableIndex + 1;
|
|
743
|
+
const isActive = activeIndex === nextNavigableIndex;
|
|
744
|
+
const isSelected = isOptionSelected(option);
|
|
745
|
+
const optionContent = customRenderOption ? (customRenderOption({ value: option, isActive, isSelected })) : (React__default.createElement(DefaultOptionContent, { isSelected: isSelected, text: getOptionLabel(option) }));
|
|
746
|
+
return {
|
|
747
|
+
node: (React__default.createElement("div", Object.assign({ key: `option-${String((_a = option.key) !== null && _a !== void 0 ? _a : getOptionLabel(option))}` }, getItemProps({
|
|
748
|
+
ref(node) {
|
|
749
|
+
const idx = nextNavigableIndex + indexOffset;
|
|
750
|
+
if (node)
|
|
751
|
+
listRef.current[idx] = node;
|
|
752
|
+
},
|
|
753
|
+
onClick: () => onSelect(option),
|
|
754
|
+
className: classnames(styles$1.option, isActive && styles$1.optionActive, optionClassName),
|
|
755
|
+
style: optionStyle,
|
|
756
|
+
}), { role: "option", tabIndex: -1, "aria-selected": isSelected ? true : false, id: `${listboxId}-item-${nextNavigableIndex + indexOffset}`, "data-index": nextNavigableIndex + indexOffset, "data-active": isActive ? true : undefined }), optionContent)),
|
|
757
|
+
nextNavigableIndex,
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
function DefaultOptionContent({ isSelected, text, }) {
|
|
761
|
+
return (React__default.createElement("div", { className: styles$1.defaultOptionContent },
|
|
762
|
+
React__default.createElement("div", { className: styles$1.icon }, isSelected && React__default.createElement(Icon, { name: "checkmark", size: "small" })),
|
|
763
|
+
React__default.createElement(Text, null, text)));
|
|
764
|
+
}
|
|
765
|
+
function handleActionRendering({ action, activeIndex, navigableIndex, getItemProps, listRef, listboxId, customRenderAction, onAction, indexOffset = 0, actionClassName, actionStyle, origin, }) {
|
|
766
|
+
var _a;
|
|
767
|
+
const nextNavigableIndex = navigableIndex + 1;
|
|
768
|
+
const isActive = activeIndex === nextNavigableIndex;
|
|
769
|
+
const actionContent = customRenderAction ? (customRenderAction({ value: action, isActive, origin })) : (React__default.createElement(DefaultActionContent, { textContent: action.label }));
|
|
770
|
+
const computedIndex = nextNavigableIndex + indexOffset;
|
|
771
|
+
const itemProps = getItemProps({
|
|
772
|
+
ref(node) {
|
|
773
|
+
if (node) {
|
|
774
|
+
listRef.current[computedIndex] = node;
|
|
775
|
+
}
|
|
776
|
+
},
|
|
777
|
+
onClick: () => {
|
|
778
|
+
onAction({
|
|
779
|
+
run: action.onClick,
|
|
780
|
+
closeOnRun: action.shouldClose,
|
|
781
|
+
});
|
|
782
|
+
},
|
|
783
|
+
className: classnames(styles$1.action, isActive && styles$1.actionActive, actionClassName),
|
|
784
|
+
style: actionStyle,
|
|
785
|
+
});
|
|
786
|
+
return {
|
|
787
|
+
node: (React__default.createElement("div", Object.assign({ key: `action-${String((origin !== null && origin !== void 0 ? origin : "menu") + "-" + ((_a = action.key) !== null && _a !== void 0 ? _a : action.label))}` }, itemProps, { role: "button", tabIndex: -1, "data-testid": "ATL-AutocompleteRebuilt-Action", id: `${listboxId}-item-${computedIndex}`, "data-index": computedIndex, "data-origin": origin, "data-active": isActive ? true : undefined }), actionContent)),
|
|
788
|
+
nextNavigableIndex,
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
function DefaultActionContent({ textContent, }) {
|
|
792
|
+
return (React__default.createElement(Typography, { textColor: "interactive", fontWeight: "semiBold", underline: "solid color-interactive", UNSAFE_style: {
|
|
793
|
+
textStyle: {
|
|
794
|
+
textDecorationThickness: "var(--border-thick)",
|
|
795
|
+
textUnderlineOffset: "var(--space-smallest)",
|
|
796
|
+
},
|
|
797
|
+
} }, textContent));
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
function PersistentRegion({ items, position, activeIndex, indexOffset, getItemProps, listRef, customRenderHeader, customRenderFooter, className, style, onAction, }) {
|
|
801
|
+
if (!items || items.length === 0)
|
|
802
|
+
return null;
|
|
803
|
+
let navigableIndex = -1;
|
|
804
|
+
return (React__default.createElement("div", { className: className, style: style, "data-region": position, "data-testid": `ATL-AutocompleteRebuilt-${position}` }, items.map(persistent => {
|
|
805
|
+
const result = handlePersistentRendering({
|
|
806
|
+
persistent,
|
|
807
|
+
position,
|
|
808
|
+
activeIndex,
|
|
809
|
+
indexOffset,
|
|
810
|
+
getItemProps,
|
|
811
|
+
customRenderHeader,
|
|
812
|
+
customRenderFooter,
|
|
813
|
+
listRef,
|
|
814
|
+
onAction,
|
|
815
|
+
navigableIndex,
|
|
816
|
+
});
|
|
817
|
+
navigableIndex = result.nextNavigableIndex;
|
|
818
|
+
return result.node;
|
|
819
|
+
})));
|
|
820
|
+
}
|
|
821
|
+
function handlePersistentRendering({ persistent, position, activeIndex, indexOffset, getItemProps, customRenderHeader, customRenderFooter, listRef, onAction, navigableIndex, }) {
|
|
822
|
+
const interactive = Boolean(persistent.onClick);
|
|
823
|
+
if (!interactive) {
|
|
824
|
+
const node = handleTextPersistentRendering({
|
|
825
|
+
persistent,
|
|
826
|
+
position,
|
|
827
|
+
customRenderHeader,
|
|
828
|
+
customRenderFooter,
|
|
829
|
+
});
|
|
830
|
+
return { node, nextNavigableIndex: navigableIndex };
|
|
831
|
+
}
|
|
832
|
+
return handleActionPersistentRendering({
|
|
833
|
+
persistent,
|
|
834
|
+
position,
|
|
835
|
+
activeIndex,
|
|
836
|
+
indexOffset,
|
|
837
|
+
getItemProps,
|
|
838
|
+
customRenderHeader,
|
|
839
|
+
customRenderFooter,
|
|
840
|
+
listRef,
|
|
841
|
+
onAction,
|
|
842
|
+
navigableIndex,
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
function handleTextPersistentRendering({ persistent, position, customRenderHeader, customRenderFooter, }) {
|
|
846
|
+
var _a;
|
|
847
|
+
let content;
|
|
848
|
+
if (position === "header" && customRenderHeader) {
|
|
849
|
+
content = customRenderHeader({ value: persistent });
|
|
850
|
+
}
|
|
851
|
+
else if (position === "footer" && customRenderFooter) {
|
|
852
|
+
content = customRenderFooter({ value: persistent });
|
|
853
|
+
}
|
|
854
|
+
else {
|
|
855
|
+
content = React__default.createElement(DefaultTextPersistentContent, { persistent: persistent });
|
|
856
|
+
}
|
|
857
|
+
return (React__default.createElement("div", { key: `persistent-${position}-${String((_a = persistent.key) !== null && _a !== void 0 ? _a : persistent.label)}`, role: "presentation", tabIndex: -1, className: styles$1.textPersistent }, content));
|
|
858
|
+
}
|
|
859
|
+
function handleActionPersistentRendering({ persistent, position, activeIndex, indexOffset, getItemProps, customRenderHeader, customRenderFooter, listRef, onAction, navigableIndex, }) {
|
|
860
|
+
var _a;
|
|
861
|
+
const nextNavigableIndex = navigableIndex + 1;
|
|
862
|
+
const isActive = activeIndex === indexOffset + nextNavigableIndex;
|
|
863
|
+
let content;
|
|
864
|
+
if (position === "header" && customRenderHeader) {
|
|
865
|
+
content = customRenderHeader({
|
|
866
|
+
value: persistent,
|
|
867
|
+
isActive,
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
else if (position === "footer" && customRenderFooter) {
|
|
871
|
+
content = customRenderFooter({
|
|
872
|
+
value: persistent,
|
|
873
|
+
isActive,
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
else {
|
|
877
|
+
content = React__default.createElement(DefaultActionContent, { textContent: persistent.label });
|
|
878
|
+
}
|
|
879
|
+
return {
|
|
880
|
+
node: (React__default.createElement("div", Object.assign({ key: `persistent-${position}-${String((_a = persistent.key) !== null && _a !== void 0 ? _a : persistent.label)}`, "data-index": indexOffset + nextNavigableIndex, id: `${position}-persistent-${indexOffset + nextNavigableIndex}`, "data-active": isActive ? true : undefined }, getItemProps({
|
|
881
|
+
ref(persistNode) {
|
|
882
|
+
const idx = indexOffset + nextNavigableIndex;
|
|
883
|
+
if (persistNode)
|
|
884
|
+
listRef.current[idx] = persistNode;
|
|
885
|
+
},
|
|
886
|
+
onClick: () => onAction({
|
|
887
|
+
run: () => {
|
|
888
|
+
var _a;
|
|
889
|
+
(_a = persistent.onClick) === null || _a === void 0 ? void 0 : _a.call(persistent);
|
|
890
|
+
},
|
|
891
|
+
closeOnRun: persistent.shouldClose,
|
|
892
|
+
}),
|
|
893
|
+
className: classnames(styles$1.action, isActive && styles$1.actionActive),
|
|
894
|
+
}), { role: "button", tabIndex: -1 }), content)),
|
|
895
|
+
nextNavigableIndex,
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
function DefaultTextPersistentContent({ persistent, }) {
|
|
899
|
+
return React__default.createElement("div", { className: styles$1.textPersistent }, persistent.label);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
const AutocompleteRebuilt = forwardRef(AutocompleteRebuiltInternal);
|
|
903
|
+
// eslint-disable-next-line max-statements
|
|
904
|
+
function AutocompleteRebuiltInternal(props, forwardedRef) {
|
|
905
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
906
|
+
const { inputValue, placeholder, disabled, error, invalid, description, size: sizeProp, loading = false, } = props;
|
|
907
|
+
const { renderable, optionCount, persistentsHeaders, persistentsFooters, headerInteractiveCount, middleNavigableCount, getOptionLabel, isOptionSelected, refs, floatingStyles, context, getReferenceProps, getFloatingProps, getItemProps, activeIndex, open, listRef, onSelection, onAction, onInputChangeFromUser, onInputBlur, onInputFocus, onInputKeyDown, setReferenceElement, } = useAutocomplete(props);
|
|
908
|
+
const listboxId = React__default.useId();
|
|
909
|
+
// Provides mount/unmount-aware transition styles for the floating element
|
|
910
|
+
const { isMounted, styles: transitionStyles } = useTransitionStyles(context, {
|
|
911
|
+
initial: { opacity: 0 },
|
|
912
|
+
open: { opacity: 1 },
|
|
913
|
+
close: { opacity: 0 },
|
|
914
|
+
duration: { open: tokens["timing-base"], close: tokens["timing-base"] },
|
|
915
|
+
});
|
|
916
|
+
const [menuWidth, setMenuWidth] = React__default.useState(undefined);
|
|
917
|
+
const [positionRefEl, setPositionRefEl] = React__default.useState(null);
|
|
918
|
+
const composedReferenceProps = getReferenceProps({
|
|
919
|
+
onKeyDown: onInputKeyDown,
|
|
920
|
+
onFocus: onInputFocus,
|
|
921
|
+
onBlur: onInputBlur,
|
|
922
|
+
});
|
|
923
|
+
const inputProps = Object.assign(Object.assign(Object.assign(Object.assign({ version: 2, value: inputValue, onChange: props.readOnly ? undefined : onInputChangeFromUser }, (props.readOnly ? { onFocus: onInputFocus, onBlur: onInputBlur } : {})), { placeholder,
|
|
924
|
+
disabled, readOnly: props.readOnly, error: error !== null && error !== void 0 ? error : undefined, name: props.name, invalid, autoComplete: "off", description, size: sizeProp ? sizeProp : undefined, prefix: props.prefix, suffix: props.suffix }), (props.readOnly ? {} : composedReferenceProps)), { role: "combobox", "aria-autocomplete": "list", "aria-expanded": open ? true : false, "aria-controls": listboxId, "aria-activedescendant": open && activeIndex != null
|
|
925
|
+
? `${listboxId}-item-${activeIndex}`
|
|
926
|
+
: undefined });
|
|
927
|
+
const referenceInputRef = (node) => {
|
|
928
|
+
setReferenceElement(node);
|
|
929
|
+
// Workaround to get the width of the visual InputText element, which is not the same as
|
|
930
|
+
// the literal input reference element when props like suffix/prefix/clearable are present.
|
|
931
|
+
const visualInputTextElement = node === null || node === void 0 ? void 0 : node.closest("[data-testid='Form-Field-Wrapper']");
|
|
932
|
+
if (visualInputTextElement) {
|
|
933
|
+
setMenuWidth(visualInputTextElement.clientWidth);
|
|
934
|
+
setPositionRefEl(visualInputTextElement);
|
|
935
|
+
}
|
|
936
|
+
};
|
|
937
|
+
const mergedInputRef = mergeRefs([
|
|
938
|
+
referenceInputRef,
|
|
939
|
+
forwardedRef,
|
|
940
|
+
]);
|
|
941
|
+
useEffect(() => {
|
|
942
|
+
if (!positionRefEl)
|
|
943
|
+
return;
|
|
944
|
+
// Set the reference element to the visual InputText element so the menu aligns with the input.
|
|
945
|
+
refs.setPositionReference(positionRefEl);
|
|
946
|
+
}, [positionRefEl, refs]);
|
|
947
|
+
const menuClassName = classnames(styles$1.list, (_a = props.UNSAFE_className) === null || _a === void 0 ? void 0 : _a.menu);
|
|
948
|
+
const showEmptyStateMessage = optionCount === 0 && props.emptyStateMessage !== false;
|
|
949
|
+
const activeIndexForMiddle = activeIndex != null ? activeIndex - headerInteractiveCount : null;
|
|
950
|
+
return (React__default.createElement("div", { "data-testid": "ATL-AutocompleteRebuilt" },
|
|
951
|
+
props.customRenderInput ? (props.customRenderInput({ inputRef: mergedInputRef, inputProps })) : (React__default.createElement(InputText, Object.assign({ ref: mergedInputRef }, inputProps))),
|
|
952
|
+
isMounted && !props.readOnly && (React__default.createElement(FloatingPortal, null,
|
|
953
|
+
React__default.createElement(FloatingFocusManager, { context: context, modal: false, initialFocus: -1, closeOnFocusOut: true, returnFocus: false },
|
|
954
|
+
React__default.createElement("div", Object.assign({}, getFloatingProps({
|
|
955
|
+
ref(node) {
|
|
956
|
+
if (node)
|
|
957
|
+
refs.setFloating(node);
|
|
958
|
+
},
|
|
959
|
+
id: listboxId,
|
|
960
|
+
role: "listbox",
|
|
961
|
+
className: menuClassName,
|
|
962
|
+
style: Object.assign(Object.assign(Object.assign(Object.assign({}, floatingStyles), transitionStyles), (_b = props.UNSAFE_styles) === null || _b === void 0 ? void 0 : _b.menu), (menuWidth
|
|
963
|
+
? { width: menuWidth, maxWidth: menuWidth }
|
|
964
|
+
: {})),
|
|
965
|
+
})),
|
|
966
|
+
React__default.createElement(PersistentRegion, { items: persistentsHeaders, position: "header", activeIndex: activeIndex, indexOffset: 0, listboxId: listboxId, getItemProps: getItemProps, listRef: listRef, customRenderHeader: props.customRenderHeader, customRenderFooter: props.customRenderFooter, onAction: onAction, className: classnames(styles$1.persistentHeader, (_c = props.UNSAFE_className) === null || _c === void 0 ? void 0 : _c.header), style: (_d = props.UNSAFE_styles) === null || _d === void 0 ? void 0 : _d.header }),
|
|
967
|
+
React__default.createElement("div", { className: styles$1.scrollRegion }, loading ? ((_e = props.customRenderLoading) !== null && _e !== void 0 ? _e : React__default.createElement(LoadingContent, null)) : (React__default.createElement(React__default.Fragment, null,
|
|
968
|
+
showEmptyStateMessage && (React__default.createElement(EmptyStateMessage, { emptyState: props.emptyStateMessage })),
|
|
969
|
+
renderable.length > 0 && (React__default.createElement(MenuList, { items: renderable, activeIndex: activeIndexForMiddle, indexOffset: headerInteractiveCount, listboxId: listboxId, getItemProps: getItemProps, listRef: listRef, customRenderOption: props.customRenderOption, customRenderSection: props.customRenderSection, customRenderAction: props.customRenderAction, getOptionLabel: getOptionLabel, onSelect: onSelection, onAction: onAction, isOptionSelected: isOptionSelected, slotOverrides: {
|
|
970
|
+
option: {
|
|
971
|
+
className: (_f = props.UNSAFE_className) === null || _f === void 0 ? void 0 : _f.option,
|
|
972
|
+
style: (_g = props.UNSAFE_styles) === null || _g === void 0 ? void 0 : _g.option,
|
|
973
|
+
},
|
|
974
|
+
action: {
|
|
975
|
+
className: (_h = props.UNSAFE_className) === null || _h === void 0 ? void 0 : _h.action,
|
|
976
|
+
style: (_j = props.UNSAFE_styles) === null || _j === void 0 ? void 0 : _j.action,
|
|
977
|
+
},
|
|
978
|
+
section: {
|
|
979
|
+
className: (_k = props.UNSAFE_className) === null || _k === void 0 ? void 0 : _k.section,
|
|
980
|
+
style: (_l = props.UNSAFE_styles) === null || _l === void 0 ? void 0 : _l.section,
|
|
981
|
+
},
|
|
982
|
+
} }))))),
|
|
983
|
+
React__default.createElement(PersistentRegion, { items: persistentsFooters, position: "footer", activeIndex: activeIndex, indexOffset: headerInteractiveCount + middleNavigableCount, listboxId: listboxId, getItemProps: getItemProps, listRef: listRef, customRenderHeader: props.customRenderHeader, customRenderFooter: props.customRenderFooter, onAction: onAction, className: classnames(styles$1.persistentFooter, (_m = props.UNSAFE_className) === null || _m === void 0 ? void 0 : _m.footer), style: (_o = props.UNSAFE_styles) === null || _o === void 0 ? void 0 : _o.footer })))))));
|
|
984
|
+
}
|
|
985
|
+
function LoadingContent() {
|
|
986
|
+
return (React__default.createElement("div", { className: styles$1.loadingList },
|
|
987
|
+
React__default.createElement(Glimmer, { shape: "rectangle", size: "base" }),
|
|
988
|
+
React__default.createElement(Glimmer, { shape: "rectangle", size: "base" }),
|
|
989
|
+
React__default.createElement(Glimmer, { shape: "rectangle", size: "base" })));
|
|
990
|
+
}
|
|
991
|
+
function EmptyStateMessage({ emptyState, }) {
|
|
992
|
+
const emptyStateDefault = "No options";
|
|
993
|
+
const emptyStateContent = emptyState !== null && emptyState !== void 0 ? emptyState : emptyStateDefault;
|
|
994
|
+
return React__default.createElement("div", { className: styles$1.emptyStateMessage }, emptyStateContent);
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
var styles = {"autocomplete":"_7mObJiwfPh4-","options":"dL5JShAJlKM-","heading":"PWZL-94hH7k-","visible":"_2RzcnTdaPyc-","option":"y9zhi8Wr8QA-","active":"_3Xg49dtL1Q8-","separator":"LIeh390F3W8-","icon":"K2phy6IC3TY-","text":"a6-LbUm5WnY-","label":"tQNbuxcE9nU-","details":"qacStG9-XbE-","spinning":"P9cQDL4MZ-s-"};
|
|
998
|
+
|
|
999
|
+
const AUTOCOMPLETE_MAX_HEIGHT = 300;
|
|
1000
|
+
|
|
1001
|
+
function useRepositionMenu(attachTo, visible, cssManagedVisibility) {
|
|
1002
|
+
const { refs, floatingStyles, update } = useFloating(Object.assign({ placement: "bottom", middleware: [
|
|
1003
|
+
offset(8),
|
|
1004
|
+
flip({ fallbackPlacements: ["top"] }),
|
|
1005
|
+
size({
|
|
1006
|
+
apply({ availableHeight, elements }) {
|
|
1007
|
+
const maxHeight = calculateMaxHeight(availableHeight, {
|
|
1008
|
+
maxHeight: AUTOCOMPLETE_MAX_HEIGHT,
|
|
1009
|
+
});
|
|
1010
|
+
Object.assign(elements.floating.style, {
|
|
1011
|
+
maxHeight: `${maxHeight}px`,
|
|
1012
|
+
});
|
|
1013
|
+
},
|
|
1014
|
+
}),
|
|
1015
|
+
], elements: {
|
|
1016
|
+
reference: attachTo,
|
|
1017
|
+
} }, (!cssManagedVisibility
|
|
1018
|
+
? {
|
|
1019
|
+
whileElementsMounted: autoUpdate,
|
|
1020
|
+
}
|
|
1021
|
+
: {})));
|
|
1022
|
+
// While DefaultMenu leverages conditional rendering, CustomMenu is hidden with CSS
|
|
1023
|
+
// We need to apply the correct update method to each case
|
|
1024
|
+
useSafeLayoutEffect_1(() => {
|
|
1025
|
+
if (cssManagedVisibility && visible && attachTo && refs.floating.current) {
|
|
1026
|
+
const cleanup = autoUpdate(attachTo, refs.floating.current, update);
|
|
1027
|
+
return cleanup;
|
|
1028
|
+
}
|
|
1029
|
+
return undefined;
|
|
1030
|
+
}, [
|
|
1031
|
+
cssManagedVisibility,
|
|
1032
|
+
visible,
|
|
1033
|
+
attachTo,
|
|
1034
|
+
refs.floating.current,
|
|
1035
|
+
update,
|
|
1036
|
+
autoUpdate,
|
|
1037
|
+
]);
|
|
1038
|
+
const targetWidth = attachTo === null || attachTo === void 0 ? void 0 : attachTo.clientWidth;
|
|
1039
|
+
return {
|
|
1040
|
+
menuRef: refs.floating.current,
|
|
1041
|
+
setMenuRef: refs.setFloating,
|
|
1042
|
+
targetWidth,
|
|
1043
|
+
styles: {
|
|
1044
|
+
float: floatingStyles,
|
|
1045
|
+
},
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
function BaseAutocompleteMenuWrapperInternal({ setMenuRef, floatStyles, targetWidth, visible, children, }) {
|
|
1050
|
+
return (React__default.createElement("div", { className: classnames(styles.options, { [styles.visible]: visible }), ref: setMenuRef, style: Object.assign(Object.assign({}, floatStyles.float), { width: targetWidth }), "data-elevation": "elevated" }, children));
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* Provides a wrapper for the Autocomplete menu that handles positioning and visibility.
|
|
1054
|
+
* @param attachTo - The element that the menu should be attached to.
|
|
1055
|
+
*/
|
|
1056
|
+
function useAutocompleteMenu({ attachTo, }) {
|
|
1057
|
+
const [menuRef, setMenuRef] = React__default.useState(null);
|
|
1058
|
+
const AutocompleteMenuWrapper = useCallback(({ children, visible, }) => {
|
|
1059
|
+
const menuFloatProps = useRepositionMenu(attachTo, visible, true);
|
|
1060
|
+
useEffect(() => {
|
|
1061
|
+
setMenuRef(menuFloatProps.menuRef);
|
|
1062
|
+
}, [menuFloatProps.menuRef]);
|
|
1063
|
+
return (React__default.createElement(BaseAutocompleteMenuWrapper, { floatStyles: menuFloatProps.styles, setMenuRef: menuFloatProps.setMenuRef, targetWidth: menuFloatProps.targetWidth, visible: visible }, children));
|
|
1064
|
+
}, [attachTo]);
|
|
1065
|
+
return { MenuWrapper: AutocompleteMenuWrapper, menuRef };
|
|
1066
|
+
}
|
|
1067
|
+
function BaseAutocompleteMenuWrapper(props) {
|
|
1068
|
+
const mounted = useIsMounted_2();
|
|
1069
|
+
const menu = React__default.createElement(BaseAutocompleteMenuWrapperInternal, Object.assign({}, props));
|
|
1070
|
+
return mounted.current ? React__default.createElement(FloatingPortal, null, menu) : menu;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
function isOptionSelected(selectedOption, option) {
|
|
1074
|
+
return Boolean(selectedOption && selectedOption.value === option.value);
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Helper function to determine if the option is a group. This is used to
|
|
1078
|
+
* determine if the option contains a list of options for rendering Section
|
|
1079
|
+
* Labels in the Autocomplete component.
|
|
1080
|
+
*/
|
|
1081
|
+
function isOptionGroup(option) {
|
|
1082
|
+
return "options" in option;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/**
|
|
1086
|
+
* The rendering of the default MenuOption
|
|
1087
|
+
*/
|
|
1088
|
+
function MenuOption({ isHighlighted, option, onOptionSelect, isSelected, addSeparators, UNSAFE_className = {}, UNSAFE_style = {}, }) {
|
|
1089
|
+
if (isOptionGroup(option)) {
|
|
1090
|
+
return (React__default.createElement(MenuGroupOptions, { UNSAFE_className: UNSAFE_className.groupOption, option: option, UNSAFE_style: UNSAFE_style.groupOption }));
|
|
1091
|
+
}
|
|
1092
|
+
return (React__default.createElement(BaseMenuOption, { UNSAFE_className: UNSAFE_className.option, UNSAFE_style: UNSAFE_style.option, option: option, isHighlighted: isHighlighted, onOptionSelect: onOptionSelect, addSeparators: addSeparators },
|
|
1093
|
+
React__default.createElement(MenuOptionContent, { option: option, isSelected: isSelected, UNSAFE_className: UNSAFE_className.content, UNSAFE_style: UNSAFE_style.content })));
|
|
1094
|
+
}
|
|
1095
|
+
function MenuOptionContent({ option, isSelected, UNSAFE_className = {}, UNSAFE_style = {}, }) {
|
|
1096
|
+
const iconClassName = classnames(styles.icon, UNSAFE_className.icon);
|
|
1097
|
+
const textClassName = classnames(styles.text, UNSAFE_className.text);
|
|
1098
|
+
const labelClassName = classnames(styles.label, UNSAFE_className.label);
|
|
1099
|
+
const detailsClassName = classnames(styles.details, UNSAFE_className.details);
|
|
1100
|
+
return (React__default.createElement(React__default.Fragment, null,
|
|
1101
|
+
React__default.createElement("div", { className: iconClassName, style: UNSAFE_style.icon }, isSelected && React__default.createElement(Icon, { name: "checkmark", size: "small" })),
|
|
1102
|
+
React__default.createElement("div", { className: textClassName, style: UNSAFE_style.text },
|
|
1103
|
+
React__default.createElement("div", { className: labelClassName, style: UNSAFE_style.label },
|
|
1104
|
+
React__default.createElement(Text, null, option.label),
|
|
1105
|
+
option.description !== undefined && (React__default.createElement(Text, { variation: "subdued" }, option.description))),
|
|
1106
|
+
option.details !== undefined && (React__default.createElement("div", { className: detailsClassName, style: UNSAFE_style.details },
|
|
1107
|
+
React__default.createElement(Text, null, option.details))))));
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* The rendering of the default MenuGroupOption
|
|
1111
|
+
*/
|
|
1112
|
+
function MenuGroupOptions({ option, UNSAFE_className = {}, UNSAFE_style = {}, }) {
|
|
1113
|
+
return (React__default.createElement(BaseMenuGroupOption, { UNSAFE_className: UNSAFE_className.heading, UNSAFE_style: UNSAFE_style.heading },
|
|
1114
|
+
React__default.createElement(Heading, { level: 5 }, option.label)));
|
|
1115
|
+
}
|
|
1116
|
+
function BaseMenuGroupOption({ children, UNSAFE_className = "", UNSAFE_style = {}, }) {
|
|
1117
|
+
const headingClassName = classnames(styles.heading, UNSAFE_className);
|
|
1118
|
+
return (React__default.createElement("div", { className: headingClassName, style: UNSAFE_style }, children));
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Renders the base option component. The component takes children and renders them inside a button.
|
|
1122
|
+
*/
|
|
1123
|
+
function BaseMenuOption({ option, isHighlighted, onOptionSelect, addSeparators, children, UNSAFE_className = "", UNSAFE_style = {}, }) {
|
|
1124
|
+
const optionClass = classnames(styles.option, {
|
|
1125
|
+
[styles.active]: isHighlighted,
|
|
1126
|
+
[styles.separator]: addSeparators,
|
|
1127
|
+
}, UNSAFE_className);
|
|
1128
|
+
return (React__default.createElement("button", { role: "option", type: "button", className: optionClass, onMouseDown: onOptionSelect === null || onOptionSelect === void 0 ? void 0 : onOptionSelect.bind(undefined, option), style: UNSAFE_style }, children));
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
var KeyboardAction;
|
|
1132
|
+
(function (KeyboardAction) {
|
|
1133
|
+
KeyboardAction[KeyboardAction["Previous"] = -1] = "Previous";
|
|
1134
|
+
KeyboardAction[KeyboardAction["Next"] = 1] = "Next";
|
|
1135
|
+
KeyboardAction[KeyboardAction["Select"] = 0] = "Select";
|
|
1136
|
+
})(KeyboardAction || (KeyboardAction = {}));
|
|
1137
|
+
/**
|
|
1138
|
+
* Hook to handle custom keyboard navigation for the Autocomplete component.
|
|
1139
|
+
* Use this hook if you are using components in the menu that aren't MenuOption or BaseMenuOption.
|
|
1140
|
+
*/
|
|
1141
|
+
function useCustomKeyboardNavigation({ onRequestHighlightChange, }) {
|
|
1142
|
+
useOnKeyDown_2((event) => {
|
|
1143
|
+
onRequestHighlightChange === null || onRequestHighlightChange === void 0 ? void 0 : onRequestHighlightChange(event, KeyboardAction.Next);
|
|
1144
|
+
}, "ArrowDown");
|
|
1145
|
+
useOnKeyDown_2((event) => {
|
|
1146
|
+
onRequestHighlightChange === null || onRequestHighlightChange === void 0 ? void 0 : onRequestHighlightChange(event, KeyboardAction.Previous);
|
|
1147
|
+
}, "ArrowUp");
|
|
1148
|
+
useOnKeyDown_2((event) => {
|
|
1149
|
+
onRequestHighlightChange === null || onRequestHighlightChange === void 0 ? void 0 : onRequestHighlightChange(event, KeyboardAction.Select);
|
|
1150
|
+
}, "Enter");
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Hook to handle keyboard navigation for the Menu in the Autocomplete component.
|
|
1154
|
+
* If using components in the menu that aren't MenuOption or BaseMenuOption, you should use the `useCustomKeyboardNavigation` hook.
|
|
1155
|
+
*/
|
|
1156
|
+
function useKeyboardNavigation({ options, onOptionSelect, menuRef, visible, }) {
|
|
1157
|
+
const [highlightedIndex, setHighlightedIndex] = useState(0);
|
|
1158
|
+
const initialHighlight = options.some(isOptionGroup) ? 1 : 0;
|
|
1159
|
+
useEffect(() => setHighlightedIndex(initialHighlight), [options]);
|
|
1160
|
+
useEffect(() => {
|
|
1161
|
+
var _a, _b;
|
|
1162
|
+
(_b = (_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.children[highlightedIndex]) === null || _a === void 0 ? void 0 : _a.scrollIntoView) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
1163
|
+
behavior: "smooth",
|
|
1164
|
+
block: "nearest",
|
|
1165
|
+
inline: "start",
|
|
1166
|
+
});
|
|
1167
|
+
}, [highlightedIndex]);
|
|
1168
|
+
const onRequestHighlightChange = useCallback((event, direction) => {
|
|
1169
|
+
if (!visible)
|
|
1170
|
+
return;
|
|
1171
|
+
const indexChange = getRequestedIndexChange({
|
|
1172
|
+
event,
|
|
1173
|
+
options,
|
|
1174
|
+
direction,
|
|
1175
|
+
highlightedIndex,
|
|
1176
|
+
});
|
|
1177
|
+
switch (direction) {
|
|
1178
|
+
case KeyboardAction.Previous:
|
|
1179
|
+
setHighlightedIndex(prev => Math.max(0, prev + indexChange));
|
|
1180
|
+
break;
|
|
1181
|
+
case KeyboardAction.Next:
|
|
1182
|
+
setHighlightedIndex(prev => Math.min(options.length - 1, prev + indexChange));
|
|
1183
|
+
break;
|
|
1184
|
+
case KeyboardAction.Select:
|
|
1185
|
+
if (isOptionGroup(options[highlightedIndex]))
|
|
1186
|
+
return;
|
|
1187
|
+
onOptionSelect(options[highlightedIndex]);
|
|
1188
|
+
break;
|
|
1189
|
+
}
|
|
1190
|
+
}, [highlightedIndex, options, onOptionSelect, visible]);
|
|
1191
|
+
useCustomKeyboardNavigation({ onRequestHighlightChange });
|
|
1192
|
+
return { highlightedIndex };
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* Function to get the requested index change based on the current highlighted index and the direction of the keyboard action.
|
|
1196
|
+
* Accounts for groups in the options array.
|
|
1197
|
+
*/
|
|
1198
|
+
function getRequestedIndexChange({ event, options, direction, highlightedIndex, }) {
|
|
1199
|
+
event.preventDefault();
|
|
1200
|
+
const requestedIndex = options[highlightedIndex + direction];
|
|
1201
|
+
return requestedIndex && isOptionGroup(requestedIndex)
|
|
1202
|
+
? direction + direction
|
|
1203
|
+
: direction;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* Renders the default Menu for the Autocomplete component.
|
|
1208
|
+
*/
|
|
1209
|
+
function DefaultMenu({ options, selectedOption, onOptionSelect, attachTo, visible, }) {
|
|
1210
|
+
const { menuRef, setMenuRef, styles: floatStyles, targetWidth, } = useRepositionMenu(attachTo, visible, false);
|
|
1211
|
+
const detectSeparatorCondition = (option) => option.description || option.details;
|
|
1212
|
+
const addSeparators = options.some(detectSeparatorCondition);
|
|
1213
|
+
const { highlightedIndex } = useKeyboardNavigation({
|
|
1214
|
+
onOptionSelect,
|
|
1215
|
+
options,
|
|
1216
|
+
visible,
|
|
1217
|
+
menuRef,
|
|
1218
|
+
});
|
|
1219
|
+
return (React__default.createElement(BaseAutocompleteMenuWrapper, { setMenuRef, floatStyles, targetWidth, visible }, options === null || options === void 0 ? void 0 : options.map((option, index) => {
|
|
1220
|
+
return (React__default.createElement(MenuOption, { key: index, option: option, isHighlighted: index === highlightedIndex, onOptionSelect: onOptionSelect, isSelected: isOptionSelected(selectedOption, option), addSeparators: addSeparators }));
|
|
1221
|
+
})));
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
function Menu({ options, selectedOption, onOptionSelect, inputFocused, attachTo, inputRef, customRenderMenu, }) {
|
|
1225
|
+
if (customRenderMenu) {
|
|
1226
|
+
return (React__default.createElement(CustomMenu, { attachTo: attachTo, inputFocused: inputFocused, inputRef: inputRef, customRenderMenu: customRenderMenu, options: options, onOptionSelect: onOptionSelect, selectedOption: selectedOption }));
|
|
1227
|
+
}
|
|
1228
|
+
if (!inputFocused || !options.length)
|
|
1229
|
+
return null;
|
|
1230
|
+
return (React__default.createElement(DefaultMenu, { attachTo: attachTo, options: options, onOptionSelect: onOptionSelect, selectedOption: selectedOption, visible: inputFocused }));
|
|
1231
|
+
}
|
|
1232
|
+
/**
|
|
1233
|
+
* Renders the custom Menu for the Autocomplete component.
|
|
1234
|
+
* Provides the menuRef and MenuWrapper to the customRenderMenu function.
|
|
1235
|
+
*/
|
|
1236
|
+
function CustomMenu({ options, selectedOption, onOptionSelect, customRenderMenu, attachTo, inputFocused, inputRef, }) {
|
|
1237
|
+
const { MenuWrapper, menuRef } = useAutocompleteMenu({ attachTo });
|
|
1238
|
+
const menuContent = customRenderMenu({
|
|
1239
|
+
options,
|
|
1240
|
+
menuRef,
|
|
1241
|
+
onOptionSelect,
|
|
1242
|
+
selectedOption,
|
|
1243
|
+
inputFocused,
|
|
1244
|
+
MenuWrapper,
|
|
1245
|
+
inputRef,
|
|
1246
|
+
});
|
|
1247
|
+
return menuContent;
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
// Max statements increased to make room for the debounce functions
|
|
1251
|
+
// eslint-disable-next-line max-statements
|
|
1252
|
+
function AutocompleteInternal(_a, ref) {
|
|
1253
|
+
var _b;
|
|
1254
|
+
var { initialOptions = [], value, allowFreeForm = true, size = undefined, debounce: debounceRate = 300, onChange, getOptions, placeholder, onBlur, onFocus, validations, customRenderMenu } = _a, inputProps = __rest(_a, ["initialOptions", "value", "allowFreeForm", "size", "debounce", "onChange", "getOptions", "placeholder", "onBlur", "onFocus", "validations", "customRenderMenu"]);
|
|
1255
|
+
const initialOptionsMemo = useMemo(() => mapToOptions(initialOptions), [initialOptions]);
|
|
1256
|
+
const [options, setOptions] = useState(initialOptionsMemo);
|
|
1257
|
+
const [inputFocused, setInputFocused] = useState(false);
|
|
1258
|
+
const [inputText, setInputText] = useState((_b = value === null || value === void 0 ? void 0 : value.label) !== null && _b !== void 0 ? _b : "");
|
|
1259
|
+
const [autocompleteRef, setAutocompleteRef] = useState(null);
|
|
1260
|
+
const delayedSearch = useDebounce_2(updateSearch, debounceRate);
|
|
1261
|
+
const inputRef = useRef(null);
|
|
1262
|
+
useEffect(() => {
|
|
1263
|
+
delayedSearch();
|
|
1264
|
+
}, [inputText]);
|
|
1265
|
+
useEffect(() => {
|
|
1266
|
+
var _a;
|
|
1267
|
+
updateInput((_a = value === null || value === void 0 ? void 0 : value.label) !== null && _a !== void 0 ? _a : "");
|
|
1268
|
+
}, [value]);
|
|
1269
|
+
return (React__default.createElement("div", { className: styles.autocomplete, ref: setAutocompleteRef },
|
|
1270
|
+
React__default.createElement(InputText, Object.assign({ ref: mergeRefs([ref, inputRef]), autocomplete: false, size: size, value: inputText, onChange: handleInputChange, placeholder: placeholder, onFocus: handleInputFocus, onBlur: handleInputBlur, validations: validations }, inputProps)),
|
|
1271
|
+
React__default.createElement(Menu, { attachTo: autocompleteRef, inputRef: inputRef, inputFocused: inputFocused, options: options, customRenderMenu: customRenderMenu, selectedOption: value, onOptionSelect: handleMenuChange })));
|
|
1272
|
+
function updateInput(newText) {
|
|
1273
|
+
setInputText(newText);
|
|
1274
|
+
if (newText === "") {
|
|
1275
|
+
setOptions(mapToOptions(initialOptions));
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
function updateSearch() {
|
|
1279
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1280
|
+
const updatedOptions = yield getOptions(inputText);
|
|
1281
|
+
const filteredOptions = updatedOptions.filter(option => isOptionGroup(option) ? option.options.length > 0 : true);
|
|
1282
|
+
setOptions(mapToOptions(filteredOptions));
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
function handleMenuChange(chosenOption) {
|
|
1286
|
+
var _a;
|
|
1287
|
+
onChange(chosenOption);
|
|
1288
|
+
updateInput((_a = chosenOption === null || chosenOption === void 0 ? void 0 : chosenOption.label) !== null && _a !== void 0 ? _a : "");
|
|
1289
|
+
setInputFocused(false);
|
|
1290
|
+
}
|
|
1291
|
+
function handleInputChange(newText) {
|
|
1292
|
+
updateInput(newText);
|
|
1293
|
+
if (allowFreeForm) {
|
|
1294
|
+
onChange({ label: newText });
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
function handleInputBlur() {
|
|
1298
|
+
setInputFocused(false);
|
|
1299
|
+
if (value == undefined || value.label !== inputText) {
|
|
1300
|
+
setInputText("");
|
|
1301
|
+
onChange(undefined);
|
|
1302
|
+
}
|
|
1303
|
+
onBlur && onBlur();
|
|
1304
|
+
}
|
|
1305
|
+
function handleInputFocus() {
|
|
1306
|
+
setInputFocused(true);
|
|
1307
|
+
if (onFocus) {
|
|
1308
|
+
onFocus();
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
function mapToOptions(items) {
|
|
1313
|
+
const retVal = items.reduce((result, item) => {
|
|
1314
|
+
result.push(item);
|
|
1315
|
+
if (isOptionGroup(item) && item.options) {
|
|
1316
|
+
result = result.concat(item.options);
|
|
1317
|
+
}
|
|
1318
|
+
return result;
|
|
1319
|
+
}, []);
|
|
1320
|
+
return retVal;
|
|
1321
|
+
}
|
|
1322
|
+
// Casts the Generics to the forward ref so autocomplete works as expected for consumers
|
|
1323
|
+
const Autocomplete$1 = forwardRef(AutocompleteInternal);
|
|
1324
|
+
|
|
1325
|
+
function isNewAutocompleteProps(props) {
|
|
1326
|
+
return props.version === 2;
|
|
1327
|
+
}
|
|
1328
|
+
function AutocompleteShim(props, ref) {
|
|
1329
|
+
if (isNewAutocompleteProps(props)) {
|
|
1330
|
+
return (React__default.createElement(AutocompleteRebuilt, Object.assign({}, props, { ref: ref })));
|
|
1331
|
+
}
|
|
1332
|
+
return React__default.createElement(Autocomplete$1, Object.assign({}, props, { ref: ref }));
|
|
1333
|
+
}
|
|
1334
|
+
const AutocompleteForwarded = forwardRef(AutocompleteShim);
|
|
1335
|
+
AutocompleteForwarded.displayName = "Autocomplete";
|
|
1336
|
+
const Autocomplete = AutocompleteForwarded;
|
|
1337
|
+
|
|
1338
|
+
export { Autocomplete, BaseAutocompleteMenuWrapper, BaseMenuGroupOption, BaseMenuOption, KeyboardAction, MenuOption, getRequestedIndexChange, isOptionGroup, isOptionSelected, useAutocompleteMenu, useCustomKeyboardNavigation, useKeyboardNavigation, useRepositionMenu };
|