@yamada-ui/autocomplete 0.1.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.
@@ -0,0 +1,1442 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/autocomplete.tsx
21
+ var autocomplete_exports = {};
22
+ __export(autocomplete_exports, {
23
+ Autocomplete: () => Autocomplete
24
+ });
25
+ module.exports = __toCommonJS(autocomplete_exports);
26
+ var import_core8 = require("@yamada-ui/core");
27
+ var import_popover2 = require("@yamada-ui/popover");
28
+ var import_utils8 = require("@yamada-ui/utils");
29
+
30
+ // src/autocomplete-icon.tsx
31
+ var import_core7 = require("@yamada-ui/core");
32
+ var import_icon = require("@yamada-ui/icon");
33
+ var import_use_clickable = require("@yamada-ui/use-clickable");
34
+ var import_utils7 = require("@yamada-ui/utils");
35
+ var import_react2 = require("react");
36
+
37
+ // src/use-autocomplete.tsx
38
+ var import_core6 = require("@yamada-ui/core");
39
+ var import_form_control = require("@yamada-ui/form-control");
40
+ var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
41
+ var import_use_descendant = require("@yamada-ui/use-descendant");
42
+ var import_use_outside_click = require("@yamada-ui/use-outside-click");
43
+ var import_utils6 = require("@yamada-ui/utils");
44
+ var import_react = require("react");
45
+
46
+ // src/autocomplete-option-group.tsx
47
+ var import_core = require("@yamada-ui/core");
48
+ var import_utils = require("@yamada-ui/utils");
49
+ var import_jsx_runtime = require("react/jsx-runtime");
50
+ var AutocompleteOptionGroup = (0, import_core.forwardRef)(
51
+ ({ className, color, h, height, minH, minHeight, children, ...rest }, ref) => {
52
+ const { styles } = useAutocompleteContext();
53
+ const { label, getContainerProps, getGroupProps } = useAutocompleteOptionGroup(rest);
54
+ h = h != null ? h : height;
55
+ minH = minH != null ? minH : minHeight;
56
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
57
+ import_core.ui.li,
58
+ {
59
+ className: "ui-autocomplete-group-container",
60
+ __css: { w: "100%", h: "fit-content", color },
61
+ ...getContainerProps(),
62
+ children: [
63
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
64
+ import_core.ui.span,
65
+ {
66
+ className: (0, import_utils.cx)("ui-autocomplete-group-label"),
67
+ __css: styles.groupLabel,
68
+ noOfLines: 1,
69
+ children: label
70
+ }
71
+ ),
72
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
73
+ import_core.ui.ul,
74
+ {
75
+ ...getGroupProps({}, ref),
76
+ className: (0, import_utils.cx)("ui-autocomplete-group", className),
77
+ __css: { h, minH, ...styles.group },
78
+ children
79
+ }
80
+ )
81
+ ]
82
+ }
83
+ );
84
+ }
85
+ );
86
+
87
+ // src/autocomplete-option.tsx
88
+ var import_core2 = require("@yamada-ui/core");
89
+ var import_utils2 = require("@yamada-ui/utils");
90
+ var import_jsx_runtime2 = require("react/jsx-runtime");
91
+ var AutocompleteOption = (0, import_core2.forwardRef)(
92
+ ({ className, icon, ...rest }, ref) => {
93
+ const { styles } = useAutocompleteContext();
94
+ const { isSelected, customIcon, children, getOptionProps } = useAutocompleteOption(rest);
95
+ icon = icon != null ? icon : customIcon;
96
+ const css = {
97
+ textDecoration: "none",
98
+ color: "inherit",
99
+ userSelect: "none",
100
+ display: "flex",
101
+ width: "100%",
102
+ alignItems: "center",
103
+ textAlign: "start",
104
+ flex: "0 0 auto",
105
+ outline: 0,
106
+ gap: "0.75rem",
107
+ ...styles.item
108
+ };
109
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
110
+ import_core2.ui.li,
111
+ {
112
+ className: (0, import_utils2.cx)("ui-autocomplete-item", className),
113
+ __css: css,
114
+ ...getOptionProps({}, ref),
115
+ children: [
116
+ icon !== null ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(AutocompleteItemIcon, { opacity: isSelected ? 1 : 0, children: icon || /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CheckIcon, {}) }) : null,
117
+ icon ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_core2.ui.span, { style: { pointerEvents: "none", flex: 1 }, noOfLines: 1, children }) : children
118
+ ]
119
+ }
120
+ );
121
+ }
122
+ );
123
+ var CheckIcon = () => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("svg", { viewBox: "0 0 14 14", width: "1em", height: "1em", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
124
+ "polygon",
125
+ {
126
+ fill: "currentColor",
127
+ points: "5.5 11.9993304 14 3.49933039 12.5 2 5.5 8.99933039 1.5 4.9968652 0 6.49933039"
128
+ }
129
+ ) });
130
+
131
+ // src/autocomplete-create.tsx
132
+ var import_core3 = require("@yamada-ui/core");
133
+ var import_utils3 = require("@yamada-ui/utils");
134
+ var import_jsx_runtime3 = require("react/jsx-runtime");
135
+ var AutocompleteCreate = (0, import_core3.forwardRef)(
136
+ ({ className, icon, children, ...rest }, ref) => {
137
+ const { inputValue, styles } = useAutocompleteContext();
138
+ const { getCreateProps } = useAutocompleteCreate();
139
+ children != null ? children : children = inputValue;
140
+ const css = {
141
+ textDecoration: "none",
142
+ color: "inherit",
143
+ userSelect: "none",
144
+ display: "flex",
145
+ width: "100%",
146
+ alignItems: "center",
147
+ textAlign: "start",
148
+ flex: "0 0 auto",
149
+ outline: 0,
150
+ gap: "0.75rem",
151
+ ...styles.item
152
+ };
153
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
154
+ import_core3.ui.li,
155
+ {
156
+ className: (0, import_utils3.cx)("ui-autocomplete-create", className),
157
+ __css: css,
158
+ ...getCreateProps(rest, ref),
159
+ children: [
160
+ icon !== null ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(AutocompleteItemIcon, { children: icon || /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(PlusIcon, {}) }) : null,
161
+ icon ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core3.ui.span, { style: { pointerEvents: "none", flex: 1 }, noOfLines: 1, children: (0, import_utils3.runIfFunc)(children, inputValue) }) : (0, import_utils3.runIfFunc)(children, inputValue)
162
+ ]
163
+ }
164
+ );
165
+ }
166
+ );
167
+ var PlusIcon = () => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("svg", { viewBox: "0 0 45.402 45.402", width: "1em", height: "1em", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
168
+ "path",
169
+ {
170
+ fill: "currentColor",
171
+ d: "M41.267,18.557H26.832V4.134C26.832,1.851,24.99,0,22.707,0c-2.283,0-4.124,1.851-4.124,4.135v14.432H4.141 c-2.283,0-4.139,1.851-4.138,4.135c-0.001,1.141,0.46,2.187,1.207,2.934c0.748,0.749,1.78,1.222,2.92,1.222h14.453V41.27 c0,1.142,0.453,2.176,1.201,2.922c0.748,0.748,1.777,1.211,2.919,1.211c2.282,0,4.129-1.851,4.129-4.133V26.857h14.435 c2.283,0,4.134-1.867,4.133-4.15C45.399,20.425,43.548,18.557,41.267,18.557z"
172
+ }
173
+ ) });
174
+
175
+ // src/autocomplete-empty.tsx
176
+ var import_core4 = require("@yamada-ui/core");
177
+ var import_utils4 = require("@yamada-ui/utils");
178
+ var import_jsx_runtime4 = require("react/jsx-runtime");
179
+ var AutocompleteEmpty = (0, import_core4.forwardRef)(
180
+ ({ className, icon, children, ...rest }, ref) => {
181
+ const { emptyMessage, styles } = useAutocompleteContext();
182
+ const { getEmptyProps } = useAutocompleteEmpty();
183
+ children != null ? children : children = emptyMessage;
184
+ const css = {
185
+ textDecoration: "none",
186
+ color: "inherit",
187
+ userSelect: "none",
188
+ display: "flex",
189
+ width: "100%",
190
+ alignItems: "center",
191
+ textAlign: "start",
192
+ flex: "0 0 auto",
193
+ outline: 0,
194
+ gap: "0.75rem",
195
+ pointerEvents: "none",
196
+ ...styles.item
197
+ };
198
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
199
+ import_core4.ui.li,
200
+ {
201
+ className: (0, import_utils4.cx)("ui-autocomplete-empty", className),
202
+ __css: css,
203
+ ...getEmptyProps(rest, ref),
204
+ children: [
205
+ icon !== null ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteItemIcon, { children: icon || /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(MinusIcon, {}) }) : null,
206
+ icon ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_core4.ui.span, { style: { pointerEvents: "none", flex: 1 }, noOfLines: 1, children }) : children
207
+ ]
208
+ }
209
+ );
210
+ }
211
+ );
212
+ var MinusIcon = () => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("svg", { viewBox: "0 0 448 512", width: "1em", height: "1em", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
213
+ "path",
214
+ {
215
+ fill: "currentColor",
216
+ d: "M432 256c0 17.7-14.3 32-32 32L48 288c-17.7 0-32-14.3-32-32s14.3-32 32-32l352 0c17.7 0 32 14.3 32 32z"
217
+ }
218
+ ) });
219
+
220
+ // src/autocomplete-list.tsx
221
+ var import_core5 = require("@yamada-ui/core");
222
+ var import_popover = require("@yamada-ui/popover");
223
+ var import_utils5 = require("@yamada-ui/utils");
224
+ var import_jsx_runtime5 = require("react/jsx-runtime");
225
+ var AutocompleteList = (0, import_core5.forwardRef)(
226
+ ({ className, w, width, minW, minWidth, ...rest }, ref) => {
227
+ var _a, _b, _c, _d, _e, _f, _g, _h;
228
+ const { styles } = useAutocompleteContext();
229
+ const { getListProps } = useAutocompleteList();
230
+ w = (_d = w != null ? w : width) != null ? _d : (_c = (_a = styles.list) == null ? void 0 : _a.w) != null ? _c : (_b = styles.list) == null ? void 0 : _b.width;
231
+ minW = (_h = minW != null ? minW : minWidth) != null ? _h : (_g = (_e = styles.list) == null ? void 0 : _e.minW) != null ? _g : (_f = styles.list) == null ? void 0 : _f.minWidth;
232
+ const css = { ...styles.list };
233
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
234
+ import_popover.PopoverContent,
235
+ {
236
+ className: (0, import_utils5.cx)("ui-autocomplete-list", className),
237
+ w,
238
+ minW,
239
+ __css: css,
240
+ ...getListProps(rest, ref)
241
+ }
242
+ );
243
+ }
244
+ );
245
+
246
+ // src/use-autocomplete.tsx
247
+ var import_jsx_runtime6 = require("react/jsx-runtime");
248
+ var kanaMap = {
249
+ \uFF76\uFF9E: "\u30AC",
250
+ \uFF77\uFF9E: "\u30AE",
251
+ \uFF78\uFF9E: "\u30B0",
252
+ \uFF79\uFF9E: "\u30B2",
253
+ \uFF7A\uFF9E: "\u30B4",
254
+ \uFF7B\uFF9E: "\u30B6",
255
+ \uFF7C\uFF9E: "\u30B8",
256
+ \uFF7D\uFF9E: "\u30BA",
257
+ \uFF7E\uFF9E: "\u30BC",
258
+ \uFF7F\uFF9E: "\u30BE",
259
+ \uFF80\uFF9E: "\u30C0",
260
+ \uFF81\uFF9E: "\u30C2",
261
+ \uFF82\uFF9E: "\u30C5",
262
+ \uFF83\uFF9E: "\u30C7",
263
+ \uFF84\uFF9E: "\u30C9",
264
+ \uFF8A\uFF9E: "\u30D0",
265
+ \uFF8B\uFF9E: "\u30D3",
266
+ \uFF8C\uFF9E: "\u30D6",
267
+ \uFF8D\uFF9E: "\u30D9",
268
+ \uFF8E\uFF9E: "\u30DC",
269
+ \uFF8A\uFF9F: "\u30D1",
270
+ \uFF8B\uFF9F: "\u30D4",
271
+ \uFF8C\uFF9F: "\u30D7",
272
+ \uFF8D\uFF9F: "\u30DA",
273
+ \uFF8E\uFF9F: "\u30DD",
274
+ \uFF73\uFF9E: "\u30F4",
275
+ \uFF9C\uFF9E: "\u30F7",
276
+ \uFF66\uFF9E: "\u30FA",
277
+ \uFF71: "\u30A2",
278
+ \uFF72: "\u30A4",
279
+ \uFF73: "\u30A6",
280
+ \uFF74: "\u30A8",
281
+ \uFF75: "\u30AA",
282
+ \uFF76: "\u30AB",
283
+ \uFF77: "\u30AD",
284
+ \uFF78: "\u30AF",
285
+ \uFF79: "\u30B1",
286
+ \uFF7A: "\u30B3",
287
+ \uFF7B: "\u30B5",
288
+ \uFF7C: "\u30B7",
289
+ \uFF7D: "\u30B9",
290
+ \uFF7E: "\u30BB",
291
+ \uFF7F: "\u30BD",
292
+ \uFF80: "\u30BF",
293
+ \uFF81: "\u30C1",
294
+ \uFF82: "\u30C4",
295
+ \uFF83: "\u30C6",
296
+ \uFF84: "\u30C8",
297
+ \uFF85: "\u30CA",
298
+ \uFF86: "\u30CB",
299
+ \uFF87: "\u30CC",
300
+ \uFF88: "\u30CD",
301
+ \uFF89: "\u30CE",
302
+ \uFF8A: "\u30CF",
303
+ \uFF8B: "\u30D2",
304
+ \uFF8C: "\u30D5",
305
+ \uFF8D: "\u30D8",
306
+ \uFF8E: "\u30DB",
307
+ \uFF8F: "\u30DE",
308
+ \uFF90: "\u30DF",
309
+ \uFF91: "\u30E0",
310
+ \uFF92: "\u30E1",
311
+ \uFF93: "\u30E2",
312
+ \uFF94: "\u30E4",
313
+ \uFF95: "\u30E6",
314
+ \uFF96: "\u30E8",
315
+ \uFF97: "\u30E9",
316
+ \uFF98: "\u30EA",
317
+ \uFF99: "\u30EB",
318
+ \uFF9A: "\u30EC",
319
+ \uFF9B: "\u30ED",
320
+ \uFF9C: "\u30EF",
321
+ \uFF66: "\u30F2",
322
+ \uFF9D: "\u30F3",
323
+ \uFF67: "\u30A1",
324
+ \uFF68: "\u30A3",
325
+ \uFF69: "\u30A5",
326
+ \uFF6A: "\u30A7",
327
+ \uFF6B: "\u30A9",
328
+ \uFF6F: "\u30C3",
329
+ \uFF6C: "\u30E3",
330
+ \uFF6D: "\u30E5",
331
+ \uFF6E: "\u30E7",
332
+ "\uFF61": "\u3002",
333
+ "\uFF64": "\u3001",
334
+ \uFF70: "\u30FC",
335
+ "\uFF62": "\u300C",
336
+ "\uFF63": "\u300D",
337
+ "\uFF65": "\u30FB"
338
+ };
339
+ var defaultFormat = (value) => {
340
+ value = value.replace(/[!-~]/g, (v) => String.fromCharCode(v.charCodeAt(0) - 65248));
341
+ const reg = new RegExp("(" + Object.keys(kanaMap).join("|") + ")", "g");
342
+ value = value.replace(reg, (v) => kanaMap[v]).replace(/゙/g, "\u309B").replace(/゚/g, "\u309C");
343
+ value = value.toUpperCase();
344
+ return value;
345
+ };
346
+ var flattenData = (data) => {
347
+ const filterData = (data2) => data2.map((data3) => {
348
+ const { value, isDisabled, isFocusable } = data3;
349
+ const trulyDisabled = !!isDisabled && !isFocusable;
350
+ const isMulti = (0, import_utils6.isArray)(value);
351
+ if (trulyDisabled)
352
+ return;
353
+ if (!isMulti) {
354
+ return data3;
355
+ } else {
356
+ return filterData(value);
357
+ }
358
+ }).filter(Boolean);
359
+ return filterData(data).flat(Infinity);
360
+ };
361
+ var isTargetOption = (target) => {
362
+ var _a;
363
+ return (0, import_utils6.isHTMLElement)(target) && !!((_a = target == null ? void 0 : target.getAttribute("role")) == null ? void 0 : _a.startsWith("autocomplete-item"));
364
+ };
365
+ var {
366
+ DescendantsContextProvider: AutocompleteDescendantsContextProvider,
367
+ useDescendantsContext: useAutocompleteDescendantsContext,
368
+ useDescendants: useAutocompleteDescendants,
369
+ useDescendant: useAutocompleteDescendant
370
+ } = (0, import_use_descendant.createDescendant)();
371
+ var [AutocompleteProvider, useAutocompleteContext] = (0, import_utils6.createContext)({
372
+ name: "AutocompleteContext",
373
+ errorMessage: `useAutocompleteContext returned is 'undefined'. Seems you forgot to wrap the components in "<Autocomplete />" or "<MultiAutocomplete />"`
374
+ });
375
+ var useAutocomplete = ({
376
+ defaultIsOpen,
377
+ closeOnSelect = true,
378
+ omitSelectedValues = false,
379
+ maxSelectedValues,
380
+ closeOnBlur = true,
381
+ closeOnEsc = true,
382
+ createOption = false,
383
+ createOrder = "first",
384
+ createSecondOrder = "first",
385
+ emptyMessage = "No results found",
386
+ format = defaultFormat,
387
+ placement = "bottom-start",
388
+ duration = 0.2,
389
+ optionProps,
390
+ placeholder,
391
+ children,
392
+ ...rest
393
+ }) => {
394
+ rest = (0, import_form_control.useFormControlProps)(rest);
395
+ const { id } = rest;
396
+ const formControlProps = (0, import_utils6.pickObject)(rest, import_form_control.formControlProperties);
397
+ const [containerProps, inputProps] = (0, import_utils6.splitObject)(
398
+ (0, import_utils6.omitObject)(rest, ["id", "value", "onChange", "month", "onChangeMonth"]),
399
+ import_core6.layoutStylesProperties
400
+ );
401
+ const descendants = useAutocompleteDescendants();
402
+ const containerRef = (0, import_react.useRef)(null);
403
+ const listRef = (0, import_react.useRef)(null);
404
+ const inputRef = (0, import_react.useRef)(null);
405
+ const timeoutIds = (0, import_react.useRef)(/* @__PURE__ */ new Set([]));
406
+ const isComposition = (0, import_react.useRef)(false);
407
+ const [data, setData] = (0, import_react.useState)(rest.data);
408
+ const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
409
+ value: rest.value,
410
+ defaultValue: rest.defaultValue,
411
+ onChange: rest.onChange
412
+ });
413
+ const [displayValue, setDisplayValue] = (0, import_react.useState)(void 0);
414
+ const [inputValue, setInputValue] = (0, import_react.useState)("");
415
+ const [focusedIndex, setFocusedIndex] = (0, import_react.useState)(-1);
416
+ const [isAllSelected, setIsAllSelected] = (0, import_react.useState)(false);
417
+ const [isHit, setIsHit] = (0, import_react.useState)(true);
418
+ const [isOpen, setIsOpen] = (0, import_react.useState)(defaultIsOpen != null ? defaultIsOpen : false);
419
+ const isFocused = focusedIndex > -1;
420
+ const isCreate = focusedIndex === -2 && createOption;
421
+ const isMulti = (0, import_utils6.isArray)(value);
422
+ const isEmptyValue = !isMulti ? !value : !value.length;
423
+ if (createOption && !(0, import_utils6.isUndefined)(children)) {
424
+ console.warn(
425
+ `${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: ${!isMulti ? "Autocomplete" : "MultiAutocomplete"} internally prefers 'children'. If 'createOption' is true, it will not be reflected correctly. If want to reflect, please set 'data' in props.`
426
+ );
427
+ }
428
+ const selectedValues = descendants.enabledValues(
429
+ ({ node }) => {
430
+ var _a;
431
+ return isMulti && value.includes((_a = node.dataset.value) != null ? _a : "");
432
+ }
433
+ );
434
+ const selectedIndexes = selectedValues.map(({ index }) => index);
435
+ const enabledValues = descendants.enabledValues(
436
+ ({ node, index }) => "target" in node.dataset && !selectedIndexes.includes(index)
437
+ );
438
+ const validChildren = (0, import_utils6.getValidChildren)(children);
439
+ const computedChildren = data == null ? void 0 : data.map(({ label, value: value2, ...props }, i) => {
440
+ if (!(0, import_utils6.isArray)(value2)) {
441
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AutocompleteOption, { value: value2, ...props, children: label }, i);
442
+ } else {
443
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AutocompleteOptionGroup, { label, ...props, children: value2.map(
444
+ ({ label: label2, value: value3, ...props2 }, i2) => !(0, import_utils6.isArray)(value3) ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AutocompleteOption, { value: value3, ...props2, children: label2 }, i2) : null
445
+ ) }, i);
446
+ }
447
+ });
448
+ const isEmpty = !validChildren.length && !(computedChildren == null ? void 0 : computedChildren.length);
449
+ const onOpen = (0, import_react.useCallback)(() => {
450
+ var _a;
451
+ if (formControlProps.disabled || formControlProps.readOnly)
452
+ return;
453
+ if (!createOption && (isEmpty || isAllSelected))
454
+ return;
455
+ setIsOpen(true);
456
+ if (inputRef.current)
457
+ inputRef.current.focus();
458
+ (_a = rest.onOpen) == null ? void 0 : _a.call(rest);
459
+ }, [createOption, formControlProps, isAllSelected, isEmpty, rest]);
460
+ const onClose = (0, import_react.useCallback)(() => {
461
+ var _a;
462
+ setIsOpen(false);
463
+ (_a = rest.onClose) == null ? void 0 : _a.call(rest);
464
+ }, [rest]);
465
+ const onFocusFirst = (0, import_react.useCallback)(() => {
466
+ const id2 = setTimeout(() => {
467
+ if (isEmpty || isAllSelected)
468
+ return;
469
+ const first = descendants.enabledfirstValue(({ node }) => "target" in node.dataset);
470
+ if (!first)
471
+ return;
472
+ if (!isMulti || !omitSelectedValues) {
473
+ setFocusedIndex(first.index);
474
+ } else {
475
+ if (selectedIndexes.includes(first.index)) {
476
+ const enabledFirst = enabledValues[0];
477
+ setFocusedIndex(enabledFirst.index);
478
+ } else {
479
+ setFocusedIndex(first.index);
480
+ }
481
+ }
482
+ });
483
+ timeoutIds.current.add(id2);
484
+ }, [
485
+ descendants,
486
+ enabledValues,
487
+ isAllSelected,
488
+ isEmpty,
489
+ isMulti,
490
+ omitSelectedValues,
491
+ selectedIndexes
492
+ ]);
493
+ const onFocusLast = (0, import_react.useCallback)(() => {
494
+ const id2 = setTimeout(() => {
495
+ if (isEmpty || isAllSelected)
496
+ return;
497
+ const last = descendants.enabledlastValue(({ node }) => "target" in node.dataset);
498
+ if (!last)
499
+ return;
500
+ if (!isMulti || !omitSelectedValues) {
501
+ setFocusedIndex(last.index);
502
+ } else {
503
+ if (selectedIndexes.includes(last.index)) {
504
+ const enabledlast = enabledValues.reverse()[0];
505
+ setFocusedIndex(enabledlast.index);
506
+ } else {
507
+ setFocusedIndex(last.index);
508
+ }
509
+ }
510
+ });
511
+ timeoutIds.current.add(id2);
512
+ }, [
513
+ descendants,
514
+ enabledValues,
515
+ isAllSelected,
516
+ isEmpty,
517
+ isMulti,
518
+ omitSelectedValues,
519
+ selectedIndexes
520
+ ]);
521
+ const onFocusSelected = (0, import_react.useCallback)(() => {
522
+ const id2 = setTimeout(() => {
523
+ const values = descendants.enabledValues();
524
+ const selected = values.find(
525
+ ({ node }) => {
526
+ var _a;
527
+ return !isMulti ? node.dataset.value === value : value.includes((_a = node.dataset.value) != null ? _a : "");
528
+ }
529
+ );
530
+ if (selected)
531
+ setFocusedIndex(selected.index);
532
+ });
533
+ timeoutIds.current.add(id2);
534
+ }, [descendants, isMulti, value]);
535
+ const onFocusNext = (0, import_react.useCallback)(
536
+ (index = focusedIndex) => {
537
+ const id2 = setTimeout(() => {
538
+ var _a;
539
+ const next = descendants.enabledNextValue(index, ({ node }) => "target" in node.dataset);
540
+ if (!next)
541
+ return;
542
+ if (!isMulti || !omitSelectedValues) {
543
+ setFocusedIndex(next.index);
544
+ } else {
545
+ if (selectedIndexes.includes(next.index)) {
546
+ const enabledNext = (_a = enabledValues.find(({ index: index2 }) => next.index < index2)) != null ? _a : enabledValues[0];
547
+ setFocusedIndex(enabledNext.index);
548
+ } else {
549
+ setFocusedIndex(next.index);
550
+ }
551
+ }
552
+ });
553
+ timeoutIds.current.add(id2);
554
+ },
555
+ [descendants, enabledValues, focusedIndex, isMulti, omitSelectedValues, selectedIndexes]
556
+ );
557
+ const onFocusPrev = (0, import_react.useCallback)(
558
+ (index = focusedIndex) => {
559
+ const id2 = setTimeout(() => {
560
+ var _a;
561
+ const prev = descendants.enabledPrevValue(index, ({ node }) => "target" in node.dataset);
562
+ if (!prev)
563
+ return;
564
+ if (!isMulti || !omitSelectedValues) {
565
+ setFocusedIndex(prev.index);
566
+ } else {
567
+ if (selectedIndexes.includes(prev.index)) {
568
+ const enabledPrev = (_a = enabledValues.reverse().find(({ index: index2 }) => index2 < prev.index)) != null ? _a : enabledValues[0];
569
+ setFocusedIndex(enabledPrev.index);
570
+ } else {
571
+ setFocusedIndex(prev.index);
572
+ }
573
+ }
574
+ });
575
+ timeoutIds.current.add(id2);
576
+ },
577
+ [descendants, enabledValues, focusedIndex, isMulti, omitSelectedValues, selectedIndexes]
578
+ );
579
+ const onFocusFirstOrSelected = isEmptyValue || omitSelectedValues ? onFocusFirst : onFocusSelected;
580
+ const onFocusLastOrSelected = isEmptyValue || omitSelectedValues ? onFocusLast : onFocusSelected;
581
+ const pickOptions = (0, import_react.useCallback)(
582
+ (value2) => {
583
+ const values = descendants.values();
584
+ let isHit2 = false;
585
+ let isFocused2 = false;
586
+ values.forEach(({ node, index }) => {
587
+ var _a;
588
+ if (format((_a = node.textContent) != null ? _a : "").includes(value2)) {
589
+ isHit2 = true;
590
+ const isDisabled = "disabled" in node.dataset;
591
+ node.dataset.target = "";
592
+ if (!isFocused2 && !isDisabled) {
593
+ isFocused2 = true;
594
+ setFocusedIndex(index);
595
+ }
596
+ } else {
597
+ delete node.dataset.target;
598
+ }
599
+ });
600
+ setIsHit(isHit2);
601
+ },
602
+ [descendants, format]
603
+ );
604
+ const rebirthOptions = (0, import_react.useCallback)(
605
+ (runFocus = true) => {
606
+ const values = descendants.values();
607
+ values.forEach(({ node }) => {
608
+ node.dataset.target = "";
609
+ });
610
+ if (runFocus)
611
+ onFocusFirst();
612
+ setIsHit(true);
613
+ },
614
+ [descendants, onFocusFirst]
615
+ );
616
+ const onChangeDisplayValue = (0, import_react.useCallback)(
617
+ (newValue, runOmit = true) => {
618
+ const enabledValues2 = descendants.enabledValues();
619
+ const selectedValues2 = enabledValues2.filter(({ node }) => node.dataset.value === newValue).map(({ node }) => {
620
+ var _a;
621
+ return (_a = node.textContent) != null ? _a : "";
622
+ });
623
+ setDisplayValue((prev) => {
624
+ if (!isMulti) {
625
+ return selectedValues2[0];
626
+ } else {
627
+ selectedValues2.forEach((selectedValue) => {
628
+ const isSelected = (0, import_utils6.isArray)(prev) && prev.includes(selectedValue != null ? selectedValue : "");
629
+ if (!isSelected) {
630
+ prev = [...(0, import_utils6.isArray)(prev) ? prev : [], selectedValue];
631
+ } else if (runOmit) {
632
+ prev = (0, import_utils6.isArray)(prev) ? prev.filter((value2) => value2 !== selectedValue) : void 0;
633
+ }
634
+ });
635
+ return prev;
636
+ }
637
+ });
638
+ },
639
+ [descendants, isMulti]
640
+ );
641
+ const onChange = (0, import_react.useCallback)(
642
+ (newValue) => {
643
+ setValue((prev) => {
644
+ if (!(0, import_utils6.isArray)(prev)) {
645
+ return newValue;
646
+ } else {
647
+ const isSelected = prev.includes(newValue);
648
+ if (!isSelected) {
649
+ return [...prev, newValue];
650
+ } else {
651
+ return prev.filter((value2) => value2 !== newValue);
652
+ }
653
+ }
654
+ });
655
+ onChangeDisplayValue(newValue);
656
+ setInputValue("");
657
+ rebirthOptions(false);
658
+ },
659
+ [onChangeDisplayValue, rebirthOptions, setValue]
660
+ );
661
+ const onSelect = (0, import_react.useCallback)(() => {
662
+ var _a, _b;
663
+ let enabledValue = descendants.value(focusedIndex);
664
+ if ("disabled" in ((_a = enabledValue == null ? void 0 : enabledValue.node.dataset) != null ? _a : {}))
665
+ enabledValue = void 0;
666
+ if (!enabledValue)
667
+ return;
668
+ const value2 = (_b = enabledValue.node.dataset.value) != null ? _b : "";
669
+ onChange(value2);
670
+ if (closeOnSelect)
671
+ onClose();
672
+ if (omitSelectedValues)
673
+ onFocusNext();
674
+ }, [closeOnSelect, descendants, focusedIndex, omitSelectedValues, onChange, onClose, onFocusNext]);
675
+ const onSearch = (0, import_react.useCallback)(
676
+ (ev) => {
677
+ var _a;
678
+ if (!isOpen)
679
+ onOpen();
680
+ (_a = rest.onSearch) == null ? void 0 : _a.call(rest, ev);
681
+ const value2 = ev.target.value;
682
+ const computedValue = format(value2);
683
+ if (computedValue) {
684
+ pickOptions(computedValue);
685
+ } else {
686
+ rebirthOptions();
687
+ }
688
+ setInputValue(value2);
689
+ },
690
+ [isOpen, onOpen, format, rest, pickOptions, rebirthOptions]
691
+ );
692
+ const onCompositionStart = (0, import_react.useCallback)(() => {
693
+ isComposition.current = true;
694
+ }, []);
695
+ const onCompositionEnd = (0, import_react.useCallback)(() => {
696
+ isComposition.current = false;
697
+ }, []);
698
+ const onCreate = (0, import_react.useCallback)(() => {
699
+ var _a;
700
+ if (!listRef.current)
701
+ return;
702
+ const newOption = { label: inputValue, value: inputValue };
703
+ let newData = [];
704
+ if (data)
705
+ newData = data;
706
+ if (createOrder === "first") {
707
+ newData = [newOption, ...newData];
708
+ } else if (createOrder === "last") {
709
+ newData = [...newData, newOption];
710
+ } else {
711
+ const i = newData.findIndex(({ label }) => label === createOrder);
712
+ if (i !== -1 && (0, import_utils6.isArray)(newData[i].value)) {
713
+ if (createSecondOrder === "first") {
714
+ newData[i].value = [newOption, ...newData[i].value];
715
+ } else {
716
+ newData[i].value = [...newData[i].value, newOption];
717
+ }
718
+ } else {
719
+ console.warn(
720
+ `${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: '${createOrder}' specified in createOrder does not exist in the option group.`
721
+ );
722
+ }
723
+ }
724
+ setData(newData);
725
+ onChange(inputValue);
726
+ rebirthOptions(false);
727
+ const index = flattenData(newData).findIndex(({ value: value2 }) => value2 === inputValue);
728
+ setFocusedIndex(index);
729
+ (_a = rest.onCreate) == null ? void 0 : _a.call(rest, newOption, newData);
730
+ }, [inputValue, data, createOrder, onChange, rebirthOptions, rest, createSecondOrder, isMulti]);
731
+ const onDelete = (0, import_react.useCallback)(() => {
732
+ if (!isMulti) {
733
+ onChange("");
734
+ } else {
735
+ onChange(value[value.length - 1]);
736
+ }
737
+ }, [isMulti, onChange, value]);
738
+ const onClear = (0, import_react.useCallback)(
739
+ (ev) => {
740
+ ev.stopPropagation();
741
+ setValue([]);
742
+ setDisplayValue(void 0);
743
+ setInputValue("");
744
+ rebirthOptions();
745
+ if (inputRef.current)
746
+ inputRef.current.focus();
747
+ },
748
+ [setDisplayValue, setInputValue, setValue, rebirthOptions, inputRef]
749
+ );
750
+ const onClick = (0, import_react.useCallback)(() => {
751
+ if (isOpen) {
752
+ if (inputRef.current)
753
+ inputRef.current.focus();
754
+ } else {
755
+ onOpen();
756
+ onFocusFirstOrSelected();
757
+ }
758
+ }, [isOpen, onFocusFirstOrSelected, onOpen]);
759
+ const onFocus = (0, import_react.useCallback)(() => {
760
+ if (isOpen)
761
+ return;
762
+ onOpen();
763
+ onFocusFirstOrSelected();
764
+ }, [isOpen, onFocusFirstOrSelected, onOpen]);
765
+ const onBlur = (0, import_react.useCallback)(
766
+ (ev) => {
767
+ const relatedTarget = (0, import_utils6.getEventRelatedTarget)(ev);
768
+ if ((0, import_utils6.isContains)(containerRef.current, relatedTarget))
769
+ return;
770
+ if (!closeOnBlur && isHit)
771
+ return;
772
+ setInputValue("");
773
+ if (isOpen)
774
+ onClose();
775
+ },
776
+ [closeOnBlur, isHit, isOpen, onClose]
777
+ );
778
+ const onKeyDown = (0, import_react.useCallback)(
779
+ (ev) => {
780
+ if (ev.key === " ")
781
+ ev.key = ev.code;
782
+ if (formControlProps.disabled || formControlProps.readOnly)
783
+ return;
784
+ if (isComposition.current)
785
+ return;
786
+ const enabledDelete = displayValue === inputValue || !inputValue.length;
787
+ const actions = {
788
+ ArrowDown: isFocused ? () => onFocusNext() : !isOpen ? (0, import_utils6.funcAll)(onOpen, onFocusFirstOrSelected) : void 0,
789
+ ArrowUp: isFocused ? () => onFocusPrev() : !isOpen ? (0, import_utils6.funcAll)(onOpen, onFocusLastOrSelected) : void 0,
790
+ Space: isCreate ? onCreate : isFocused ? onSelect : !isOpen ? (0, import_utils6.funcAll)(onOpen, onFocusFirstOrSelected) : void 0,
791
+ Enter: isCreate ? onCreate : isFocused ? onSelect : !isOpen ? (0, import_utils6.funcAll)(onOpen, onFocusFirstOrSelected) : void 0,
792
+ Home: isOpen ? onFocusFirst : void 0,
793
+ End: isOpen ? onFocusLast : void 0,
794
+ Escape: closeOnEsc ? onClose : void 0,
795
+ Backspace: !isEmptyValue && enabledDelete ? onDelete : void 0
796
+ };
797
+ const action = actions[ev.key];
798
+ if (!action)
799
+ return;
800
+ ev.preventDefault();
801
+ ev.stopPropagation();
802
+ action(ev);
803
+ },
804
+ [
805
+ formControlProps,
806
+ displayValue,
807
+ inputValue,
808
+ onOpen,
809
+ isFocused,
810
+ onFocusFirstOrSelected,
811
+ onFocusNext,
812
+ onFocusLastOrSelected,
813
+ onFocusPrev,
814
+ isCreate,
815
+ onCreate,
816
+ onSelect,
817
+ isOpen,
818
+ onFocusFirst,
819
+ onFocusLast,
820
+ closeOnEsc,
821
+ onClose,
822
+ isEmptyValue,
823
+ onDelete
824
+ ]
825
+ );
826
+ (0, import_react.useEffect)(() => {
827
+ if (!isMulti)
828
+ return;
829
+ if (!omitSelectedValues && (0, import_utils6.isUndefined)(maxSelectedValues))
830
+ return;
831
+ const isAll = value.length > 0 && value.length === descendants.count();
832
+ const isMax = value.length === maxSelectedValues;
833
+ if (isAll || isMax) {
834
+ onClose();
835
+ setIsAllSelected(true);
836
+ } else {
837
+ setIsAllSelected(false);
838
+ }
839
+ }, [omitSelectedValues, value, descendants, isMulti, onClose, maxSelectedValues]);
840
+ (0, import_utils6.useUpdateEffect)(() => {
841
+ if (isOpen)
842
+ return;
843
+ setFocusedIndex(-1);
844
+ setInputValue("");
845
+ }, [isOpen]);
846
+ (0, import_utils6.useUpdateEffect)(() => {
847
+ if (!isHit)
848
+ setFocusedIndex(-2);
849
+ }, [isHit]);
850
+ (0, import_utils6.useUnmountEffect)(() => {
851
+ timeoutIds.current.forEach((id2) => clearTimeout(id2));
852
+ timeoutIds.current.clear();
853
+ });
854
+ (0, import_use_outside_click.useOutsideClick)({
855
+ ref: containerRef,
856
+ handler: onClose,
857
+ enabled: closeOnBlur || !isHit
858
+ });
859
+ const getPopoverProps = (0, import_react.useCallback)(
860
+ (props) => ({
861
+ ...rest,
862
+ ...props,
863
+ isOpen,
864
+ onOpen,
865
+ onClose,
866
+ placement,
867
+ duration,
868
+ trigger: "never",
869
+ closeOnButton: false
870
+ }),
871
+ [duration, isOpen, onClose, onOpen, placement, rest]
872
+ );
873
+ const getContainerProps = (0, import_react.useCallback)(
874
+ (props = {}, ref = null) => ({
875
+ ref: (0, import_utils6.mergeRefs)(containerRef, ref),
876
+ ...containerProps,
877
+ ...props,
878
+ ...formControlProps,
879
+ onClick: (0, import_utils6.handlerAll)(props.onClick, rest.onClick, onClick),
880
+ onFocus: (0, import_utils6.handlerAll)(props.onFocus, rest.onFocus, onFocus),
881
+ onBlur: (0, import_utils6.handlerAll)(props.onBlur, rest.onBlur, onBlur)
882
+ }),
883
+ [containerProps, formControlProps, onBlur, onClick, onFocus, rest]
884
+ );
885
+ const getFieldProps = (0, import_react.useCallback)(
886
+ (props = {}, ref = null) => ({
887
+ ref,
888
+ tabIndex: -1,
889
+ ...props,
890
+ ...formControlProps,
891
+ placeholder,
892
+ "data-active": (0, import_utils6.dataAttr)(isOpen),
893
+ "aria-expanded": (0, import_utils6.dataAttr)(isOpen),
894
+ onKeyDown: (0, import_utils6.handlerAll)(props.onKeyDown, rest.onKeyDown, onKeyDown)
895
+ }),
896
+ [formControlProps, placeholder, isOpen, rest, onKeyDown]
897
+ );
898
+ return {
899
+ id,
900
+ descendants,
901
+ value,
902
+ displayValue,
903
+ inputValue,
904
+ isHit,
905
+ isEmpty,
906
+ computedChildren,
907
+ focusedIndex,
908
+ omitSelectedValues,
909
+ closeOnSelect,
910
+ createOption,
911
+ emptyMessage,
912
+ isOpen,
913
+ isAllSelected,
914
+ listRef,
915
+ inputRef,
916
+ optionProps,
917
+ formControlProps,
918
+ setFocusedIndex,
919
+ onChangeDisplayValue,
920
+ onChange,
921
+ onSearch,
922
+ onCreate,
923
+ onClear,
924
+ onCompositionStart,
925
+ onCompositionEnd,
926
+ pickOptions,
927
+ rebirthOptions,
928
+ onOpen,
929
+ onClose,
930
+ onFocusFirst,
931
+ onFocusLast,
932
+ onFocusSelected,
933
+ onFocusNext,
934
+ onFocusPrev,
935
+ getPopoverProps,
936
+ getContainerProps,
937
+ getFieldProps,
938
+ inputProps
939
+ };
940
+ };
941
+ var useAutocompleteInput = () => {
942
+ const {
943
+ id,
944
+ inputRef,
945
+ onSearch,
946
+ onCompositionStart,
947
+ onCompositionEnd,
948
+ isAllSelected,
949
+ formControlProps,
950
+ inputProps
951
+ } = useAutocompleteContext();
952
+ (0, import_utils6.useUpdateEffect)(() => {
953
+ if (isAllSelected && inputRef.current)
954
+ inputRef.current.blur();
955
+ }, [isAllSelected]);
956
+ const getInputProps = (0, import_react.useCallback)(
957
+ (props = {}, ref = null) => ({
958
+ ref: (0, import_utils6.mergeRefs)(inputRef, ref),
959
+ ...formControlProps,
960
+ ...inputProps,
961
+ ...props,
962
+ id,
963
+ cursor: formControlProps.readOnly ? "default" : "text",
964
+ pointerEvents: formControlProps.disabled || isAllSelected ? "none" : "auto",
965
+ tabIndex: isAllSelected ? -1 : 0,
966
+ onChange: (0, import_utils6.handlerAll)(props.onChange, onSearch),
967
+ onCompositionStart: (0, import_utils6.handlerAll)(
968
+ props.onCompositionStart,
969
+ inputProps.onCompositionStart,
970
+ onCompositionStart
971
+ ),
972
+ onCompositionEnd: (0, import_utils6.handlerAll)(
973
+ props.onCompositionEnd,
974
+ inputProps.onCompositionEnd,
975
+ onCompositionEnd
976
+ )
977
+ }),
978
+ [
979
+ inputProps,
980
+ inputRef,
981
+ formControlProps,
982
+ id,
983
+ isAllSelected,
984
+ onSearch,
985
+ onCompositionStart,
986
+ onCompositionEnd
987
+ ]
988
+ );
989
+ return {
990
+ getInputProps
991
+ };
992
+ };
993
+ var useAutocompleteList = () => {
994
+ const { focusedIndex, isOpen, listRef, rebirthOptions } = useAutocompleteContext();
995
+ const descendants = useAutocompleteDescendantsContext();
996
+ const beforeFocusedIndex = (0, import_react.useRef)(-1);
997
+ const selectedValue = descendants.value(focusedIndex);
998
+ const onAnimationComplete = (0, import_react.useCallback)(() => {
999
+ if (!isOpen)
1000
+ rebirthOptions(false);
1001
+ }, [isOpen, rebirthOptions]);
1002
+ (0, import_react.useEffect)(() => {
1003
+ if (!listRef.current || !selectedValue)
1004
+ return;
1005
+ const parent = listRef.current;
1006
+ const child = selectedValue.node;
1007
+ const parentHeight = parent.clientHeight;
1008
+ const viewTop = parent.scrollTop;
1009
+ const viewBottom = viewTop + parentHeight;
1010
+ const childHeight = child.clientHeight;
1011
+ const childTop = child.offsetTop;
1012
+ const childBottom = childTop + childHeight;
1013
+ const isInView = viewTop <= childTop && childBottom <= viewBottom;
1014
+ const isScrollBottom = beforeFocusedIndex.current < selectedValue.index;
1015
+ if (!isInView) {
1016
+ if (childBottom <= parentHeight) {
1017
+ listRef.current.scrollTo({ top: 0 });
1018
+ } else {
1019
+ if (!isScrollBottom) {
1020
+ listRef.current.scrollTo({ top: childTop + 1 });
1021
+ } else {
1022
+ listRef.current.scrollTo({ top: childBottom - parentHeight });
1023
+ }
1024
+ }
1025
+ }
1026
+ beforeFocusedIndex.current = selectedValue.index;
1027
+ }, [listRef, selectedValue]);
1028
+ (0, import_utils6.useUpdateEffect)(() => {
1029
+ if (!isOpen)
1030
+ beforeFocusedIndex.current = -1;
1031
+ }, [isOpen]);
1032
+ const getListProps = (0, import_react.useCallback)(
1033
+ (props = {}, ref = null) => ({
1034
+ as: "ul",
1035
+ ref: (0, import_utils6.mergeRefs)(listRef, ref),
1036
+ role: "select",
1037
+ tabIndex: -1,
1038
+ ...props,
1039
+ onAnimationComplete: (0, import_utils6.handlerAll)(props.onAnimationComplete, onAnimationComplete)
1040
+ }),
1041
+ [listRef, onAnimationComplete]
1042
+ );
1043
+ return {
1044
+ getListProps
1045
+ };
1046
+ };
1047
+ var useAutocompleteOptionGroup = ({ label, ...rest }) => {
1048
+ const { value, omitSelectedValues } = useAutocompleteContext();
1049
+ const isMulti = (0, import_utils6.isArray)(value);
1050
+ const descendants = useAutocompleteDescendantsContext();
1051
+ const values = descendants.values();
1052
+ const selectedValues = isMulti && omitSelectedValues ? descendants.values(({ node }) => {
1053
+ var _a;
1054
+ return value.includes((_a = node.dataset.value) != null ? _a : "");
1055
+ }) : [];
1056
+ const selectedIndexes = selectedValues.map(({ index }) => index);
1057
+ const childValues = values.filter(
1058
+ ({ node, index }) => {
1059
+ var _a;
1060
+ return ((_a = node.parentElement) == null ? void 0 : _a.dataset.label) === label && !selectedIndexes.includes(index) && "target" in node.dataset;
1061
+ }
1062
+ );
1063
+ const isEmpty = !childValues.length;
1064
+ const computedRest = (0, import_utils6.splitObject)(rest, import_core6.layoutStylesProperties);
1065
+ const getContainerProps = (0, import_react.useCallback)(
1066
+ (props = {}, ref = null) => {
1067
+ const style = {
1068
+ border: "0px",
1069
+ clip: "rect(0px, 0px, 0px, 0px)",
1070
+ height: "1px",
1071
+ width: "1px",
1072
+ margin: "-1px",
1073
+ padding: "0px",
1074
+ overflow: "hidden",
1075
+ whiteSpace: "nowrap",
1076
+ position: "absolute"
1077
+ };
1078
+ return {
1079
+ ref,
1080
+ ...props,
1081
+ ...computedRest[0],
1082
+ style: isEmpty ? style : void 0,
1083
+ "data-label": label,
1084
+ role: "autocomplete-group-container"
1085
+ };
1086
+ },
1087
+ [computedRest, isEmpty, label]
1088
+ );
1089
+ const getGroupProps = (0, import_react.useCallback)(
1090
+ (props = {}, ref = null) => ({
1091
+ ref,
1092
+ ...props,
1093
+ ...computedRest[1],
1094
+ "data-label": label,
1095
+ role: "autocomplete-group"
1096
+ }),
1097
+ [computedRest, label]
1098
+ );
1099
+ return {
1100
+ label,
1101
+ getContainerProps,
1102
+ getGroupProps
1103
+ };
1104
+ };
1105
+ var useAutocompleteOption = (props) => {
1106
+ var _a, _b, _c, _d;
1107
+ const {
1108
+ value,
1109
+ omitSelectedValues,
1110
+ onChange,
1111
+ onChangeDisplayValue,
1112
+ focusedIndex,
1113
+ setFocusedIndex,
1114
+ onClose,
1115
+ closeOnSelect: generalCloseOnSelect,
1116
+ optionProps,
1117
+ inputRef,
1118
+ onFocusNext
1119
+ } = useAutocompleteContext();
1120
+ let {
1121
+ icon: customIcon,
1122
+ isDisabled,
1123
+ isFocusable,
1124
+ closeOnSelect: customCloseOnSelect,
1125
+ children,
1126
+ ...computedProps
1127
+ } = { ...optionProps, ...props };
1128
+ const trulyDisabled = !!isDisabled && !isFocusable;
1129
+ const itemRef = (0, import_react.useRef)(null);
1130
+ const { index, register, descendants } = useAutocompleteDescendant({ disabled: trulyDisabled });
1131
+ const values = descendants.values();
1132
+ const frontValues = values.slice(0, index);
1133
+ const isMulti = (0, import_utils6.isArray)(value);
1134
+ const isDuplicated = !isMulti ? frontValues.some(({ node }) => {
1135
+ var _a2;
1136
+ return node.dataset.value === ((_a2 = computedProps.value) != null ? _a2 : "");
1137
+ }) : false;
1138
+ const isSelected = !isDuplicated && (!isMulti ? ((_a = computedProps.value) != null ? _a : "") === value : value.includes((_b = computedProps.value) != null ? _b : ""));
1139
+ const isTarget = "target" in ((_d = (_c = itemRef.current) == null ? void 0 : _c.dataset) != null ? _d : {});
1140
+ const isFocused = index === focusedIndex;
1141
+ const onClick = (0, import_react.useCallback)(
1142
+ (ev) => {
1143
+ var _a2;
1144
+ ev.stopPropagation();
1145
+ if (isDisabled) {
1146
+ if (inputRef.current)
1147
+ inputRef.current.focus();
1148
+ return;
1149
+ }
1150
+ if (!isTargetOption(ev.currentTarget)) {
1151
+ if (inputRef.current)
1152
+ inputRef.current.focus();
1153
+ return;
1154
+ }
1155
+ setFocusedIndex(index);
1156
+ onChange((_a2 = computedProps.value) != null ? _a2 : "");
1157
+ if (inputRef.current)
1158
+ inputRef.current.focus();
1159
+ if (customCloseOnSelect != null ? customCloseOnSelect : generalCloseOnSelect)
1160
+ onClose();
1161
+ if (omitSelectedValues)
1162
+ onFocusNext(index);
1163
+ },
1164
+ [
1165
+ onFocusNext,
1166
+ omitSelectedValues,
1167
+ isDisabled,
1168
+ computedProps,
1169
+ setFocusedIndex,
1170
+ index,
1171
+ onChange,
1172
+ customCloseOnSelect,
1173
+ generalCloseOnSelect,
1174
+ onClose,
1175
+ inputRef
1176
+ ]
1177
+ );
1178
+ (0, import_react.useEffect)(() => {
1179
+ var _a2;
1180
+ if (isSelected)
1181
+ onChangeDisplayValue((_a2 = computedProps.value) != null ? _a2 : "", false);
1182
+ }, [computedProps, isSelected, onChangeDisplayValue]);
1183
+ const getOptionProps = (0, import_react.useCallback)(
1184
+ (props2 = {}, ref = null) => {
1185
+ var _a2;
1186
+ const style = {
1187
+ border: "0px",
1188
+ clip: "rect(0px, 0px, 0px, 0px)",
1189
+ height: "1px",
1190
+ width: "1px",
1191
+ margin: "-1px",
1192
+ padding: "0px",
1193
+ overflow: "hidden",
1194
+ whiteSpace: "nowrap",
1195
+ position: "absolute"
1196
+ };
1197
+ return {
1198
+ ref: (0, import_utils6.mergeRefs)(itemRef, ref, register),
1199
+ ...(0, import_utils6.omitObject)(computedProps, ["value"]),
1200
+ ...props2,
1201
+ role: "autocomplete-item",
1202
+ tabIndex: -1,
1203
+ style: !isTarget || omitSelectedValues && isSelected ? style : void 0,
1204
+ "data-target": (0, import_utils6.dataAttr)(true),
1205
+ "data-value": (_a2 = computedProps.value) != null ? _a2 : "",
1206
+ "data-focus": (0, import_utils6.dataAttr)(isFocused),
1207
+ "data-disabled": (0, import_utils6.dataAttr)(isDisabled),
1208
+ "aria-checked": (0, import_utils6.ariaAttr)(isSelected),
1209
+ "aria-disabled": (0, import_utils6.ariaAttr)(isDisabled),
1210
+ onClick: (0, import_utils6.handlerAll)(computedProps.onClick, props2.onClick, onClick)
1211
+ };
1212
+ },
1213
+ [
1214
+ computedProps,
1215
+ isDisabled,
1216
+ isFocused,
1217
+ isSelected,
1218
+ isTarget,
1219
+ omitSelectedValues,
1220
+ onClick,
1221
+ register
1222
+ ]
1223
+ );
1224
+ return {
1225
+ isSelected,
1226
+ isFocused,
1227
+ customIcon,
1228
+ children,
1229
+ getOptionProps
1230
+ };
1231
+ };
1232
+ var useAutocompleteCreate = () => {
1233
+ const { isHit, onCreate } = useAutocompleteContext();
1234
+ const getCreateProps = (0, import_react.useCallback)(
1235
+ (props = {}, ref = null) => {
1236
+ const style = {
1237
+ border: "0px",
1238
+ clip: "rect(0px, 0px, 0px, 0px)",
1239
+ height: "1px",
1240
+ width: "1px",
1241
+ margin: "-1px",
1242
+ padding: "0px",
1243
+ overflow: "hidden",
1244
+ whiteSpace: "nowrap",
1245
+ position: "absolute"
1246
+ };
1247
+ return {
1248
+ ref,
1249
+ ...props,
1250
+ tabIndex: -1,
1251
+ style: isHit ? style : void 0,
1252
+ "data-focus": (0, import_utils6.dataAttr)(!isHit),
1253
+ onClick: (0, import_utils6.handlerAll)(props.onClick, onCreate)
1254
+ };
1255
+ },
1256
+ [isHit, onCreate]
1257
+ );
1258
+ return { getCreateProps };
1259
+ };
1260
+ var useAutocompleteEmpty = () => {
1261
+ const { isHit, isEmpty } = useAutocompleteContext();
1262
+ const getEmptyProps = (0, import_react.useCallback)(
1263
+ (props = {}, ref = null) => {
1264
+ const style = {
1265
+ border: "0px",
1266
+ clip: "rect(0px, 0px, 0px, 0px)",
1267
+ height: "1px",
1268
+ width: "1px",
1269
+ margin: "-1px",
1270
+ padding: "0px",
1271
+ overflow: "hidden",
1272
+ whiteSpace: "nowrap",
1273
+ position: "absolute"
1274
+ };
1275
+ return {
1276
+ ref,
1277
+ ...props,
1278
+ tabIndex: -1,
1279
+ style: isHit && !isEmpty ? style : void 0
1280
+ };
1281
+ },
1282
+ [isHit, isEmpty]
1283
+ );
1284
+ return { getEmptyProps };
1285
+ };
1286
+
1287
+ // src/autocomplete-icon.tsx
1288
+ var import_jsx_runtime7 = require("react/jsx-runtime");
1289
+ var AutocompleteIcon = (0, import_core7.forwardRef)(
1290
+ ({ className, children, __css, ...rest }, ref) => {
1291
+ const { styles } = useAutocompleteContext();
1292
+ const css = {
1293
+ position: "absolute",
1294
+ top: "50%",
1295
+ transform: "translateY(-50%)",
1296
+ display: "inline-flex",
1297
+ alignItems: "center",
1298
+ justifyContent: "center",
1299
+ pointerEvents: "none",
1300
+ cursor: "pointer",
1301
+ ...styles.icon,
1302
+ ...__css
1303
+ };
1304
+ const validChildren = (0, import_utils7.getValidChildren)(children);
1305
+ const cloneChildren = validChildren.map(
1306
+ (child) => (0, import_react2.cloneElement)(child, {
1307
+ focusable: false,
1308
+ "aria-hidden": true,
1309
+ style: {
1310
+ maxWidth: "1em",
1311
+ maxHeight: "1em",
1312
+ color: "currentColor"
1313
+ }
1314
+ })
1315
+ );
1316
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_core7.ui.div, { ref, className: (0, import_utils7.cx)("ui-autocomplete-icon", className), __css: css, ...rest, children: (0, import_utils7.isValidElement)(children) ? cloneChildren : /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_icon.ChevronIcon, {}) });
1317
+ }
1318
+ );
1319
+ var AutocompleteItemIcon = (0, import_core7.forwardRef)(
1320
+ ({ className, ...rest }, ref) => {
1321
+ const { styles } = useAutocompleteContext();
1322
+ const css = {
1323
+ flexShrink: 0,
1324
+ display: "inline-flex",
1325
+ justifyContent: "center",
1326
+ alignItems: "center",
1327
+ fontSize: "0.85em",
1328
+ ...styles.itemIcon
1329
+ };
1330
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
1331
+ import_core7.ui.span,
1332
+ {
1333
+ ref,
1334
+ className: (0, import_utils7.cx)("ui-autocomplete-item-icon", className),
1335
+ __css: css,
1336
+ ...rest
1337
+ }
1338
+ );
1339
+ }
1340
+ );
1341
+
1342
+ // src/autocomplete.tsx
1343
+ var import_jsx_runtime8 = require("react/jsx-runtime");
1344
+ var Autocomplete = (0, import_core8.forwardRef)((props, ref) => {
1345
+ const [styles, mergedProps] = (0, import_core8.useMultiComponentStyle)("Select", props);
1346
+ let {
1347
+ className,
1348
+ defaultValue = "",
1349
+ color,
1350
+ h,
1351
+ height,
1352
+ minH,
1353
+ minHeight,
1354
+ containerProps,
1355
+ listProps,
1356
+ inputProps,
1357
+ iconProps,
1358
+ children,
1359
+ ...computedProps
1360
+ } = (0, import_core8.omitThemeProps)(mergedProps);
1361
+ const {
1362
+ descendants,
1363
+ formControlProps,
1364
+ getPopoverProps,
1365
+ getContainerProps,
1366
+ getFieldProps,
1367
+ createOption,
1368
+ isEmpty,
1369
+ inputValue,
1370
+ computedChildren,
1371
+ ...rest
1372
+ } = useAutocomplete({ ...computedProps, defaultValue, children });
1373
+ h = h != null ? h : height;
1374
+ minH = minH != null ? minH : minHeight;
1375
+ const css = {
1376
+ position: "relative",
1377
+ w: "100%",
1378
+ h: "fit-content",
1379
+ color,
1380
+ ...styles.container
1381
+ };
1382
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(AutocompleteDescendantsContextProvider, { value: descendants, children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
1383
+ AutocompleteProvider,
1384
+ {
1385
+ value: { ...rest, formControlProps, inputValue, createOption, isEmpty, styles },
1386
+ children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_popover2.Popover, { ...getPopoverProps(), children: /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
1387
+ import_core8.ui.div,
1388
+ {
1389
+ className: (0, import_utils8.cx)("ui-autocomplete", className),
1390
+ __css: css,
1391
+ ...getContainerProps(containerProps),
1392
+ children: [
1393
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
1394
+ AutocompleteField,
1395
+ {
1396
+ h,
1397
+ minH,
1398
+ inputProps,
1399
+ ...getFieldProps({}, ref)
1400
+ }
1401
+ ),
1402
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(AutocompleteIcon, { ...iconProps, ...formControlProps }),
1403
+ !isEmpty ? /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(AutocompleteList, { ...listProps, children: [
1404
+ createOption ? /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(AutocompleteCreate, {}) : /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(AutocompleteEmpty, {}),
1405
+ children != null ? children : computedChildren
1406
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(AutocompleteList, { ...listProps, children: createOption && inputValue ? /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(AutocompleteCreate, {}) : /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(AutocompleteEmpty, {}) })
1407
+ ]
1408
+ }
1409
+ ) })
1410
+ }
1411
+ ) });
1412
+ });
1413
+ var AutocompleteField = (0, import_core8.forwardRef)(
1414
+ ({ className, h, minH, placeholder, inputProps, ...rest }, ref) => {
1415
+ const { displayValue, inputValue, styles } = useAutocompleteContext();
1416
+ const { getInputProps } = useAutocompleteInput();
1417
+ const css = {
1418
+ paddingEnd: "2rem",
1419
+ h,
1420
+ minH,
1421
+ display: "flex",
1422
+ alignItems: "center",
1423
+ ...styles.field,
1424
+ cursor: "text"
1425
+ };
1426
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_popover2.PopoverTrigger, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_core8.ui.div, { className: (0, import_utils8.cx)("ui-autocomplete-field", className), __css: css, ...rest, children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
1427
+ import_core8.ui.input,
1428
+ {
1429
+ className: "ui-autocomplete-input",
1430
+ display: "inline-block",
1431
+ w: "full",
1432
+ placeholder,
1433
+ ...getInputProps(inputProps, ref),
1434
+ value: inputValue || displayValue || ""
1435
+ }
1436
+ ) }) });
1437
+ }
1438
+ );
1439
+ // Annotate the CommonJS export names for ESM import in node:
1440
+ 0 && (module.exports = {
1441
+ Autocomplete
1442
+ });