@heroui/autocomplete 2.3.12 → 2.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,459 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ Autocomplete: () => autocomplete_default,
25
+ AutocompleteItem: () => import_listbox2.ListboxItem,
26
+ AutocompleteSection: () => import_listbox2.ListboxSection,
27
+ useAutocomplete: () => useAutocomplete
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+ var import_listbox2 = require("@heroui/listbox");
31
+
32
+ // src/autocomplete.tsx
33
+ var import_system2 = require("@heroui/system");
34
+ var import_popover = require("@heroui/popover");
35
+ var import_scroll_shadow = require("@heroui/scroll-shadow");
36
+ var import_shared_icons = require("@heroui/shared-icons");
37
+ var import_listbox = require("@heroui/listbox");
38
+ var import_button = require("@heroui/button");
39
+ var import_input = require("@heroui/input");
40
+ var import_framer_motion = require("framer-motion");
41
+
42
+ // src/use-autocomplete.ts
43
+ var import_system = require("@heroui/system");
44
+ var import_use_safe_layout_effect = require("@heroui/use-safe-layout-effect");
45
+ var import_theme = require("@heroui/theme");
46
+ var import_i18n = require("@react-aria/i18n");
47
+ var import_combobox = require("@react-stately/combobox");
48
+ var import_react_utils = require("@heroui/react-utils");
49
+ var import_react = require("react");
50
+ var import_shared_utils = require("@heroui/shared-utils");
51
+ var import_utils = require("@react-aria/utils");
52
+ var import_combobox2 = require("@react-aria/combobox");
53
+ var import_form = require("@heroui/form");
54
+ var import_aria_utils = require("@heroui/aria-utils");
55
+ function useAutocomplete(originalProps) {
56
+ var _a, _b, _c, _d, _e;
57
+ const globalContext = (0, import_system.useProviderContext)();
58
+ const { validationBehavior: formValidationBehavior } = (0, import_form.useSlottedContext)(import_form.FormContext) || {};
59
+ const [props, variantProps] = (0, import_system.mapPropsVariants)(originalProps, import_theme.autocomplete.variantKeys);
60
+ const disableAnimation = (_b = (_a = originalProps.disableAnimation) != null ? _a : globalContext == null ? void 0 : globalContext.disableAnimation) != null ? _b : false;
61
+ const isClearable = originalProps.disableClearable !== void 0 ? !originalProps.disableClearable : originalProps.isReadOnly ? false : originalProps.isClearable;
62
+ const {
63
+ ref,
64
+ as,
65
+ label,
66
+ isLoading,
67
+ menuTrigger = "focus",
68
+ filterOptions = {
69
+ sensitivity: "base"
70
+ },
71
+ children,
72
+ selectorIcon,
73
+ clearIcon,
74
+ scrollRef: scrollRefProp,
75
+ defaultFilter,
76
+ endContent,
77
+ allowsEmptyCollection = true,
78
+ shouldCloseOnBlur = true,
79
+ popoverProps = {},
80
+ inputProps: userInputProps = {},
81
+ scrollShadowProps = {},
82
+ listboxProps = {},
83
+ selectorButtonProps = {},
84
+ clearButtonProps = {},
85
+ showScrollIndicators = true,
86
+ allowsCustomValue = false,
87
+ isVirtualized,
88
+ maxListboxHeight = 256,
89
+ itemHeight = 32,
90
+ validationBehavior = (_c = formValidationBehavior != null ? formValidationBehavior : globalContext == null ? void 0 : globalContext.validationBehavior) != null ? _c : "native",
91
+ className,
92
+ classNames,
93
+ errorMessage,
94
+ onOpenChange,
95
+ onClose,
96
+ isReadOnly = false,
97
+ ...otherProps
98
+ } = props;
99
+ const { contains } = (0, import_i18n.useFilter)(filterOptions);
100
+ let state = (0, import_combobox.useComboBoxState)({
101
+ ...originalProps,
102
+ children,
103
+ menuTrigger,
104
+ validationBehavior,
105
+ shouldCloseOnBlur,
106
+ allowsEmptyCollection,
107
+ defaultFilter: defaultFilter && typeof defaultFilter === "function" ? defaultFilter : contains,
108
+ onOpenChange: (open, menuTrigger2) => {
109
+ onOpenChange == null ? void 0 : onOpenChange(open, menuTrigger2);
110
+ if (!open) {
111
+ onClose == null ? void 0 : onClose();
112
+ }
113
+ }
114
+ });
115
+ state = {
116
+ ...state,
117
+ ...isReadOnly && {
118
+ disabledKeys: /* @__PURE__ */ new Set([...state.collection.getKeys()])
119
+ }
120
+ };
121
+ const buttonRef = (0, import_react.useRef)(null);
122
+ const inputWrapperRef = (0, import_react.useRef)(null);
123
+ const listBoxRef = (0, import_react.useRef)(null);
124
+ const popoverRef = (0, import_react.useRef)(null);
125
+ const inputRef = (0, import_react_utils.useDOMRef)(ref);
126
+ const scrollShadowRef = (0, import_react_utils.useDOMRef)(scrollRefProp);
127
+ const {
128
+ buttonProps,
129
+ inputProps,
130
+ listBoxProps,
131
+ isInvalid: isAriaInvalid,
132
+ validationDetails,
133
+ validationErrors
134
+ } = (0, import_combobox2.useComboBox)(
135
+ {
136
+ validationBehavior,
137
+ ...originalProps,
138
+ inputRef,
139
+ buttonRef,
140
+ listBoxRef,
141
+ popoverRef
142
+ },
143
+ state
144
+ );
145
+ const isInvalid = originalProps.isInvalid || isAriaInvalid;
146
+ const slotsProps = {
147
+ inputProps: (0, import_utils.mergeProps)(
148
+ {
149
+ label,
150
+ ref: inputRef,
151
+ wrapperRef: inputWrapperRef,
152
+ onClick: () => {
153
+ if (!state.isOpen && !!state.selectedItem) {
154
+ state.open();
155
+ }
156
+ },
157
+ isClearable: false,
158
+ disableAnimation
159
+ },
160
+ userInputProps
161
+ ),
162
+ popoverProps: (0, import_utils.mergeProps)(
163
+ {
164
+ offset: 5,
165
+ placement: "bottom",
166
+ triggerScaleOnOpen: false,
167
+ disableAnimation
168
+ },
169
+ popoverProps
170
+ ),
171
+ scrollShadowProps: (0, import_utils.mergeProps)(
172
+ {
173
+ ref: scrollShadowRef,
174
+ isEnabled: (_d = showScrollIndicators && state.collection.size > 5) != null ? _d : true,
175
+ hideScrollBar: true,
176
+ offset: 15
177
+ },
178
+ scrollShadowProps
179
+ ),
180
+ listboxProps: (0, import_utils.mergeProps)(
181
+ {
182
+ hideEmptyContent: allowsCustomValue,
183
+ emptyContent: "No results found.",
184
+ disableAnimation
185
+ },
186
+ listboxProps
187
+ ),
188
+ selectorButtonProps: (0, import_utils.mergeProps)(
189
+ {
190
+ isLoading,
191
+ size: "sm",
192
+ variant: "light",
193
+ radius: "full",
194
+ color: isInvalid ? "danger" : originalProps == null ? void 0 : originalProps.color,
195
+ isIconOnly: true,
196
+ disableAnimation
197
+ },
198
+ selectorButtonProps
199
+ ),
200
+ clearButtonProps: (0, import_utils.mergeProps)(
201
+ {
202
+ size: "sm",
203
+ variant: "light",
204
+ radius: "full",
205
+ color: isInvalid ? "danger" : originalProps == null ? void 0 : originalProps.color,
206
+ isIconOnly: true,
207
+ disableAnimation
208
+ },
209
+ clearButtonProps
210
+ )
211
+ };
212
+ const baseStyles = (0, import_shared_utils.clsx)(classNames == null ? void 0 : classNames.base, className);
213
+ const isOpen = ((_e = slotsProps.listboxProps) == null ? void 0 : _e.hideEmptyContent) ? state.isOpen && !!state.collection.size : state.isOpen;
214
+ (0, import_use_safe_layout_effect.useSafeLayoutEffect)(() => {
215
+ if (!inputRef.current) return;
216
+ const key = inputRef.current.value;
217
+ const item = state.collection.getItem(key);
218
+ if (item && state.inputValue !== item.textValue) {
219
+ state.setSelectedKey(key);
220
+ state.setInputValue(item.textValue);
221
+ }
222
+ }, [inputRef.current]);
223
+ (0, import_react.useEffect)(() => {
224
+ let key = state.collection.getFirstKey();
225
+ while (key && state.disabledKeys.has(key)) {
226
+ key = state.collection.getKeyAfter(key);
227
+ }
228
+ state.selectionManager.setFocusedKey(key);
229
+ }, [state.collection, state.disabledKeys]);
230
+ (0, import_react.useEffect)(() => {
231
+ if (isOpen) {
232
+ if (popoverRef.current && inputWrapperRef.current) {
233
+ let rect = inputWrapperRef.current.getBoundingClientRect();
234
+ let popover = popoverRef.current;
235
+ popover.style.width = rect.width + "px";
236
+ }
237
+ }
238
+ }, [isOpen]);
239
+ if (inputProps.onKeyDown) {
240
+ const originalOnKeyDown = inputProps.onKeyDown;
241
+ inputProps.onKeyDown = (e) => {
242
+ if ("continuePropagation" in e) {
243
+ e.stopPropagation = () => {
244
+ };
245
+ }
246
+ return originalOnKeyDown(e);
247
+ };
248
+ }
249
+ const Component = as || "div";
250
+ const slots = (0, import_react.useMemo)(
251
+ () => (0, import_theme.autocomplete)({
252
+ ...variantProps,
253
+ isClearable,
254
+ disableAnimation
255
+ }),
256
+ [(0, import_shared_utils.objectToDeps)(variantProps), isClearable, disableAnimation]
257
+ );
258
+ const getBaseProps = () => ({
259
+ "data-invalid": (0, import_shared_utils.dataAttr)(isInvalid),
260
+ "data-open": (0, import_shared_utils.dataAttr)(state.isOpen),
261
+ className: slots.base({ class: baseStyles })
262
+ });
263
+ const getSelectorButtonProps = () => {
264
+ var _a2;
265
+ return {
266
+ ref: buttonRef,
267
+ ...(0, import_utils.mergeProps)(buttonProps, slotsProps.selectorButtonProps),
268
+ "data-open": (0, import_shared_utils.dataAttr)(state.isOpen),
269
+ className: slots.selectorButton({
270
+ class: (0, import_shared_utils.clsx)(classNames == null ? void 0 : classNames.selectorButton, (_a2 = slotsProps.selectorButtonProps) == null ? void 0 : _a2.className)
271
+ })
272
+ };
273
+ };
274
+ const getClearButtonProps = () => {
275
+ var _a2, _b2;
276
+ return {
277
+ ...(0, import_utils.mergeProps)(buttonProps, slotsProps.clearButtonProps),
278
+ // disable original focus and state toggle from react aria
279
+ onPressStart: () => {
280
+ var _a3;
281
+ (_a3 = inputRef.current) == null ? void 0 : _a3.focus();
282
+ },
283
+ onPress: (e) => {
284
+ var _a3, _b3;
285
+ (_b3 = (_a3 = slotsProps.clearButtonProps) == null ? void 0 : _a3.onPress) == null ? void 0 : _b3.call(_a3, e);
286
+ if (state.selectedItem) {
287
+ state.setSelectedKey(null);
288
+ }
289
+ state.setInputValue("");
290
+ state.open();
291
+ },
292
+ "data-visible": !!state.selectedItem || ((_a2 = state.inputValue) == null ? void 0 : _a2.length) > 0,
293
+ className: slots.clearButton({
294
+ class: (0, import_shared_utils.clsx)(classNames == null ? void 0 : classNames.clearButton, (_b2 = slotsProps.clearButtonProps) == null ? void 0 : _b2.className)
295
+ })
296
+ };
297
+ };
298
+ const hasUncommittedValidation = validationBehavior === "native" && state.displayValidation.isInvalid === false && state.realtimeValidation.isInvalid === true;
299
+ const getInputProps = () => ({
300
+ ...otherProps,
301
+ ...inputProps,
302
+ ...slotsProps.inputProps,
303
+ isInvalid: hasUncommittedValidation ? void 0 : isInvalid,
304
+ validationBehavior,
305
+ errorMessage: typeof errorMessage === "function" ? errorMessage({ isInvalid, validationErrors, validationDetails }) : errorMessage || (validationErrors == null ? void 0 : validationErrors.join(" ")),
306
+ onClick: (0, import_utils.chain)(slotsProps.inputProps.onClick, otherProps.onClick)
307
+ });
308
+ const getListBoxProps = () => {
309
+ const shouldVirtualize = isVirtualized != null ? isVirtualized : state.collection.size > 50;
310
+ return {
311
+ state,
312
+ ref: listBoxRef,
313
+ isVirtualized: shouldVirtualize,
314
+ virtualization: shouldVirtualize ? {
315
+ maxListboxHeight,
316
+ itemHeight
317
+ } : void 0,
318
+ scrollShadowProps: slotsProps.scrollShadowProps,
319
+ ...(0, import_utils.mergeProps)(slotsProps.listboxProps, listBoxProps, {
320
+ shouldHighlightOnFocus: true
321
+ })
322
+ };
323
+ };
324
+ const getPopoverProps = (props2 = {}) => {
325
+ var _a2, _b2, _c2;
326
+ const popoverProps2 = (0, import_utils.mergeProps)(slotsProps.popoverProps, props2);
327
+ return {
328
+ state,
329
+ ref: popoverRef,
330
+ triggerRef: inputWrapperRef,
331
+ scrollRef: listBoxRef,
332
+ triggerType: "listbox",
333
+ ...popoverProps2,
334
+ classNames: {
335
+ ...(_a2 = slotsProps.popoverProps) == null ? void 0 : _a2.classNames,
336
+ content: slots.popoverContent({
337
+ class: (0, import_shared_utils.clsx)(
338
+ classNames == null ? void 0 : classNames.popoverContent,
339
+ (_c2 = (_b2 = slotsProps.popoverProps) == null ? void 0 : _b2.classNames) == null ? void 0 : _c2["content"],
340
+ props2.className
341
+ )
342
+ })
343
+ },
344
+ shouldCloseOnInteractOutside: (popoverProps2 == null ? void 0 : popoverProps2.shouldCloseOnInteractOutside) ? popoverProps2.shouldCloseOnInteractOutside : (element) => (0, import_aria_utils.ariaShouldCloseOnInteractOutside)(element, inputWrapperRef, state),
345
+ // when the popover is open, the focus should be on input instead of dialog
346
+ // therefore, we skip dialog focus here
347
+ disableDialogFocus: true
348
+ };
349
+ };
350
+ const getEmptyPopoverProps = () => {
351
+ return {
352
+ ref: popoverRef,
353
+ className: "hidden"
354
+ };
355
+ };
356
+ const getListBoxWrapperProps = (props2 = {}) => {
357
+ var _a2, _b2;
358
+ return {
359
+ ...(0, import_utils.mergeProps)(slotsProps.scrollShadowProps, props2),
360
+ className: slots.listboxWrapper({
361
+ class: (0, import_shared_utils.clsx)(
362
+ classNames == null ? void 0 : classNames.listboxWrapper,
363
+ (_a2 = slotsProps.scrollShadowProps) == null ? void 0 : _a2.className,
364
+ props2 == null ? void 0 : props2.className
365
+ )
366
+ }),
367
+ style: {
368
+ maxHeight: (_b2 = originalProps.maxListboxHeight) != null ? _b2 : 256
369
+ }
370
+ };
371
+ };
372
+ const getEndContentWrapperProps = (props2 = {}) => ({
373
+ className: slots.endContentWrapper({
374
+ class: (0, import_shared_utils.clsx)(classNames == null ? void 0 : classNames.endContentWrapper, props2 == null ? void 0 : props2.className)
375
+ }),
376
+ onPointerDown: (0, import_utils.chain)(props2.onPointerDown, (e) => {
377
+ var _a2;
378
+ if (e.button === 0 && e.currentTarget === e.target) {
379
+ (_a2 = inputRef.current) == null ? void 0 : _a2.focus();
380
+ }
381
+ }),
382
+ onMouseDown: (0, import_utils.chain)(props2.onMouseDown, (e) => {
383
+ if (e.button === 0 && e.currentTarget === e.target) {
384
+ e.preventDefault();
385
+ }
386
+ })
387
+ });
388
+ return {
389
+ Component,
390
+ inputRef,
391
+ label,
392
+ state,
393
+ slots,
394
+ classNames,
395
+ isLoading,
396
+ clearIcon,
397
+ isOpen,
398
+ endContent,
399
+ isClearable,
400
+ disableAnimation,
401
+ allowsCustomValue,
402
+ selectorIcon,
403
+ getBaseProps,
404
+ getInputProps,
405
+ getListBoxProps,
406
+ getPopoverProps,
407
+ getEmptyPopoverProps,
408
+ getClearButtonProps,
409
+ getSelectorButtonProps,
410
+ getListBoxWrapperProps,
411
+ getEndContentWrapperProps
412
+ };
413
+ }
414
+
415
+ // src/autocomplete.tsx
416
+ var import_jsx_runtime = require("react/jsx-runtime");
417
+ var Autocomplete = (0, import_system2.forwardRef)(function Autocomplete2(props, ref) {
418
+ var _a;
419
+ const {
420
+ Component,
421
+ isOpen,
422
+ disableAnimation,
423
+ selectorIcon = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_shared_icons.ChevronDownIcon, {}),
424
+ clearIcon = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_shared_icons.CloseIcon, {}),
425
+ endContent,
426
+ getBaseProps,
427
+ getSelectorButtonProps,
428
+ getInputProps,
429
+ getListBoxProps,
430
+ getPopoverProps,
431
+ getEmptyPopoverProps,
432
+ getClearButtonProps,
433
+ getListBoxWrapperProps,
434
+ getEndContentWrapperProps
435
+ } = useAutocomplete({ ...props, ref });
436
+ const listboxProps = getListBoxProps();
437
+ const popoverContent = isOpen ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_popover.FreeSoloPopover, { ...getPopoverProps(), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_scroll_shadow.ScrollShadow, { ...getListBoxWrapperProps(), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_listbox.Listbox, { ...listboxProps }) }) }) : ((_a = listboxProps.state) == null ? void 0 : _a.collection.size) === 0 ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...getEmptyPopoverProps() }) : null;
438
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Component, { ...getBaseProps(), children: [
439
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
440
+ import_input.Input,
441
+ {
442
+ ...getInputProps(),
443
+ endContent: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { ...getEndContentWrapperProps(), children: [
444
+ endContent || /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.Button, { ...getClearButtonProps(), children: clearIcon }),
445
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.Button, { ...getSelectorButtonProps(), children: selectorIcon })
446
+ ] })
447
+ }
448
+ ),
449
+ disableAnimation ? popoverContent : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_framer_motion.AnimatePresence, { children: popoverContent })
450
+ ] });
451
+ });
452
+ var autocomplete_default = Autocomplete;
453
+ // Annotate the CommonJS export names for ESM import in node:
454
+ 0 && (module.exports = {
455
+ Autocomplete,
456
+ AutocompleteItem,
457
+ AutocompleteSection,
458
+ useAutocomplete
459
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,16 @@
1
+ "use client";
2
+ import {
3
+ autocomplete_default
4
+ } from "./chunk-DNIGSRME.mjs";
5
+ import {
6
+ useAutocomplete
7
+ } from "./chunk-OHYOYGT2.mjs";
8
+
9
+ // src/index.ts
10
+ import { ListboxItem, ListboxSection } from "@heroui/listbox";
11
+ export {
12
+ autocomplete_default as Autocomplete,
13
+ ListboxItem as AutocompleteItem,
14
+ ListboxSection as AutocompleteSection,
15
+ useAutocomplete
16
+ };