@jobber/components 8.4.0 → 8.6.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 +6 -4
- package/dist/Autocomplete/Autocomplete.types.d.ts +3 -137
- package/dist/Autocomplete/components/FloatingMenu.d.ts +6 -6
- package/dist/Autocomplete/components/MenuList.d.ts +4 -4
- package/dist/Autocomplete/components/PersistentRegion.d.ts +3 -3
- package/dist/Autocomplete/index.cjs +25 -1658
- package/dist/Autocomplete/index.d.ts +3 -30
- package/dist/Autocomplete/index.mjs +25 -1647
- package/dist/Autocomplete/tests/Autocomplete.setup.d.ts +15 -15
- package/dist/Autocomplete/useAutocomplete.d.ts +2 -2
- package/dist/Autocomplete-cjs.js +1306 -0
- package/dist/Autocomplete-es.js +1304 -0
- package/dist/FormField/FormFieldTypes.d.ts +0 -7
- package/dist/InputDate/InputDate.types.d.ts +1 -1
- package/dist/InputEmail/InputEmail.types.d.ts +1 -1
- package/dist/InputNumber/InputNumber.types.d.ts +1 -1
- package/dist/InputPhoneNumber/InputPhoneNumber.types.d.ts +1 -1
- package/dist/InputText/InputText.types.d.ts +1 -5
- package/dist/InputTime/InputTime.types.d.ts +1 -1
- package/dist/Menu-es.js +1 -1
- package/dist/MenuSubmenuTrigger-es.js +1 -1
- package/dist/Select/OptionGroup.d.ts +2 -2
- package/dist/Select/Select.types.d.ts +1 -1
- package/dist/docs/Autocomplete/{AutocompleteV2.md → Autocomplete.md} +13 -107
- package/dist/docs/Banner/Banner.md +3 -3
- package/dist/docs/Box/Box.md +2 -2
- package/dist/docs/Chips/Chips.md +1 -1
- package/dist/docs/DataList/DataList.md +1 -1
- package/dist/docs/FormField/FormField.md +0 -1
- package/dist/docs/Icon/Icon.md +1 -1
- package/dist/docs/InputGroup/InputGroup.md +1 -1
- package/dist/docs/InputPassword/InputPassword.md +0 -1
- package/dist/docs/Menu/Menu.md +1 -1
- package/dist/docs/Modal/Modal.md +3 -3
- package/dist/docs/MultiSelect/MultiSelect.md +1 -1
- package/dist/docs/index.md +1 -2
- package/dist/docs/usage-guidelines/usage-guidelines.md +13 -44
- package/dist/floating-ui.react-dom-es.js +1 -1
- package/dist/floating-ui.react-es.js +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.esm-es.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/sharedHelpers/types.d.ts +0 -4
- package/dist/styles.css +29 -153
- package/package.json +2 -2
- package/dist/Autocomplete/Autocomplete.rebuilt.d.ts +0 -8
- package/dist/Autocomplete/Autocomplete.utils.d.ts +0 -8
- package/dist/Autocomplete/Menu/DefaultMenu.d.ts +0 -16
- package/dist/Autocomplete/Menu/Menu.d.ts +0 -3
- package/dist/Autocomplete/Menu/MenuWrapper.d.ts +0 -23
- package/dist/Autocomplete/Option.d.ts +0 -78
- package/dist/Autocomplete/V1.docgen.d.ts +0 -4
- package/dist/Autocomplete/V2.docgen.d.ts +0 -6
- package/dist/Autocomplete/useKeyboardNavigation.d.ts +0 -35
- package/dist/Autocomplete/useRepositionMenu.d.ts +0 -10
- package/dist/docs/Autocomplete/AutocompleteV1.md +0 -1328
|
@@ -0,0 +1,1304 @@
|
|
|
1
|
+
import React__default, { useState, useRef, useEffect, useCallback, useMemo, forwardRef } from 'react';
|
|
2
|
+
import { u as useFloating, j as useClick, k as useListNavigation, c as useDismiss, d as useInteractions, F as FloatingPortal, h as FloatingFocusManager, l as useTransitionStyles } 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 { b as autoUpdate, o as offset, f as flip, d as size } from './floating-ui.react-dom-es.js';
|
|
8
|
+
import { H as Heading } from './Heading-es.js';
|
|
9
|
+
import { T as Text } from './Text-es.js';
|
|
10
|
+
import { T as Typography } from './Typography-es.js';
|
|
11
|
+
import { I as Icon } from './Icon-es.js';
|
|
12
|
+
import { G as Glimmer } from './Glimmer-es.js';
|
|
13
|
+
import { I as InputText } from './InputText-es.js';
|
|
14
|
+
import { c as FormFieldWrapper } from './FormFieldWrapper-es.js';
|
|
15
|
+
import './tslib.es6-es.js';
|
|
16
|
+
import 'react-hook-form';
|
|
17
|
+
import { m as mergeRefs } from './mergeRefs-es.js';
|
|
18
|
+
import './Button-es.js';
|
|
19
|
+
import { f as filterDataAttributes } from './filterDataAttributes-es.js';
|
|
20
|
+
|
|
21
|
+
/** Stable empty array for cleared/empty selection state. Reuse to avoid reference churn. */
|
|
22
|
+
const EMPTY_SELECTED_VALUES = [];
|
|
23
|
+
|
|
24
|
+
var styles = {"list":"w5g7qggGrr4-","loadingList":"ELlB2Kio7q8-","option":"y9zhi8Wr8QA-","defaultOptionContent":"adJv-3bhYOQ-","icon":"K2phy6IC3TY-","optionActive":"p2CY3TGCdRc-","actionActive":"-tyLeCnCEUI-","action":"dXIHuZjqX84-","section":"gQ86NtBHt-Y-","emptyStateMessage":"uElGEScjH68-","stickyTop":"nXvoRgKgoz8-","scrollRegion":"Ey1zL5E-c74-","persistentHeader":"ei36u5racbA-","persistentFooter":"IDIz9ZHykRw-","textPersistent":"Wx--khA61eA-","multiSelectField":"COW3V1gDtwI-","chipArea":"_1iRpnGn3PaA-","inlineInput":"bHaScorkEaQ-","selectionChip":"sFMc1SJPOSM-","selectionChipActive":"_9Ljs17OW-QM-","selectionChipDisabled":"QYUKkbmNjLE-","chipDismiss":"Wv6D8g2MKPo-","limitText":"UAegmMck3-U-","spinning":"P9cQDL4MZ-s-"};
|
|
25
|
+
|
|
26
|
+
function flattenMenu(menu) {
|
|
27
|
+
const optionItems = [];
|
|
28
|
+
const sections = [];
|
|
29
|
+
const persistentsHeaders = [];
|
|
30
|
+
const persistentsFooters = [];
|
|
31
|
+
menu.forEach(item => {
|
|
32
|
+
if (item.type === "header") {
|
|
33
|
+
persistentsHeaders.push(item);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (item.type === "footer") {
|
|
37
|
+
persistentsFooters.push(item);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const group = item;
|
|
41
|
+
sections.push(group);
|
|
42
|
+
optionItems.push(...group.options);
|
|
43
|
+
});
|
|
44
|
+
return { optionItems, sections, persistentsHeaders, persistentsFooters };
|
|
45
|
+
}
|
|
46
|
+
function buildItemsForGroup(group, optionsFilter) {
|
|
47
|
+
var _a;
|
|
48
|
+
const isSection = group.type === "section";
|
|
49
|
+
const filtered = optionsFilter ? optionsFilter(group.options) : group.options;
|
|
50
|
+
const actions = (_a = group.actions) !== null && _a !== void 0 ? _a : [];
|
|
51
|
+
const result = [];
|
|
52
|
+
// Only render a section header when that section has at least one option after filtering
|
|
53
|
+
if (isSection && filtered.length > 0) {
|
|
54
|
+
result.push({
|
|
55
|
+
kind: "section",
|
|
56
|
+
section: group,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (filtered.length > 0) {
|
|
60
|
+
result.push(...filtered.map(o => ({
|
|
61
|
+
kind: "option",
|
|
62
|
+
value: o,
|
|
63
|
+
})));
|
|
64
|
+
}
|
|
65
|
+
// Only render actions for a group when that group has at least one option after filtering
|
|
66
|
+
if (actions.length > 0 && filtered.length > 0) {
|
|
67
|
+
result.push(...actions.map(action => ({
|
|
68
|
+
kind: "action",
|
|
69
|
+
action,
|
|
70
|
+
origin: "menu",
|
|
71
|
+
})));
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
function buildRenderableList(sections, optionsFilter) {
|
|
76
|
+
const items = [];
|
|
77
|
+
for (const group of sections) {
|
|
78
|
+
items.push(...buildItemsForGroup(group, optionsFilter));
|
|
79
|
+
}
|
|
80
|
+
return items;
|
|
81
|
+
}
|
|
82
|
+
function getNavigableItemAtIndex(activeIndex, renderable) {
|
|
83
|
+
if (activeIndex == null)
|
|
84
|
+
return null;
|
|
85
|
+
let navigableIndex = -1;
|
|
86
|
+
for (const item of renderable) {
|
|
87
|
+
// Ignore sections
|
|
88
|
+
if (item.kind === "section")
|
|
89
|
+
continue;
|
|
90
|
+
navigableIndex += 1;
|
|
91
|
+
if (navigableIndex === activeIndex)
|
|
92
|
+
return item;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
function findNavigableIndexForValue(renderable, equals, selectedValue) {
|
|
97
|
+
let navigableIndex = -1;
|
|
98
|
+
for (const item of renderable) {
|
|
99
|
+
// Ignore sections
|
|
100
|
+
if (item.kind === "section")
|
|
101
|
+
continue;
|
|
102
|
+
navigableIndex += 1;
|
|
103
|
+
if (item.kind === "option" && equals(item.value, selectedValue)) {
|
|
104
|
+
return navigableIndex;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
function invokeActiveItemOnEnter(event, activeIndex, renderable, onSelect, onAction) {
|
|
110
|
+
const activeItem = getNavigableItemAtIndex(activeIndex, renderable);
|
|
111
|
+
if (!activeItem)
|
|
112
|
+
return;
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
if (activeItem.kind === "option") {
|
|
115
|
+
onSelect(activeItem.value);
|
|
116
|
+
}
|
|
117
|
+
else if (activeItem.kind === "action") {
|
|
118
|
+
onAction({
|
|
119
|
+
run: activeItem.action.onClick,
|
|
120
|
+
closeOnRun: activeItem.action.shouldClose,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const MENU_OFFSET = tokens["space-small"];
|
|
126
|
+
const AUTOCOMPLETE_MAX_HEIGHT = 300;
|
|
127
|
+
function useStableSetReference(refs) {
|
|
128
|
+
const latestRefs = useRef(refs);
|
|
129
|
+
latestRefs.current = refs;
|
|
130
|
+
return useCallback((el) => latestRefs.current.setReference(el), []);
|
|
131
|
+
}
|
|
132
|
+
function useAutocompleteListNav({ navigableCount, shouldResetActiveIndexOnClose, onMenuOpen, onMenuClose, selectedIndex, readOnly = false, disabled = false, outsidePressExcludeSelector, }) {
|
|
133
|
+
const [open, setOpen] = useState(false);
|
|
134
|
+
const [activeIndex, setActiveIndex] = useState(null);
|
|
135
|
+
const listRef = useRef([]);
|
|
136
|
+
const { refs, floatingStyles, context } = useFloating({
|
|
137
|
+
placement: "bottom",
|
|
138
|
+
whileElementsMounted: autoUpdate,
|
|
139
|
+
open,
|
|
140
|
+
onOpenChange: (isOpen, _event, reason) => {
|
|
141
|
+
setOpen(isOpen);
|
|
142
|
+
if (isOpen) {
|
|
143
|
+
onMenuOpen === null || onMenuOpen === void 0 ? void 0 : onMenuOpen();
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
if (shouldResetActiveIndexOnClose === null || shouldResetActiveIndexOnClose === void 0 ? void 0 : shouldResetActiveIndexOnClose()) {
|
|
147
|
+
setActiveIndex(null);
|
|
148
|
+
}
|
|
149
|
+
onMenuClose === null || onMenuClose === void 0 ? void 0 : onMenuClose(String(reason !== null && reason !== void 0 ? reason : ""));
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
middleware: [
|
|
153
|
+
offset(MENU_OFFSET),
|
|
154
|
+
flip({ fallbackPlacements: ["top"] }),
|
|
155
|
+
size({
|
|
156
|
+
apply({ availableHeight, rects, elements }) {
|
|
157
|
+
const maxHeight = calculateMaxHeight(availableHeight, {
|
|
158
|
+
maxHeight: AUTOCOMPLETE_MAX_HEIGHT,
|
|
159
|
+
});
|
|
160
|
+
Object.assign(elements.floating.style, {
|
|
161
|
+
maxHeight: `${maxHeight}px`,
|
|
162
|
+
width: `${rects.reference.width}px`,
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
}),
|
|
166
|
+
],
|
|
167
|
+
});
|
|
168
|
+
const enabled = !readOnly && !disabled;
|
|
169
|
+
const click = useClick(context, {
|
|
170
|
+
enabled,
|
|
171
|
+
toggle: false, // Only open, never close on click
|
|
172
|
+
});
|
|
173
|
+
const listNav = useListNavigation(context, {
|
|
174
|
+
listRef,
|
|
175
|
+
activeIndex,
|
|
176
|
+
selectedIndex,
|
|
177
|
+
scrollItemIntoView: {
|
|
178
|
+
behavior: "smooth",
|
|
179
|
+
block: "end",
|
|
180
|
+
},
|
|
181
|
+
loop: true,
|
|
182
|
+
onNavigate: setActiveIndex,
|
|
183
|
+
virtual: true,
|
|
184
|
+
enabled: open,
|
|
185
|
+
openOnArrowKeyDown: false,
|
|
186
|
+
focusItemOnOpen: "auto",
|
|
187
|
+
focusItemOnHover: false,
|
|
188
|
+
});
|
|
189
|
+
const dismiss = useDismiss(context, {
|
|
190
|
+
outsidePress: outsidePressExcludeSelector
|
|
191
|
+
? (event) => {
|
|
192
|
+
var _a, _b, _c, _d;
|
|
193
|
+
const target = event.target;
|
|
194
|
+
const insideRef = (_b = (_a = context.elements.domReference) === null || _a === void 0 ? void 0 : _a.contains(target)) !== null && _b !== void 0 ? _b : false;
|
|
195
|
+
const insideFloating = (_d = (_c = context.elements.floating) === null || _c === void 0 ? void 0 : _c.contains(target)) !== null && _d !== void 0 ? _d : false;
|
|
196
|
+
const insideExclude = event.target.closest(outsidePressExcludeSelector);
|
|
197
|
+
return !(insideRef || insideFloating || insideExclude);
|
|
198
|
+
}
|
|
199
|
+
: true,
|
|
200
|
+
escapeKey: true,
|
|
201
|
+
outsidePressEvent: "click",
|
|
202
|
+
});
|
|
203
|
+
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([click, listNav, dismiss]);
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
listRef.current.length = navigableCount;
|
|
206
|
+
setActiveIndex(prev => {
|
|
207
|
+
if (navigableCount <= 0)
|
|
208
|
+
return null;
|
|
209
|
+
if (prev == null)
|
|
210
|
+
return null;
|
|
211
|
+
return prev >= navigableCount ? navigableCount - 1 : prev;
|
|
212
|
+
});
|
|
213
|
+
}, [navigableCount, setActiveIndex, listRef]);
|
|
214
|
+
const setReferenceElement = useStableSetReference(refs);
|
|
215
|
+
return {
|
|
216
|
+
refs,
|
|
217
|
+
floatingStyles,
|
|
218
|
+
context,
|
|
219
|
+
getReferenceProps,
|
|
220
|
+
getFloatingProps,
|
|
221
|
+
getItemProps,
|
|
222
|
+
activeIndex,
|
|
223
|
+
setActiveIndex,
|
|
224
|
+
listRef,
|
|
225
|
+
open,
|
|
226
|
+
setOpen,
|
|
227
|
+
setReferenceElement,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Handler that prevents default pointer behavior.
|
|
233
|
+
* Used to prevent blur/focus issues when clicking on non-interactive menu elements.
|
|
234
|
+
*/
|
|
235
|
+
function preventDefaultPointerDown(e) {
|
|
236
|
+
e.preventDefault();
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Creates a handler for pointer down events on interactive menu items (options/actions).
|
|
240
|
+
* Prevents default to avoid blur and sets flag for focus management.
|
|
241
|
+
*
|
|
242
|
+
* @param isHandlingMenuInteractionRef - Ref to track if a menu interaction is in progress
|
|
243
|
+
* @returns A pointer down event handler
|
|
244
|
+
*/
|
|
245
|
+
function createInteractionPointerDownHandler(isHandlingMenuInteractionRef) {
|
|
246
|
+
return (e) => {
|
|
247
|
+
e.preventDefault();
|
|
248
|
+
// Set flag to prevent blur/focus handlers from interfering
|
|
249
|
+
isHandlingMenuInteractionRef.current = true;
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Keeping this hook cohesive improves readability by centralizing related
|
|
254
|
+
// interactions and state transitions.
|
|
255
|
+
// eslint-disable-next-line max-statements
|
|
256
|
+
function useAutocomplete(props, inputRef) {
|
|
257
|
+
const { menu, emptyActions, getOptionLabel: getOptionLabelProp, isOptionEqualToValue, inputValue, onInputChange, value, onChange, multiple, openOnFocus = true, readOnly = false, disabled = false, debounce: debounceMs = 300, } = props;
|
|
258
|
+
const isHandlingMenuInteractionRef = useRef(false);
|
|
259
|
+
// TODO: Clean up the types in these refs by enhancing the type system in useCallbackRef
|
|
260
|
+
const getOptionLabelPropRef = useCallbackRef((opt) => getOptionLabelProp === null || getOptionLabelProp === void 0 ? void 0 : getOptionLabelProp(opt));
|
|
261
|
+
const getOptionLabel = useCallback((opt) => {
|
|
262
|
+
const maybe = getOptionLabelPropRef(opt);
|
|
263
|
+
return maybe !== null && maybe !== void 0 ? maybe : opt.label;
|
|
264
|
+
}, [getOptionLabelPropRef]);
|
|
265
|
+
const isOptionEqualToValueRef = useCallbackRef((a, b) => isOptionEqualToValue === null || isOptionEqualToValue === void 0 ? void 0 : isOptionEqualToValue(a, b));
|
|
266
|
+
const equals = useCallback((a, b) => {
|
|
267
|
+
const custom = isOptionEqualToValueRef(a, b);
|
|
268
|
+
return custom != null ? custom : getOptionLabel(a) === getOptionLabel(b);
|
|
269
|
+
}, [isOptionEqualToValueRef, getOptionLabel]);
|
|
270
|
+
const isOptionSelected = useCallback((opt) => {
|
|
271
|
+
var _a;
|
|
272
|
+
if (multiple) {
|
|
273
|
+
const current = (_a = value) !== null && _a !== void 0 ? _a : EMPTY_SELECTED_VALUES;
|
|
274
|
+
return current.some(v => equals(v, opt));
|
|
275
|
+
}
|
|
276
|
+
const current = value;
|
|
277
|
+
return current != null ? equals(current, opt) : false;
|
|
278
|
+
}, [multiple, value, equals]);
|
|
279
|
+
const flatInitial = useMemo(() => flattenMenu(menu), [menu]);
|
|
280
|
+
const sections = flatInitial.sections;
|
|
281
|
+
const optionItems = flatInitial.optionItems;
|
|
282
|
+
const persistentsHeaders = flatInitial.persistentsHeaders;
|
|
283
|
+
const persistentsFooters = flatInitial.persistentsFooters;
|
|
284
|
+
// Stable wrappers for function props
|
|
285
|
+
const inputEqualsOptionRef = useCallbackRef((text, o) => {
|
|
286
|
+
const fn = props.inputEqualsOption;
|
|
287
|
+
return fn ? fn(text, o) : undefined;
|
|
288
|
+
});
|
|
289
|
+
const inputEquals = useCallback((text, o) => {
|
|
290
|
+
const custom = inputEqualsOptionRef(text, o);
|
|
291
|
+
return custom != null ? custom : getOptionLabel(o) === text;
|
|
292
|
+
}, [inputEqualsOptionRef, getOptionLabel]);
|
|
293
|
+
// inputValue changes very often, is this worth memoizing?
|
|
294
|
+
const exactLabelMatch = useMemo(() => {
|
|
295
|
+
return optionItems.some(o => inputEquals(inputValue, o));
|
|
296
|
+
}, [optionItems, inputEquals, inputValue]);
|
|
297
|
+
const lastInputWasUser = useRef(false);
|
|
298
|
+
const [debouncedInputValue, setDebouncedInputValue] = useState(inputValue);
|
|
299
|
+
const debouncedSetter = useDebounce(setDebouncedInputValue, debounceMs);
|
|
300
|
+
useEffect(() => {
|
|
301
|
+
// Skip debounce when clearing input for immediate feedback, preventing flickering of last selected item
|
|
302
|
+
if (debounceMs === 0 || inputValue === "") {
|
|
303
|
+
setDebouncedInputValue(inputValue);
|
|
304
|
+
// Cancel any pending debounced call so a stale intermediate value
|
|
305
|
+
// (e.g. "P" while deleting "Pipe…") doesn't overwrite the immediate set.
|
|
306
|
+
debouncedSetter.cancel();
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
debouncedSetter(inputValue);
|
|
310
|
+
}, [inputValue, debounceMs, debouncedSetter]);
|
|
311
|
+
const filterOptionsRef = useCallbackRef((opts, term) => {
|
|
312
|
+
const fn = (typeof props.filterOptions === "function"
|
|
313
|
+
? props.filterOptions
|
|
314
|
+
: undefined) || undefined;
|
|
315
|
+
return fn ? fn(opts, term) : undefined;
|
|
316
|
+
});
|
|
317
|
+
const applyFilter = useCallback((opts, term) => {
|
|
318
|
+
if (props.filterOptions === false)
|
|
319
|
+
return opts;
|
|
320
|
+
const override = filterOptionsRef(opts, term);
|
|
321
|
+
if (override)
|
|
322
|
+
return override;
|
|
323
|
+
const lowered = term.toLowerCase();
|
|
324
|
+
return opts.filter(opt => getOptionLabel(opt).toLowerCase().includes(lowered));
|
|
325
|
+
}, [props.filterOptions, filterOptionsRef, getOptionLabel]);
|
|
326
|
+
const renderable = useMemo(() => {
|
|
327
|
+
const filter = (opts) => {
|
|
328
|
+
if (exactLabelMatch && !lastInputWasUser.current)
|
|
329
|
+
return opts;
|
|
330
|
+
return applyFilter(opts, debouncedInputValue);
|
|
331
|
+
};
|
|
332
|
+
const items = buildRenderableList(sections, filter);
|
|
333
|
+
const hasAnyOptions = items.some(i => i.kind === "option");
|
|
334
|
+
if (!hasAnyOptions) {
|
|
335
|
+
if (emptyActions) {
|
|
336
|
+
const derived = typeof emptyActions === "function"
|
|
337
|
+
? emptyActions({ inputValue })
|
|
338
|
+
: emptyActions;
|
|
339
|
+
return derived.map(act => ({
|
|
340
|
+
kind: "action",
|
|
341
|
+
action: act,
|
|
342
|
+
origin: "empty",
|
|
343
|
+
}));
|
|
344
|
+
}
|
|
345
|
+
// No options and no emptyActions: render empty state
|
|
346
|
+
return [];
|
|
347
|
+
}
|
|
348
|
+
return items;
|
|
349
|
+
}, [
|
|
350
|
+
sections,
|
|
351
|
+
applyFilter,
|
|
352
|
+
debouncedInputValue,
|
|
353
|
+
exactLabelMatch,
|
|
354
|
+
emptyActions,
|
|
355
|
+
inputValue,
|
|
356
|
+
]);
|
|
357
|
+
// This is only options
|
|
358
|
+
const optionCount = renderable.reduce((count, item) => count + (item.kind === "option" ? 1 : 0), 0);
|
|
359
|
+
const hasSelection = useMemo(() => {
|
|
360
|
+
var _a;
|
|
361
|
+
if (multiple) {
|
|
362
|
+
const current = (_a = value) !== null && _a !== void 0 ? _a : EMPTY_SELECTED_VALUES;
|
|
363
|
+
return Array.isArray(current) && current.length > 0;
|
|
364
|
+
}
|
|
365
|
+
return value != null;
|
|
366
|
+
}, [multiple, value]);
|
|
367
|
+
const headerInteractivePersistents = persistentsHeaders.filter(p => Boolean(p.onClick));
|
|
368
|
+
const footerInteractivePersistents = persistentsFooters.filter(p => Boolean(p.onClick));
|
|
369
|
+
const mainNavigableCount = renderable.reduce((c, i) => c + (i.kind === "section" ? 0 : 1), 0);
|
|
370
|
+
const totalNavigableCount = headerInteractivePersistents.length +
|
|
371
|
+
mainNavigableCount +
|
|
372
|
+
footerInteractivePersistents.length;
|
|
373
|
+
// Compute the currently selected index in the global navigable list (header -> middle -> footer).
|
|
374
|
+
// In multiple mode this is always null — there is no single "selected" index, and
|
|
375
|
+
// floating-ui would otherwise jump the highlight whenever the value array changes.
|
|
376
|
+
const selectedIndex = useMemo(() => {
|
|
377
|
+
if (multiple)
|
|
378
|
+
return null;
|
|
379
|
+
const selectedValue = value;
|
|
380
|
+
if (!selectedValue)
|
|
381
|
+
return null;
|
|
382
|
+
const middleIndex = findNavigableIndexForValue(renderable, equals, selectedValue);
|
|
383
|
+
if (middleIndex == null)
|
|
384
|
+
return null;
|
|
385
|
+
return headerInteractivePersistents.length + middleIndex;
|
|
386
|
+
}, [
|
|
387
|
+
multiple,
|
|
388
|
+
value,
|
|
389
|
+
renderable,
|
|
390
|
+
equals,
|
|
391
|
+
headerInteractivePersistents.length,
|
|
392
|
+
]);
|
|
393
|
+
const { refs, floatingStyles, context, getReferenceProps, getFloatingProps, getItemProps, activeIndex, setActiveIndex, listRef, open, setOpen, setReferenceElement, } = useAutocompleteListNav({
|
|
394
|
+
navigableCount: totalNavigableCount,
|
|
395
|
+
shouldResetActiveIndexOnClose: () => !hasSelection,
|
|
396
|
+
selectedIndex,
|
|
397
|
+
readOnly,
|
|
398
|
+
disabled,
|
|
399
|
+
outsidePressExcludeSelector: multiple
|
|
400
|
+
? "[data-testid='ATL-Autocomplete-chipArea']"
|
|
401
|
+
: undefined,
|
|
402
|
+
onMenuOpen: () => {
|
|
403
|
+
if (multiple)
|
|
404
|
+
return;
|
|
405
|
+
const selectedValue = value;
|
|
406
|
+
if (selectedValue) {
|
|
407
|
+
const selectedNavigableIndex = findNavigableIndexForValue(renderable, equals, selectedValue);
|
|
408
|
+
if (selectedNavigableIndex != null) {
|
|
409
|
+
setActiveIndex(selectedNavigableIndex);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
setActiveIndex(null);
|
|
414
|
+
},
|
|
415
|
+
onMenuClose: () => {
|
|
416
|
+
if (props.allowFreeForm !== true) {
|
|
417
|
+
const hasText = inputValue.trim().length > 0;
|
|
418
|
+
if (hasText && !hasSelection) {
|
|
419
|
+
lastInputWasUser.current = false;
|
|
420
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange("");
|
|
421
|
+
setActiveIndex(null);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
});
|
|
426
|
+
const prevOpenRef = useRef(false);
|
|
427
|
+
// TODO: Leverage FloatingUI useFocus, and onArrowKeyDown to manage open state and allow onOpenChange to be the source of truth
|
|
428
|
+
// JOB-154442
|
|
429
|
+
useEffect(() => {
|
|
430
|
+
var _a, _b;
|
|
431
|
+
if (open && !prevOpenRef.current) {
|
|
432
|
+
(_a = props.onOpen) === null || _a === void 0 ? void 0 : _a.call(props);
|
|
433
|
+
}
|
|
434
|
+
else if (!open && prevOpenRef.current) {
|
|
435
|
+
(_b = props.onClose) === null || _b === void 0 ? void 0 : _b.call(props);
|
|
436
|
+
}
|
|
437
|
+
prevOpenRef.current = open;
|
|
438
|
+
}, [open, props.onOpen, props.onClose]);
|
|
439
|
+
function selectOption(option) {
|
|
440
|
+
var _a;
|
|
441
|
+
if (multiple) {
|
|
442
|
+
const current = (_a = value) !== null && _a !== void 0 ? _a : EMPTY_SELECTED_VALUES;
|
|
443
|
+
const exists = current.some(v => equals(v, option));
|
|
444
|
+
const next = exists
|
|
445
|
+
? current.filter(v => !equals(v, option))
|
|
446
|
+
: [...current, option];
|
|
447
|
+
onChange((next.length === 0 ? EMPTY_SELECTED_VALUES : next));
|
|
448
|
+
lastInputWasUser.current = false;
|
|
449
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange("");
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
onChange(option);
|
|
453
|
+
lastInputWasUser.current = false;
|
|
454
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange(getOptionLabel(option));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
function reHighlightSelectedItem() {
|
|
458
|
+
const selectedValue = value;
|
|
459
|
+
if (!selectedValue)
|
|
460
|
+
return;
|
|
461
|
+
const idx = findNavigableIndexForValue(renderable, equals, selectedValue);
|
|
462
|
+
if (idx != null)
|
|
463
|
+
setActiveIndex(idx);
|
|
464
|
+
}
|
|
465
|
+
function tryCommitFreeFormOnEnter() {
|
|
466
|
+
if (props.allowFreeForm !== true)
|
|
467
|
+
return false;
|
|
468
|
+
if (open && activeIndex != null)
|
|
469
|
+
return false;
|
|
470
|
+
const inputText = inputValue.trim();
|
|
471
|
+
if (inputText.length === 0)
|
|
472
|
+
return false;
|
|
473
|
+
commitFromInputText(inputText);
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
const onSelection = useCallback((option) => {
|
|
477
|
+
var _a;
|
|
478
|
+
selectOption(option);
|
|
479
|
+
if (!multiple) {
|
|
480
|
+
setOpen(false);
|
|
481
|
+
}
|
|
482
|
+
(_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
483
|
+
}, [selectOption, setOpen, multiple, inputRef]);
|
|
484
|
+
const onAction = useCallback((action) => {
|
|
485
|
+
var _a;
|
|
486
|
+
action.run();
|
|
487
|
+
setActiveIndex(null);
|
|
488
|
+
if (action.closeOnRun !== false)
|
|
489
|
+
setOpen(false);
|
|
490
|
+
(_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
491
|
+
}, [setOpen, setActiveIndex, inputRef]);
|
|
492
|
+
/**
|
|
493
|
+
* Handler for mousedown on interactive menu items (options/actions)
|
|
494
|
+
* Prevents default to avoid blur and sets flag for focus management
|
|
495
|
+
*/
|
|
496
|
+
const onInteractionPointerDown = useMemo(() => createInteractionPointerDownHandler(isHandlingMenuInteractionRef), []);
|
|
497
|
+
function applyFreeFormValue(freeFormCreated) {
|
|
498
|
+
var _a;
|
|
499
|
+
const nextValue = multiple
|
|
500
|
+
? [...((_a = value) !== null && _a !== void 0 ? _a : EMPTY_SELECTED_VALUES), freeFormCreated]
|
|
501
|
+
: freeFormCreated;
|
|
502
|
+
props.onChange(nextValue);
|
|
503
|
+
if (multiple) {
|
|
504
|
+
lastInputWasUser.current = false;
|
|
505
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange("");
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
function commitFromInputText(inputText) {
|
|
509
|
+
var _a;
|
|
510
|
+
if (inputText.length === 0)
|
|
511
|
+
return false;
|
|
512
|
+
const match = optionItems.find(o => inputEquals(inputText, o));
|
|
513
|
+
if (match) {
|
|
514
|
+
onSelection(match);
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
setOpen(false);
|
|
518
|
+
if (props.allowFreeForm !== true)
|
|
519
|
+
return false;
|
|
520
|
+
const freeFormCreated = (_a = props.createFreeFormValue) === null || _a === void 0 ? void 0 : _a.call(props, inputText);
|
|
521
|
+
if (!freeFormCreated)
|
|
522
|
+
return false;
|
|
523
|
+
applyFreeFormValue(freeFormCreated);
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
const tryRestoreInputToSelectedLabel = useCallback(() => {
|
|
527
|
+
if (props.allowFreeForm === true)
|
|
528
|
+
return;
|
|
529
|
+
if (multiple) {
|
|
530
|
+
if (inputValue.trim().length > 0) {
|
|
531
|
+
lastInputWasUser.current = false;
|
|
532
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange("");
|
|
533
|
+
}
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
const selectedValue = value;
|
|
537
|
+
if (!selectedValue)
|
|
538
|
+
return;
|
|
539
|
+
const selectedLabel = getOptionLabel(selectedValue);
|
|
540
|
+
if (inputValue === selectedLabel)
|
|
541
|
+
return;
|
|
542
|
+
lastInputWasUser.current = false;
|
|
543
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange(selectedLabel);
|
|
544
|
+
}, [
|
|
545
|
+
props.allowFreeForm,
|
|
546
|
+
getOptionLabel,
|
|
547
|
+
inputValue,
|
|
548
|
+
onInputChange,
|
|
549
|
+
multiple,
|
|
550
|
+
value,
|
|
551
|
+
]);
|
|
552
|
+
const onInputFocus = useCallback((event) => {
|
|
553
|
+
var _a;
|
|
554
|
+
if (!readOnly && openOnFocus && !isHandlingMenuInteractionRef.current) {
|
|
555
|
+
setOpen(true);
|
|
556
|
+
}
|
|
557
|
+
// Only call user's onFocus for genuine focus events, not programmatic restorations
|
|
558
|
+
if (!isHandlingMenuInteractionRef.current) {
|
|
559
|
+
(_a = props.onFocus) === null || _a === void 0 ? void 0 : _a.call(props, event);
|
|
560
|
+
}
|
|
561
|
+
isHandlingMenuInteractionRef.current = false;
|
|
562
|
+
}, [props.onFocus, readOnly, openOnFocus, setOpen]);
|
|
563
|
+
const onInputBlur = useCallback((event) => {
|
|
564
|
+
var _a, _b;
|
|
565
|
+
if (isHandlingMenuInteractionRef.current) {
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
if (readOnly) {
|
|
569
|
+
(_a = props.onBlur) === null || _a === void 0 ? void 0 : _a.call(props, event);
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (props.allowFreeForm === true) {
|
|
573
|
+
const inputText = inputValue.trim();
|
|
574
|
+
if (inputText.length > 0)
|
|
575
|
+
commitFromInputText(inputText);
|
|
576
|
+
}
|
|
577
|
+
else {
|
|
578
|
+
tryRestoreInputToSelectedLabel();
|
|
579
|
+
}
|
|
580
|
+
lastInputWasUser.current = false;
|
|
581
|
+
(_b = props.onBlur) === null || _b === void 0 ? void 0 : _b.call(props, event);
|
|
582
|
+
}, [
|
|
583
|
+
readOnly,
|
|
584
|
+
props.allowFreeForm,
|
|
585
|
+
inputValue,
|
|
586
|
+
props.onBlur,
|
|
587
|
+
tryRestoreInputToSelectedLabel,
|
|
588
|
+
setOpen,
|
|
589
|
+
]);
|
|
590
|
+
function getRegionByActiveIndex(index) {
|
|
591
|
+
const headerCount = headerInteractivePersistents.length;
|
|
592
|
+
const mainCount = mainNavigableCount;
|
|
593
|
+
if (index < headerCount)
|
|
594
|
+
return { region: "header", regionIndex: index };
|
|
595
|
+
const middleIndex = index - headerCount;
|
|
596
|
+
if (middleIndex < mainCount) {
|
|
597
|
+
return { region: "middle", regionIndex: middleIndex };
|
|
598
|
+
}
|
|
599
|
+
return {
|
|
600
|
+
region: "footer",
|
|
601
|
+
regionIndex: middleIndex - mainCount,
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
function computeInitialIndexForArrowUp() {
|
|
605
|
+
// If there are interactive footers, prefer the very last navigable item
|
|
606
|
+
if (footerInteractivePersistents.length > 0) {
|
|
607
|
+
return totalNavigableCount > 0 ? totalNavigableCount - 1 : null;
|
|
608
|
+
}
|
|
609
|
+
// Otherwise, prefer the last OPTION (not action), matching legacy behavior
|
|
610
|
+
let navigable = -1;
|
|
611
|
+
let lastOptionIdx = -1;
|
|
612
|
+
for (const item of renderable) {
|
|
613
|
+
if (item.kind === "section")
|
|
614
|
+
continue;
|
|
615
|
+
navigable += 1;
|
|
616
|
+
if (item.kind === "option")
|
|
617
|
+
lastOptionIdx = navigable;
|
|
618
|
+
}
|
|
619
|
+
if (lastOptionIdx >= 0) {
|
|
620
|
+
return headerInteractivePersistents.length + lastOptionIdx;
|
|
621
|
+
}
|
|
622
|
+
return totalNavigableCount > 0 ? totalNavigableCount - 1 : null;
|
|
623
|
+
}
|
|
624
|
+
function handleArrowNavigation(key, event) {
|
|
625
|
+
if (!open) {
|
|
626
|
+
setOpen(true);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
if (activeIndex == null) {
|
|
630
|
+
setActiveIndex(key === "ArrowDown" ? 0 : computeInitialIndexForArrowUp());
|
|
631
|
+
event.preventDefault();
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
function handleEnterKey(event) {
|
|
635
|
+
if (tryCommitFreeFormOnEnter()) {
|
|
636
|
+
event.preventDefault();
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
if (!open || activeIndex == null)
|
|
640
|
+
return;
|
|
641
|
+
const { region, regionIndex } = getRegionByActiveIndex(activeIndex);
|
|
642
|
+
if (region === "middle") {
|
|
643
|
+
invokeActiveItemOnEnter(event, regionIndex, renderable, onSelection, onAction);
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
const persistent = region === "header"
|
|
647
|
+
? headerInteractivePersistents[regionIndex]
|
|
648
|
+
: footerInteractivePersistents[regionIndex];
|
|
649
|
+
if (persistent === null || persistent === void 0 ? void 0 : persistent.onClick) {
|
|
650
|
+
onAction({
|
|
651
|
+
run: persistent.onClick,
|
|
652
|
+
closeOnRun: persistent.shouldClose,
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
const removeLastSelection = useCallback(() => {
|
|
657
|
+
var _a;
|
|
658
|
+
if (!multiple || readOnly)
|
|
659
|
+
return;
|
|
660
|
+
const current = (_a = value) !== null && _a !== void 0 ? _a : EMPTY_SELECTED_VALUES;
|
|
661
|
+
if (current.length > 0) {
|
|
662
|
+
const next = current.slice(0, -1);
|
|
663
|
+
onChange((next.length === 0 ? EMPTY_SELECTED_VALUES : next));
|
|
664
|
+
}
|
|
665
|
+
}, [multiple, readOnly, value, onChange]);
|
|
666
|
+
const removeSelection = useCallback((option) => {
|
|
667
|
+
var _a;
|
|
668
|
+
if (readOnly)
|
|
669
|
+
return;
|
|
670
|
+
if (multiple) {
|
|
671
|
+
const current = (_a = value) !== null && _a !== void 0 ? _a : EMPTY_SELECTED_VALUES;
|
|
672
|
+
const next = current.filter(v => !equals(v, option));
|
|
673
|
+
onChange((next.length === 0
|
|
674
|
+
? EMPTY_SELECTED_VALUES
|
|
675
|
+
: next));
|
|
676
|
+
}
|
|
677
|
+
else {
|
|
678
|
+
const current = value;
|
|
679
|
+
if (current && equals(current, option)) {
|
|
680
|
+
onChange(undefined);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}, [readOnly, multiple, value, equals, onChange]);
|
|
684
|
+
const onInputKeyDown = useCallback((event) => {
|
|
685
|
+
const key = event.key;
|
|
686
|
+
if (key === "Backspace" && multiple && inputValue === "") {
|
|
687
|
+
removeLastSelection();
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
if (key !== "ArrowDown" && key !== "ArrowUp" && key !== "Enter")
|
|
691
|
+
return;
|
|
692
|
+
if (key === "ArrowDown" || key === "ArrowUp") {
|
|
693
|
+
handleArrowNavigation(key, event);
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
handleEnterKey(event);
|
|
697
|
+
}, [open, onSelection, onAction, multiple, inputValue, removeLastSelection]);
|
|
698
|
+
const onInputChangeFromUser = useCallback((val) => {
|
|
699
|
+
lastInputWasUser.current = true;
|
|
700
|
+
const isEmpty = val.trim().length === 0;
|
|
701
|
+
if (isEmpty) {
|
|
702
|
+
setActiveIndex(null);
|
|
703
|
+
if (!multiple && hasSelection) {
|
|
704
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(undefined);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
else if (val.length > inputValue.length) {
|
|
708
|
+
setActiveIndex(null);
|
|
709
|
+
}
|
|
710
|
+
else if (val.length < inputValue.length &&
|
|
711
|
+
!multiple &&
|
|
712
|
+
open &&
|
|
713
|
+
hasSelection) {
|
|
714
|
+
reHighlightSelectedItem();
|
|
715
|
+
}
|
|
716
|
+
if (!readOnly) {
|
|
717
|
+
setOpen((!isEmpty && !props.allowFreeForm) || openOnFocus);
|
|
718
|
+
}
|
|
719
|
+
onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange(val);
|
|
720
|
+
}, [
|
|
721
|
+
onInputChange,
|
|
722
|
+
inputValue,
|
|
723
|
+
setActiveIndex,
|
|
724
|
+
readOnly,
|
|
725
|
+
props.allowFreeForm,
|
|
726
|
+
openOnFocus,
|
|
727
|
+
setOpen,
|
|
728
|
+
multiple,
|
|
729
|
+
hasSelection,
|
|
730
|
+
onChange,
|
|
731
|
+
open,
|
|
732
|
+
]);
|
|
733
|
+
const clearAll = useCallback(() => {
|
|
734
|
+
if (multiple) {
|
|
735
|
+
onChange(EMPTY_SELECTED_VALUES);
|
|
736
|
+
}
|
|
737
|
+
else {
|
|
738
|
+
onChange(undefined);
|
|
739
|
+
}
|
|
740
|
+
onInputChangeFromUser("");
|
|
741
|
+
}, [multiple, onChange, onInputChangeFromUser]);
|
|
742
|
+
return {
|
|
743
|
+
// rendering data
|
|
744
|
+
renderable,
|
|
745
|
+
optionCount,
|
|
746
|
+
persistentsHeaders,
|
|
747
|
+
persistentsFooters,
|
|
748
|
+
headerInteractiveCount: headerInteractivePersistents.length,
|
|
749
|
+
middleNavigableCount: mainNavigableCount,
|
|
750
|
+
getOptionLabel,
|
|
751
|
+
isOptionSelected,
|
|
752
|
+
// floating-ui
|
|
753
|
+
refs,
|
|
754
|
+
floatingStyles,
|
|
755
|
+
context,
|
|
756
|
+
getReferenceProps,
|
|
757
|
+
getFloatingProps,
|
|
758
|
+
getItemProps,
|
|
759
|
+
// state
|
|
760
|
+
open,
|
|
761
|
+
setOpen,
|
|
762
|
+
activeIndex,
|
|
763
|
+
setActiveIndex,
|
|
764
|
+
listRef,
|
|
765
|
+
// actions
|
|
766
|
+
onSelection,
|
|
767
|
+
onAction,
|
|
768
|
+
onInteractionPointerDown,
|
|
769
|
+
removeSelection,
|
|
770
|
+
clearAll,
|
|
771
|
+
// input handlers
|
|
772
|
+
onInputChangeFromUser,
|
|
773
|
+
onInputBlur,
|
|
774
|
+
onInputFocus,
|
|
775
|
+
onInputKeyDown,
|
|
776
|
+
// ref attachment
|
|
777
|
+
setReferenceElement,
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
function useChipNavigation({ selectedValues, inputValue, readOnly, removeSelection, onInputKeyDown, onInputBlur, }) {
|
|
782
|
+
const [rawActiveChipIndex, setRawActiveChipIndex] = useState(null);
|
|
783
|
+
const [previousInputValue, setPreviousInputValue] = useState(inputValue);
|
|
784
|
+
const activeChipIndex = clampActiveIndex(rawActiveChipIndex, selectedValues.length);
|
|
785
|
+
if (previousInputValue !== inputValue) {
|
|
786
|
+
setPreviousInputValue(inputValue);
|
|
787
|
+
if (inputValue !== "" && rawActiveChipIndex !== null) {
|
|
788
|
+
setRawActiveChipIndex(null);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
const handleActiveChipKey = useCallback(
|
|
792
|
+
// eslint-disable-next-line max-statements
|
|
793
|
+
(event) => {
|
|
794
|
+
if (activeChipIndex === null)
|
|
795
|
+
return false;
|
|
796
|
+
const { key } = event;
|
|
797
|
+
if (key === "ArrowLeft") {
|
|
798
|
+
event.preventDefault();
|
|
799
|
+
setRawActiveChipIndex(i => Math.max(0, (i !== null && i !== void 0 ? i : 0) - 1));
|
|
800
|
+
return true;
|
|
801
|
+
}
|
|
802
|
+
if (key === "ArrowRight") {
|
|
803
|
+
event.preventDefault();
|
|
804
|
+
setRawActiveChipIndex(activeChipIndex + 1 >= selectedValues.length
|
|
805
|
+
? null
|
|
806
|
+
: activeChipIndex + 1);
|
|
807
|
+
return true;
|
|
808
|
+
}
|
|
809
|
+
if (key === "Backspace" || key === "Delete") {
|
|
810
|
+
event.preventDefault();
|
|
811
|
+
const option = selectedValues[activeChipIndex];
|
|
812
|
+
const newLen = selectedValues.length - 1;
|
|
813
|
+
if (newLen === 0) {
|
|
814
|
+
setRawActiveChipIndex(null);
|
|
815
|
+
}
|
|
816
|
+
else if (activeChipIndex >= newLen) {
|
|
817
|
+
setRawActiveChipIndex(newLen - 1);
|
|
818
|
+
}
|
|
819
|
+
removeSelection(option);
|
|
820
|
+
return true;
|
|
821
|
+
}
|
|
822
|
+
if (key === "Escape") {
|
|
823
|
+
setRawActiveChipIndex(null);
|
|
824
|
+
return true;
|
|
825
|
+
}
|
|
826
|
+
setRawActiveChipIndex(null);
|
|
827
|
+
return false;
|
|
828
|
+
}, [activeChipIndex, selectedValues, removeSelection]);
|
|
829
|
+
const onKeyDown = useCallback((event) => {
|
|
830
|
+
if (handleActiveChipKey(event))
|
|
831
|
+
return;
|
|
832
|
+
if (event.key === "ArrowLeft" &&
|
|
833
|
+
!readOnly &&
|
|
834
|
+
inputValue === "" &&
|
|
835
|
+
selectedValues.length > 0) {
|
|
836
|
+
event.preventDefault();
|
|
837
|
+
setRawActiveChipIndex(selectedValues.length - 1);
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
onInputKeyDown(event);
|
|
841
|
+
}, [handleActiveChipKey, selectedValues, inputValue, readOnly, onInputKeyDown]);
|
|
842
|
+
const onBlur = useCallback((event) => {
|
|
843
|
+
setRawActiveChipIndex(null);
|
|
844
|
+
onInputBlur(event);
|
|
845
|
+
}, [onInputBlur]);
|
|
846
|
+
return { activeChipIndex, onKeyDown, onBlur };
|
|
847
|
+
}
|
|
848
|
+
function clampActiveIndex(index, length) {
|
|
849
|
+
if (index === null)
|
|
850
|
+
return null;
|
|
851
|
+
if (length === 0)
|
|
852
|
+
return null;
|
|
853
|
+
if (index >= length)
|
|
854
|
+
return length - 1;
|
|
855
|
+
return index;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
function MenuList({ items, activeIndex, indexOffset = 0, getItemProps, listRef, listboxId, customRenderOption, customRenderSection, customRenderAction, getOptionLabel, onSelect, onAction, onInteractionPointerDown, isOptionSelected, slotOverrides, }) {
|
|
859
|
+
let navigableIndex = -1;
|
|
860
|
+
function renderItemNode(item) {
|
|
861
|
+
var _a, _b, _c, _d, _e, _f;
|
|
862
|
+
if (item.kind === "section") {
|
|
863
|
+
return handleSectionRendering({
|
|
864
|
+
section: item.section,
|
|
865
|
+
customRenderSection,
|
|
866
|
+
sectionClassName: (_a = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.section) === null || _a === void 0 ? void 0 : _a.className,
|
|
867
|
+
sectionStyle: (_b = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.section) === null || _b === void 0 ? void 0 : _b.style,
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
if (item.kind === "option") {
|
|
871
|
+
const result = handleOptionRendering({
|
|
872
|
+
option: item.value,
|
|
873
|
+
activeIndex,
|
|
874
|
+
navigableIndex,
|
|
875
|
+
getItemProps,
|
|
876
|
+
listRef,
|
|
877
|
+
listboxId,
|
|
878
|
+
isOptionSelected,
|
|
879
|
+
customRenderOption,
|
|
880
|
+
getOptionLabel,
|
|
881
|
+
onSelect,
|
|
882
|
+
onInteractionPointerDown,
|
|
883
|
+
indexOffset,
|
|
884
|
+
optionClassName: (_c = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.option) === null || _c === void 0 ? void 0 : _c.className,
|
|
885
|
+
optionStyle: (_d = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.option) === null || _d === void 0 ? void 0 : _d.style,
|
|
886
|
+
});
|
|
887
|
+
navigableIndex = result.nextNavigableIndex;
|
|
888
|
+
return result.node;
|
|
889
|
+
}
|
|
890
|
+
const result = handleActionRendering({
|
|
891
|
+
action: item.action,
|
|
892
|
+
activeIndex,
|
|
893
|
+
navigableIndex,
|
|
894
|
+
getItemProps,
|
|
895
|
+
listRef,
|
|
896
|
+
listboxId,
|
|
897
|
+
customRenderAction,
|
|
898
|
+
onAction,
|
|
899
|
+
onInteractionPointerDown,
|
|
900
|
+
indexOffset,
|
|
901
|
+
actionClassName: (_e = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.action) === null || _e === void 0 ? void 0 : _e.className,
|
|
902
|
+
actionStyle: (_f = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.action) === null || _f === void 0 ? void 0 : _f.style,
|
|
903
|
+
origin: item.origin,
|
|
904
|
+
});
|
|
905
|
+
navigableIndex = result.nextNavigableIndex;
|
|
906
|
+
return result.node;
|
|
907
|
+
}
|
|
908
|
+
return React__default.createElement(React__default.Fragment, null, items.map(renderItemNode));
|
|
909
|
+
}
|
|
910
|
+
function handleSectionRendering({ customRenderSection, section, sectionClassName, sectionStyle, }) {
|
|
911
|
+
var _a;
|
|
912
|
+
const headerContent = customRenderSection ? (customRenderSection(section)) : (React__default.createElement(DefaultSectionContent, { section: section }));
|
|
913
|
+
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-Autocomplete-Section", className: classnames(styles.section, styles.stickyTop, sectionClassName), style: sectionStyle, onPointerDown: preventDefaultPointerDown }, headerContent));
|
|
914
|
+
}
|
|
915
|
+
function DefaultSectionContent({ section, }) {
|
|
916
|
+
return React__default.createElement(Heading, { level: 5 }, section.label);
|
|
917
|
+
}
|
|
918
|
+
function handleOptionRendering({ option, activeIndex, navigableIndex, getItemProps, listRef, listboxId, isOptionSelected, customRenderOption, getOptionLabel, onSelect, onInteractionPointerDown, indexOffset = 0, optionClassName, optionStyle, }) {
|
|
919
|
+
var _a;
|
|
920
|
+
const nextNavigableIndex = navigableIndex + 1;
|
|
921
|
+
const isActive = activeIndex === nextNavigableIndex;
|
|
922
|
+
const isSelected = isOptionSelected(option);
|
|
923
|
+
const optionContent = customRenderOption ? (customRenderOption({ value: option, isActive, isSelected })) : (React__default.createElement(DefaultOptionContent, { isSelected: isSelected, text: getOptionLabel(option) }));
|
|
924
|
+
return {
|
|
925
|
+
node: (React__default.createElement("div", Object.assign({ key: `option-${String((_a = option.key) !== null && _a !== void 0 ? _a : getOptionLabel(option))}` }, getItemProps({
|
|
926
|
+
ref(node) {
|
|
927
|
+
const idx = nextNavigableIndex + indexOffset;
|
|
928
|
+
if (node)
|
|
929
|
+
listRef.current[idx] = node;
|
|
930
|
+
},
|
|
931
|
+
onClick: () => onSelect(option),
|
|
932
|
+
onPointerDown: onInteractionPointerDown,
|
|
933
|
+
className: classnames(styles.option, isActive && styles.optionActive, optionClassName),
|
|
934
|
+
style: optionStyle,
|
|
935
|
+
}), { role: "option", tabIndex: -1, "aria-selected": isSelected ? true : false, id: `${listboxId}-item-${nextNavigableIndex + indexOffset}`, "data-index": nextNavigableIndex + indexOffset, "data-active": isActive ? true : undefined }), optionContent)),
|
|
936
|
+
nextNavigableIndex,
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
function DefaultOptionContent({ isSelected, text, }) {
|
|
940
|
+
return (React__default.createElement("div", { className: styles.defaultOptionContent },
|
|
941
|
+
React__default.createElement("div", { className: styles.icon, style: isSelected ? undefined : { visibility: "hidden" } },
|
|
942
|
+
React__default.createElement(Icon, { name: "checkmark", size: "small" })),
|
|
943
|
+
React__default.createElement(Text, null, text)));
|
|
944
|
+
}
|
|
945
|
+
function handleActionRendering({ action, activeIndex, navigableIndex, getItemProps, listRef, listboxId, customRenderAction, onAction, onInteractionPointerDown, indexOffset = 0, actionClassName, actionStyle, origin, }) {
|
|
946
|
+
var _a;
|
|
947
|
+
const nextNavigableIndex = navigableIndex + 1;
|
|
948
|
+
const isActive = activeIndex === nextNavigableIndex;
|
|
949
|
+
const actionContent = customRenderAction ? (customRenderAction({ value: action, isActive, origin })) : (React__default.createElement(DefaultActionContent, { textContent: action.label }));
|
|
950
|
+
const computedIndex = nextNavigableIndex + indexOffset;
|
|
951
|
+
const itemProps = getItemProps({
|
|
952
|
+
ref(node) {
|
|
953
|
+
if (node) {
|
|
954
|
+
listRef.current[computedIndex] = node;
|
|
955
|
+
}
|
|
956
|
+
},
|
|
957
|
+
onClick: () => {
|
|
958
|
+
onAction({
|
|
959
|
+
run: action.onClick,
|
|
960
|
+
closeOnRun: action.shouldClose,
|
|
961
|
+
});
|
|
962
|
+
},
|
|
963
|
+
onPointerDown: onInteractionPointerDown,
|
|
964
|
+
className: classnames(styles.action, isActive && styles.actionActive, actionClassName),
|
|
965
|
+
style: actionStyle,
|
|
966
|
+
});
|
|
967
|
+
return {
|
|
968
|
+
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-Autocomplete-Action", id: `${listboxId}-item-${computedIndex}`, "data-index": computedIndex, "data-origin": origin, "data-active": isActive ? true : undefined }), actionContent)),
|
|
969
|
+
nextNavigableIndex,
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
function DefaultActionContent({ textContent, }) {
|
|
973
|
+
return (React__default.createElement(Typography, { textColor: "interactive", fontWeight: "semiBold", underline: "solid color-interactive", UNSAFE_style: {
|
|
974
|
+
textStyle: {
|
|
975
|
+
textDecorationThickness: "var(--border-thick)",
|
|
976
|
+
textUnderlineOffset: "var(--space-smallest)",
|
|
977
|
+
},
|
|
978
|
+
} }, textContent));
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
function PersistentRegion({ items, position, activeIndex, indexOffset, getItemProps, listRef, customRenderHeader, customRenderFooter, className, style, onAction, onInteractionPointerDown, }) {
|
|
982
|
+
if (!items || items.length === 0)
|
|
983
|
+
return null;
|
|
984
|
+
let navigableIndex = -1;
|
|
985
|
+
return (React__default.createElement("div", { className: className, style: style, "data-region": position, "data-testid": `ATL-Autocomplete-${position}` }, items.map(persistent => {
|
|
986
|
+
const result = handlePersistentRendering({
|
|
987
|
+
persistent,
|
|
988
|
+
position,
|
|
989
|
+
activeIndex,
|
|
990
|
+
indexOffset,
|
|
991
|
+
getItemProps,
|
|
992
|
+
customRenderHeader,
|
|
993
|
+
customRenderFooter,
|
|
994
|
+
listRef,
|
|
995
|
+
onAction,
|
|
996
|
+
onInteractionPointerDown,
|
|
997
|
+
navigableIndex,
|
|
998
|
+
});
|
|
999
|
+
navigableIndex = result.nextNavigableIndex;
|
|
1000
|
+
return result.node;
|
|
1001
|
+
})));
|
|
1002
|
+
}
|
|
1003
|
+
function handlePersistentRendering({ persistent, position, activeIndex, indexOffset, getItemProps, customRenderHeader, customRenderFooter, listRef, onAction, onInteractionPointerDown, navigableIndex, }) {
|
|
1004
|
+
const interactive = Boolean(persistent.onClick);
|
|
1005
|
+
if (!interactive) {
|
|
1006
|
+
const node = handleTextPersistentRendering({
|
|
1007
|
+
persistent,
|
|
1008
|
+
position,
|
|
1009
|
+
customRenderHeader,
|
|
1010
|
+
customRenderFooter,
|
|
1011
|
+
});
|
|
1012
|
+
return { node, nextNavigableIndex: navigableIndex };
|
|
1013
|
+
}
|
|
1014
|
+
return handleActionPersistentRendering({
|
|
1015
|
+
persistent,
|
|
1016
|
+
position,
|
|
1017
|
+
activeIndex,
|
|
1018
|
+
indexOffset,
|
|
1019
|
+
getItemProps,
|
|
1020
|
+
customRenderHeader,
|
|
1021
|
+
customRenderFooter,
|
|
1022
|
+
listRef,
|
|
1023
|
+
onAction,
|
|
1024
|
+
onInteractionPointerDown,
|
|
1025
|
+
navigableIndex,
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
function handleTextPersistentRendering({ persistent, position, customRenderHeader, customRenderFooter, }) {
|
|
1029
|
+
var _a;
|
|
1030
|
+
let content;
|
|
1031
|
+
if (position === "header" && customRenderHeader) {
|
|
1032
|
+
content = customRenderHeader({ value: persistent });
|
|
1033
|
+
}
|
|
1034
|
+
else if (position === "footer" && customRenderFooter) {
|
|
1035
|
+
content = customRenderFooter({ value: persistent });
|
|
1036
|
+
}
|
|
1037
|
+
else {
|
|
1038
|
+
content = React__default.createElement(DefaultTextPersistentContent, { persistent: persistent });
|
|
1039
|
+
}
|
|
1040
|
+
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.textPersistent, onPointerDown: preventDefaultPointerDown }, content));
|
|
1041
|
+
}
|
|
1042
|
+
function handleActionPersistentRendering({ persistent, position, activeIndex, indexOffset, getItemProps, customRenderHeader, customRenderFooter, listRef, onAction, onInteractionPointerDown, navigableIndex, }) {
|
|
1043
|
+
var _a;
|
|
1044
|
+
const nextNavigableIndex = navigableIndex + 1;
|
|
1045
|
+
const isActive = activeIndex === indexOffset + nextNavigableIndex;
|
|
1046
|
+
let content;
|
|
1047
|
+
if (position === "header" && customRenderHeader) {
|
|
1048
|
+
content = customRenderHeader({
|
|
1049
|
+
value: persistent,
|
|
1050
|
+
isActive,
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
else if (position === "footer" && customRenderFooter) {
|
|
1054
|
+
content = customRenderFooter({
|
|
1055
|
+
value: persistent,
|
|
1056
|
+
isActive,
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
else {
|
|
1060
|
+
content = React__default.createElement(DefaultActionContent, { textContent: persistent.label });
|
|
1061
|
+
}
|
|
1062
|
+
return {
|
|
1063
|
+
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({
|
|
1064
|
+
ref(persistNode) {
|
|
1065
|
+
const idx = indexOffset + nextNavigableIndex;
|
|
1066
|
+
if (persistNode)
|
|
1067
|
+
listRef.current[idx] = persistNode;
|
|
1068
|
+
},
|
|
1069
|
+
onClick: () => {
|
|
1070
|
+
onAction({
|
|
1071
|
+
run: () => {
|
|
1072
|
+
var _a;
|
|
1073
|
+
(_a = persistent.onClick) === null || _a === void 0 ? void 0 : _a.call(persistent);
|
|
1074
|
+
},
|
|
1075
|
+
closeOnRun: persistent.shouldClose,
|
|
1076
|
+
});
|
|
1077
|
+
},
|
|
1078
|
+
onPointerDown: onInteractionPointerDown,
|
|
1079
|
+
className: classnames(styles.action, isActive && styles.actionActive),
|
|
1080
|
+
}), { role: "button", tabIndex: -1 }), content)),
|
|
1081
|
+
nextNavigableIndex,
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
function DefaultTextPersistentContent({ persistent, }) {
|
|
1085
|
+
return React__default.createElement("div", { className: styles.textPersistent }, persistent.label);
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
function FloatingMenu({ context, getFloatingProps, refs, listboxId, className, floatingStyles, transitionStyles, menuStyle, renderable, persistentsHeaders, persistentsFooters, activeIndex, headerInteractiveCount, middleNavigableCount, getItemProps, listRef, onSelection, onAction, onInteractionPointerDown, getOptionLabel, isOptionSelected, loading, showEmptyStateMessage, emptyStateMessage, customRenderLoading, customRenderOption, customRenderSection, customRenderAction, customRenderHeader, customRenderFooter, slotOverrides, }) {
|
|
1089
|
+
var _a, _b, _c, _d;
|
|
1090
|
+
const activeIndexForMiddle = activeIndex != null ? activeIndex - headerInteractiveCount : null;
|
|
1091
|
+
return (React__default.createElement(FloatingPortal, null,
|
|
1092
|
+
React__default.createElement(FloatingFocusManager, { context: context, modal: false, initialFocus: -1, closeOnFocusOut: true, returnFocus: false },
|
|
1093
|
+
React__default.createElement("div", Object.assign({}, getFloatingProps({
|
|
1094
|
+
ref(node) {
|
|
1095
|
+
if (node)
|
|
1096
|
+
refs.setFloating(node);
|
|
1097
|
+
},
|
|
1098
|
+
id: listboxId,
|
|
1099
|
+
role: "listbox",
|
|
1100
|
+
className,
|
|
1101
|
+
style: Object.assign(Object.assign(Object.assign({}, floatingStyles), transitionStyles), menuStyle),
|
|
1102
|
+
})),
|
|
1103
|
+
React__default.createElement(PersistentRegion, { items: persistentsHeaders, position: "header", activeIndex: activeIndex, indexOffset: 0, listboxId: listboxId, getItemProps: getItemProps, listRef: listRef, customRenderHeader: customRenderHeader, customRenderFooter: customRenderFooter, onAction: onAction, onInteractionPointerDown: onInteractionPointerDown, className: classnames(styles.persistentHeader, (_a = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.header) === null || _a === void 0 ? void 0 : _a.className), style: (_b = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.header) === null || _b === void 0 ? void 0 : _b.style }),
|
|
1104
|
+
React__default.createElement("div", { className: styles.scrollRegion }, loading ? (customRenderLoading !== null && customRenderLoading !== void 0 ? customRenderLoading : React__default.createElement(LoadingContent, null)) : (React__default.createElement(React__default.Fragment, null,
|
|
1105
|
+
showEmptyStateMessage && (React__default.createElement(EmptyStateMessage, { emptyState: emptyStateMessage })),
|
|
1106
|
+
renderable.length > 0 && (React__default.createElement(MenuList, { items: renderable, activeIndex: activeIndexForMiddle, indexOffset: headerInteractiveCount, listboxId: listboxId, getItemProps: getItemProps, listRef: listRef, customRenderOption: customRenderOption, customRenderSection: customRenderSection, customRenderAction: customRenderAction, getOptionLabel: getOptionLabel, onSelect: onSelection, onAction: onAction, onInteractionPointerDown: onInteractionPointerDown, isOptionSelected: isOptionSelected, slotOverrides: slotOverrides }))))),
|
|
1107
|
+
React__default.createElement(PersistentRegion, { items: persistentsFooters, position: "footer", activeIndex: activeIndex, indexOffset: headerInteractiveCount + middleNavigableCount, listboxId: listboxId, getItemProps: getItemProps, listRef: listRef, customRenderHeader: customRenderHeader, customRenderFooter: customRenderFooter, onAction: onAction, onInteractionPointerDown: onInteractionPointerDown, className: classnames(styles.persistentFooter, (_c = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.footer) === null || _c === void 0 ? void 0 : _c.className), style: (_d = slotOverrides === null || slotOverrides === void 0 ? void 0 : slotOverrides.footer) === null || _d === void 0 ? void 0 : _d.style })))));
|
|
1108
|
+
}
|
|
1109
|
+
function LoadingContent() {
|
|
1110
|
+
return (React__default.createElement("div", { className: styles.loadingList, onPointerDown: preventDefaultPointerDown },
|
|
1111
|
+
React__default.createElement(Glimmer, { shape: "rectangle", size: "base" }),
|
|
1112
|
+
React__default.createElement(Glimmer, { shape: "rectangle", size: "base" }),
|
|
1113
|
+
React__default.createElement(Glimmer, { shape: "rectangle", size: "base" })));
|
|
1114
|
+
}
|
|
1115
|
+
function EmptyStateMessage({ emptyState, }) {
|
|
1116
|
+
const emptyStateDefault = "No options";
|
|
1117
|
+
const emptyStateContent = emptyState !== null && emptyState !== void 0 ? emptyState : emptyStateDefault;
|
|
1118
|
+
return (React__default.createElement("div", { className: styles.emptyStateMessage, onPointerDown: preventDefaultPointerDown }, emptyStateContent));
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
const Autocomplete = forwardRef(AutocompleteInternal);
|
|
1122
|
+
// eslint-disable-next-line max-statements
|
|
1123
|
+
function AutocompleteInternal(props, forwardedRef) {
|
|
1124
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
1125
|
+
const { inputValue, placeholder, disabled, error, invalid, description, size: sizeProp, loading = false, } = props;
|
|
1126
|
+
const formFieldRef = useRef(null);
|
|
1127
|
+
// Ref for the multi-select input element, where we cannot use refs.domReference
|
|
1128
|
+
// since it is not attached to the literal input element, whereas single + InputText only offers a
|
|
1129
|
+
// ref to the literal input element.
|
|
1130
|
+
const internalInputRef = useRef(null);
|
|
1131
|
+
const { renderable, optionCount, persistentsHeaders, persistentsFooters, headerInteractiveCount, middleNavigableCount, getOptionLabel, isOptionSelected, refs, floatingStyles, context, getReferenceProps, getFloatingProps, getItemProps, activeIndex, open, listRef, onSelection, onAction, onInteractionPointerDown, removeSelection, onInputChangeFromUser, onInputBlur, onInputFocus, onInputKeyDown, setReferenceElement, clearAll, } = useAutocomplete(props, internalInputRef);
|
|
1132
|
+
const listboxId = React__default.useId();
|
|
1133
|
+
// Provides mount/unmount-aware transition styles for the floating element
|
|
1134
|
+
const { isMounted, styles: transitionStyles } = useTransitionStyles(context, {
|
|
1135
|
+
initial: { opacity: 0 },
|
|
1136
|
+
open: { opacity: 1 },
|
|
1137
|
+
close: { opacity: 0 },
|
|
1138
|
+
duration: { open: tokens["timing-base"], close: tokens["timing-base"] },
|
|
1139
|
+
});
|
|
1140
|
+
const [positionRefEl, setPositionRefEl] = React__default.useState(null);
|
|
1141
|
+
const inputId = React__default.useId();
|
|
1142
|
+
const descriptionId = `descriptionUUID--${inputId}`;
|
|
1143
|
+
const selectedValues = props.multiple
|
|
1144
|
+
? ((_a = props.value) !== null && _a !== void 0 ? _a : EMPTY_SELECTED_VALUES)
|
|
1145
|
+
: EMPTY_SELECTED_VALUES;
|
|
1146
|
+
const { activeChipIndex, onKeyDown: chipKeyDown, onBlur: chipBlur, } = useChipNavigation({
|
|
1147
|
+
selectedValues,
|
|
1148
|
+
inputValue,
|
|
1149
|
+
readOnly: props.readOnly,
|
|
1150
|
+
removeSelection,
|
|
1151
|
+
onInputKeyDown,
|
|
1152
|
+
onInputBlur,
|
|
1153
|
+
});
|
|
1154
|
+
const focusInputOnPointerDown = useCallback((e) => {
|
|
1155
|
+
var _a;
|
|
1156
|
+
const target = e.target;
|
|
1157
|
+
if (target.closest("input, button"))
|
|
1158
|
+
return;
|
|
1159
|
+
e.preventDefault();
|
|
1160
|
+
(_a = internalInputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
1161
|
+
}, []);
|
|
1162
|
+
const composedReferenceProps = getReferenceProps(Object.assign({ onKeyDown: props.multiple ? chipKeyDown : onInputKeyDown, onFocus: onInputFocus, onBlur: props.multiple ? chipBlur : onInputBlur }, (props.multiple ? { onPointerDown: focusInputOnPointerDown } : {})));
|
|
1163
|
+
const dataAttrs = filterDataAttributes(props);
|
|
1164
|
+
const ariaProps = {
|
|
1165
|
+
role: "combobox",
|
|
1166
|
+
"aria-autocomplete": "list",
|
|
1167
|
+
"aria-expanded": open ? true : false,
|
|
1168
|
+
"aria-controls": listboxId,
|
|
1169
|
+
"aria-activedescendant": activeChipIndex === null && open && activeIndex != null
|
|
1170
|
+
? `${listboxId}-item-${activeIndex}`
|
|
1171
|
+
: undefined,
|
|
1172
|
+
};
|
|
1173
|
+
const inputProps = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ value: inputValue, onChange: props.readOnly ? undefined : onInputChangeFromUser }, (props.readOnly ? { onFocus: onInputFocus, onBlur: onInputBlur } : {})), { placeholder,
|
|
1174
|
+
disabled, readOnly: props.readOnly, error: error !== null && error !== void 0 ? error : undefined, name: props.name, invalid, clearable: props.clearable, autoComplete: "off", autoFocus: props.autoFocus, description, size: sizeProp ? sizeProp : undefined, prefix: props.prefix, suffix: props.suffix }), (props.readOnly ? {} : composedReferenceProps)), ariaProps), dataAttrs);
|
|
1175
|
+
const chipAreaRef = useCallback((node) => {
|
|
1176
|
+
if (!node)
|
|
1177
|
+
return;
|
|
1178
|
+
// In multiple mode, we use the input as the reference so floating-ui's
|
|
1179
|
+
// useListNavigation treats it as a typeable combobox and allows Space.
|
|
1180
|
+
// Only use chipArea for positioning/sizing.
|
|
1181
|
+
if (!props.multiple) {
|
|
1182
|
+
setReferenceElement(node);
|
|
1183
|
+
}
|
|
1184
|
+
const multiContainer = node.closest("[data-testid='ATL-Autocomplete-multiSelectContainer']");
|
|
1185
|
+
if (multiContainer) {
|
|
1186
|
+
setPositionRefEl(multiContainer);
|
|
1187
|
+
}
|
|
1188
|
+
}, [setReferenceElement, props.multiple]);
|
|
1189
|
+
const referenceInputRef = useCallback((node) => {
|
|
1190
|
+
setReferenceElement(node);
|
|
1191
|
+
if (!props.multiple) {
|
|
1192
|
+
// Workaround to get the visual InputText element, which is not the same
|
|
1193
|
+
// as the literal input reference element when props like suffix/prefix
|
|
1194
|
+
// are present. Used to set the position reference for the floating menu.
|
|
1195
|
+
const visualInputTextElement = node === null || node === void 0 ? void 0 : node.closest("[data-testid='Form-Field-Wrapper']");
|
|
1196
|
+
if (visualInputTextElement) {
|
|
1197
|
+
setPositionRefEl(visualInputTextElement);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
}, [setReferenceElement, props.multiple]);
|
|
1201
|
+
const mergedInputRef = useMemo(() => mergeRefs([
|
|
1202
|
+
referenceInputRef,
|
|
1203
|
+
internalInputRef,
|
|
1204
|
+
forwardedRef,
|
|
1205
|
+
]), [referenceInputRef, forwardedRef]);
|
|
1206
|
+
useEffect(() => {
|
|
1207
|
+
if (!positionRefEl)
|
|
1208
|
+
return;
|
|
1209
|
+
refs.setPositionReference(positionRefEl);
|
|
1210
|
+
}, [positionRefEl, refs]);
|
|
1211
|
+
const menuClassName = classnames(styles.list, (_b = props.UNSAFE_className) === null || _b === void 0 ? void 0 : _b.menu);
|
|
1212
|
+
const showEmptyStateMessage = optionCount === 0 && props.emptyStateMessage !== false;
|
|
1213
|
+
const floatingMenu = isMounted && !props.readOnly && !disabled && (React__default.createElement(FloatingMenu, { context: context, getFloatingProps: getFloatingProps, refs: refs, listboxId: listboxId, className: menuClassName, floatingStyles: floatingStyles, transitionStyles: transitionStyles, menuStyle: (_c = props.UNSAFE_styles) === null || _c === void 0 ? void 0 : _c.menu, renderable: renderable, persistentsHeaders: persistentsHeaders, persistentsFooters: persistentsFooters, activeIndex: activeIndex, headerInteractiveCount: headerInteractiveCount, middleNavigableCount: middleNavigableCount, getItemProps: getItemProps, listRef: listRef, onSelection: onSelection, onAction: onAction, onInteractionPointerDown: onInteractionPointerDown, getOptionLabel: getOptionLabel, isOptionSelected: isOptionSelected, loading: loading, showEmptyStateMessage: showEmptyStateMessage, emptyStateMessage: props.emptyStateMessage, customRenderLoading: props.customRenderLoading, customRenderOption: props.customRenderOption, customRenderSection: props.customRenderSection, customRenderAction: props.customRenderAction, customRenderHeader: props.customRenderHeader, customRenderFooter: props.customRenderFooter, slotOverrides: {
|
|
1214
|
+
option: {
|
|
1215
|
+
className: (_d = props.UNSAFE_className) === null || _d === void 0 ? void 0 : _d.option,
|
|
1216
|
+
style: (_e = props.UNSAFE_styles) === null || _e === void 0 ? void 0 : _e.option,
|
|
1217
|
+
},
|
|
1218
|
+
action: {
|
|
1219
|
+
className: (_f = props.UNSAFE_className) === null || _f === void 0 ? void 0 : _f.action,
|
|
1220
|
+
style: (_g = props.UNSAFE_styles) === null || _g === void 0 ? void 0 : _g.action,
|
|
1221
|
+
},
|
|
1222
|
+
section: {
|
|
1223
|
+
className: (_h = props.UNSAFE_className) === null || _h === void 0 ? void 0 : _h.section,
|
|
1224
|
+
style: (_j = props.UNSAFE_styles) === null || _j === void 0 ? void 0 : _j.section,
|
|
1225
|
+
},
|
|
1226
|
+
header: {
|
|
1227
|
+
className: (_k = props.UNSAFE_className) === null || _k === void 0 ? void 0 : _k.header,
|
|
1228
|
+
style: (_l = props.UNSAFE_styles) === null || _l === void 0 ? void 0 : _l.header,
|
|
1229
|
+
},
|
|
1230
|
+
footer: {
|
|
1231
|
+
className: (_m = props.UNSAFE_className) === null || _m === void 0 ? void 0 : _m.footer,
|
|
1232
|
+
style: (_o = props.UNSAFE_styles) === null || _o === void 0 ? void 0 : _o.footer,
|
|
1233
|
+
},
|
|
1234
|
+
} }));
|
|
1235
|
+
if (props.customRenderInput) {
|
|
1236
|
+
return (React__default.createElement("div", { "data-testid": "ATL-Autocomplete" },
|
|
1237
|
+
props.customRenderInput({ inputRef: mergedInputRef, inputProps }),
|
|
1238
|
+
floatingMenu));
|
|
1239
|
+
}
|
|
1240
|
+
if (props.multiple) {
|
|
1241
|
+
return (React__default.createElement(MultipleSelectionLayout, { selectedValues: selectedValues, inputValue: inputValue, disabled: disabled, error: error, invalid: invalid, sizeProp: sizeProp, inputId: inputId, descriptionId: descriptionId, description: description, placeholder: placeholder, clearable: props.clearable, prefix: props.prefix, suffix: props.suffix, readOnly: props.readOnly, name: props.name, autoFocus: props.autoFocus, formFieldRef: formFieldRef, chipAreaRef: chipAreaRef, mergedInputRef: mergedInputRef, internalInputRef: internalInputRef, activeChipIndex: activeChipIndex, getOptionLabel: getOptionLabel, customRenderValue: props.customRenderValue, removeSelection: removeSelection, clearAll: clearAll, onInputChangeFromUser: onInputChangeFromUser, chipAreaEventProps: props.readOnly
|
|
1242
|
+
? { onFocus: onInputFocus, onBlur: chipBlur }
|
|
1243
|
+
: composedReferenceProps, inputAriaProps: ariaProps, inputDataAttrs: dataAttrs, limitVisibleSelections: props.limitVisibleSelections, limitSelectionText: props.limitSelectionText, unsafeClassName: (_p = props.UNSAFE_className) === null || _p === void 0 ? void 0 : _p.selection, unsafeStyle: (_q = props.UNSAFE_styles) === null || _q === void 0 ? void 0 : _q.selection }, floatingMenu));
|
|
1244
|
+
}
|
|
1245
|
+
return (React__default.createElement("div", { "data-testid": "ATL-Autocomplete" },
|
|
1246
|
+
React__default.createElement(InputText, Object.assign({ ref: mergedInputRef }, inputProps)),
|
|
1247
|
+
floatingMenu));
|
|
1248
|
+
}
|
|
1249
|
+
function SelectionChip({ value, active, disabled, getOptionLabel, customRenderValue, canDismiss, onDismiss, unsafeClassName, unsafeStyle, }) {
|
|
1250
|
+
return (React__default.createElement("span", { "data-selection-disabled": disabled, className: classnames(styles.selectionChip, {
|
|
1251
|
+
[styles.selectionChipActive]: active,
|
|
1252
|
+
[styles.selectionChipDisabled]: disabled,
|
|
1253
|
+
}, unsafeClassName), style: unsafeStyle, "data-testid": "ATL-Autocomplete-chip", onPointerDown: preventDefaultPointerDown },
|
|
1254
|
+
customRenderValue ? (customRenderValue({ value, getOptionLabel })) : (React__default.createElement(Typography, { size: "small" }, getOptionLabel(value))),
|
|
1255
|
+
canDismiss && (React__default.createElement("button", { type: "button", disabled: disabled, className: styles.chipDismiss, onClick: onDismiss, onPointerDown: preventDefaultPointerDown, "aria-label": `Remove ${getOptionLabel(value)}`, tabIndex: -1 },
|
|
1256
|
+
React__default.createElement(Icon, { size: "small", name: "remove" })))));
|
|
1257
|
+
}
|
|
1258
|
+
function SelectionChipsList({ selectedValues, isFocused, limitVisibleSelections = 6, limitSelectionText = (count) => `+${count}`, activeChipIndex, disabled, getOptionLabel, customRenderValue, canDismiss, removeSelection, unsafeClassName, unsafeStyle, }) {
|
|
1259
|
+
const shouldLimit = !isFocused &&
|
|
1260
|
+
limitVisibleSelections !== -1 &&
|
|
1261
|
+
selectedValues.length > limitVisibleSelections;
|
|
1262
|
+
const visibleValues = shouldLimit
|
|
1263
|
+
? selectedValues.slice(0, limitVisibleSelections)
|
|
1264
|
+
: selectedValues;
|
|
1265
|
+
const hiddenCount = shouldLimit
|
|
1266
|
+
? selectedValues.length - limitVisibleSelections
|
|
1267
|
+
: 0;
|
|
1268
|
+
return (React__default.createElement(React__default.Fragment, null,
|
|
1269
|
+
visibleValues.map((v, i) => {
|
|
1270
|
+
var _a;
|
|
1271
|
+
return (React__default.createElement(SelectionChip, { key: (_a = v.key) !== null && _a !== void 0 ? _a : getOptionLabel(v), value: v, active: activeChipIndex === i, disabled: disabled, getOptionLabel: getOptionLabel, customRenderValue: customRenderValue, canDismiss: canDismiss, onDismiss: () => removeSelection(v), unsafeClassName: unsafeClassName, unsafeStyle: unsafeStyle }));
|
|
1272
|
+
}),
|
|
1273
|
+
hiddenCount > 0 && (React__default.createElement("span", { className: styles.limitText, "data-testid": "ATL-Autocomplete-limitText" },
|
|
1274
|
+
React__default.createElement(Typography, { size: "small", element: "span" }, limitSelectionText(hiddenCount))))));
|
|
1275
|
+
}
|
|
1276
|
+
function MultipleSelectionLayout({ selectedValues, inputValue, disabled, error, invalid, sizeProp, inputId, descriptionId, description, placeholder, clearable, prefix, suffix, readOnly, name, autoFocus, formFieldRef, chipAreaRef, mergedInputRef, internalInputRef, activeChipIndex, getOptionLabel, customRenderValue, removeSelection, clearAll, onInputChangeFromUser, chipAreaEventProps, inputAriaProps, inputDataAttrs, limitVisibleSelections, limitSelectionText, unsafeClassName, unsafeStyle, children, }) {
|
|
1277
|
+
var _a;
|
|
1278
|
+
const [isFocused, setIsFocused] = useState(false);
|
|
1279
|
+
const hasValue = selectedValues.length > 0 || inputValue;
|
|
1280
|
+
const canDismissChip = !disabled && !readOnly;
|
|
1281
|
+
const handleFocusIn = useCallback(() => {
|
|
1282
|
+
setIsFocused(true);
|
|
1283
|
+
}, []);
|
|
1284
|
+
const handleFocusOut = useCallback((e) => {
|
|
1285
|
+
if (!e.currentTarget.contains(e.relatedTarget)) {
|
|
1286
|
+
setIsFocused(false);
|
|
1287
|
+
}
|
|
1288
|
+
}, []);
|
|
1289
|
+
return (React__default.createElement("div", { "data-testid": "ATL-Autocomplete", onFocus: handleFocusIn, onBlur: handleFocusOut },
|
|
1290
|
+
React__default.createElement("div", { className: styles.multiSelectField, "data-testid": "ATL-Autocomplete-multiSelectContainer" },
|
|
1291
|
+
React__default.createElement(FormFieldWrapper, { disabled: disabled, size: sizeProp ? sizeProp : undefined, error: (_a = error) !== null && _a !== void 0 ? _a : "", invalid: Boolean(error || invalid), identifier: inputId, descriptionIdentifier: descriptionId, description: description, clearable: clearable !== null && clearable !== void 0 ? clearable : "never", onClear: () => {
|
|
1292
|
+
var _a;
|
|
1293
|
+
clearAll();
|
|
1294
|
+
(_a = internalInputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
1295
|
+
}, type: "text", placeholder: placeholder, value: hasValue ? "has-value" : "", prefix: prefix, suffix: suffix, wrapperRef: formFieldRef },
|
|
1296
|
+
React__default.createElement("div", Object.assign({ ref: chipAreaRef, className: styles.chipArea, "data-testid": "ATL-Autocomplete-chipArea" }, chipAreaEventProps),
|
|
1297
|
+
React__default.createElement(SelectionChipsList, { selectedValues: selectedValues, isFocused: isFocused, limitVisibleSelections: limitVisibleSelections, limitSelectionText: limitSelectionText, activeChipIndex: activeChipIndex, disabled: disabled, getOptionLabel: getOptionLabel, customRenderValue: customRenderValue, canDismiss: canDismissChip, removeSelection: removeSelection, unsafeClassName: unsafeClassName, unsafeStyle: unsafeStyle }),
|
|
1298
|
+
React__default.createElement("input", Object.assign({ ref: mergedInputRef, className: styles.inlineInput, id: inputId, value: inputValue, onChange: readOnly
|
|
1299
|
+
? undefined
|
|
1300
|
+
: e => onInputChangeFromUser(e.target.value), disabled: disabled, readOnly: readOnly, name: name, autoComplete: "off", autoFocus: autoFocus }, inputAriaProps, inputDataAttrs))))),
|
|
1301
|
+
children));
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
export { Autocomplete as A };
|