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