@heroui/autocomplete 2.3.11 → 2.3.12

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,377 +0,0 @@
1
- "use client";
2
-
3
- // src/use-autocomplete.ts
4
- import { mapPropsVariants, useProviderContext } from "@heroui/system";
5
- import { useSafeLayoutEffect } from "@heroui/use-safe-layout-effect";
6
- import { autocomplete } from "@heroui/theme";
7
- import { useFilter } from "@react-aria/i18n";
8
- import { useComboBoxState } from "@react-stately/combobox";
9
- import { useDOMRef } from "@heroui/react-utils";
10
- import { useEffect, useMemo, useRef } from "react";
11
- import { clsx, dataAttr, objectToDeps } from "@heroui/shared-utils";
12
- import { chain, mergeProps } from "@react-aria/utils";
13
- import { useComboBox } from "@react-aria/combobox";
14
- import { FormContext, useSlottedContext } from "@heroui/form";
15
- import { ariaShouldCloseOnInteractOutside } from "@heroui/aria-utils";
16
- function useAutocomplete(originalProps) {
17
- var _a, _b, _c, _d, _e;
18
- const globalContext = useProviderContext();
19
- const { validationBehavior: formValidationBehavior } = useSlottedContext(FormContext) || {};
20
- const [props, variantProps] = mapPropsVariants(originalProps, autocomplete.variantKeys);
21
- const disableAnimation = (_b = (_a = originalProps.disableAnimation) != null ? _a : globalContext == null ? void 0 : globalContext.disableAnimation) != null ? _b : false;
22
- const isClearable = originalProps.disableClearable !== void 0 ? !originalProps.disableClearable : originalProps.isReadOnly ? false : originalProps.isClearable;
23
- const {
24
- ref,
25
- as,
26
- label,
27
- isLoading,
28
- menuTrigger = "focus",
29
- filterOptions = {
30
- sensitivity: "base"
31
- },
32
- children,
33
- selectorIcon,
34
- clearIcon,
35
- scrollRef: scrollRefProp,
36
- defaultFilter,
37
- endContent,
38
- allowsEmptyCollection = true,
39
- shouldCloseOnBlur = true,
40
- popoverProps = {},
41
- inputProps: userInputProps = {},
42
- scrollShadowProps = {},
43
- listboxProps = {},
44
- selectorButtonProps = {},
45
- clearButtonProps = {},
46
- showScrollIndicators = true,
47
- allowsCustomValue = false,
48
- isVirtualized,
49
- maxListboxHeight = 256,
50
- itemHeight = 32,
51
- validationBehavior = (_c = formValidationBehavior != null ? formValidationBehavior : globalContext == null ? void 0 : globalContext.validationBehavior) != null ? _c : "native",
52
- className,
53
- classNames,
54
- errorMessage,
55
- onOpenChange,
56
- onClose,
57
- isReadOnly = false,
58
- ...otherProps
59
- } = props;
60
- const { contains } = useFilter(filterOptions);
61
- let state = useComboBoxState({
62
- ...originalProps,
63
- children,
64
- menuTrigger,
65
- validationBehavior,
66
- shouldCloseOnBlur,
67
- allowsEmptyCollection,
68
- defaultFilter: defaultFilter && typeof defaultFilter === "function" ? defaultFilter : contains,
69
- onOpenChange: (open, menuTrigger2) => {
70
- onOpenChange == null ? void 0 : onOpenChange(open, menuTrigger2);
71
- if (!open) {
72
- onClose == null ? void 0 : onClose();
73
- }
74
- }
75
- });
76
- state = {
77
- ...state,
78
- ...isReadOnly && {
79
- disabledKeys: /* @__PURE__ */ new Set([...state.collection.getKeys()])
80
- }
81
- };
82
- const buttonRef = useRef(null);
83
- const inputWrapperRef = useRef(null);
84
- const listBoxRef = useRef(null);
85
- const popoverRef = useRef(null);
86
- const inputRef = useDOMRef(ref);
87
- const scrollShadowRef = useDOMRef(scrollRefProp);
88
- const {
89
- buttonProps,
90
- inputProps,
91
- listBoxProps,
92
- isInvalid: isAriaInvalid,
93
- validationDetails,
94
- validationErrors
95
- } = useComboBox(
96
- {
97
- validationBehavior,
98
- ...originalProps,
99
- inputRef,
100
- buttonRef,
101
- listBoxRef,
102
- popoverRef
103
- },
104
- state
105
- );
106
- const isInvalid = originalProps.isInvalid || isAriaInvalid;
107
- const slotsProps = {
108
- inputProps: mergeProps(
109
- {
110
- label,
111
- ref: inputRef,
112
- wrapperRef: inputWrapperRef,
113
- onClick: () => {
114
- if (!state.isOpen && !!state.selectedItem) {
115
- state.open();
116
- }
117
- },
118
- isClearable: false,
119
- disableAnimation
120
- },
121
- userInputProps
122
- ),
123
- popoverProps: mergeProps(
124
- {
125
- offset: 5,
126
- placement: "bottom",
127
- triggerScaleOnOpen: false,
128
- disableAnimation
129
- },
130
- popoverProps
131
- ),
132
- scrollShadowProps: mergeProps(
133
- {
134
- ref: scrollShadowRef,
135
- isEnabled: (_d = showScrollIndicators && state.collection.size > 5) != null ? _d : true,
136
- hideScrollBar: true,
137
- offset: 15
138
- },
139
- scrollShadowProps
140
- ),
141
- listboxProps: mergeProps(
142
- {
143
- hideEmptyContent: allowsCustomValue,
144
- emptyContent: "No results found.",
145
- disableAnimation
146
- },
147
- listboxProps
148
- ),
149
- selectorButtonProps: mergeProps(
150
- {
151
- isLoading,
152
- size: "sm",
153
- variant: "light",
154
- radius: "full",
155
- color: isInvalid ? "danger" : originalProps == null ? void 0 : originalProps.color,
156
- isIconOnly: true,
157
- disableAnimation
158
- },
159
- selectorButtonProps
160
- ),
161
- clearButtonProps: mergeProps(
162
- {
163
- size: "sm",
164
- variant: "light",
165
- radius: "full",
166
- color: isInvalid ? "danger" : originalProps == null ? void 0 : originalProps.color,
167
- isIconOnly: true,
168
- disableAnimation
169
- },
170
- clearButtonProps
171
- )
172
- };
173
- const baseStyles = clsx(classNames == null ? void 0 : classNames.base, className);
174
- const isOpen = ((_e = slotsProps.listboxProps) == null ? void 0 : _e.hideEmptyContent) ? state.isOpen && !!state.collection.size : state.isOpen;
175
- useSafeLayoutEffect(() => {
176
- if (!inputRef.current)
177
- return;
178
- const key = inputRef.current.value;
179
- const item = state.collection.getItem(key);
180
- if (item && state.inputValue !== item.textValue) {
181
- state.setSelectedKey(key);
182
- state.setInputValue(item.textValue);
183
- }
184
- }, [inputRef.current]);
185
- useEffect(() => {
186
- let key = state.collection.getFirstKey();
187
- while (key && state.disabledKeys.has(key)) {
188
- key = state.collection.getKeyAfter(key);
189
- }
190
- state.selectionManager.setFocusedKey(key);
191
- }, [state.collection, state.disabledKeys]);
192
- useEffect(() => {
193
- if (isOpen) {
194
- if (popoverRef.current && inputWrapperRef.current) {
195
- let rect = inputWrapperRef.current.getBoundingClientRect();
196
- let popover = popoverRef.current;
197
- popover.style.width = rect.width + "px";
198
- }
199
- }
200
- }, [isOpen]);
201
- if (inputProps.onKeyDown) {
202
- const originalOnKeyDown = inputProps.onKeyDown;
203
- inputProps.onKeyDown = (e) => {
204
- if ("continuePropagation" in e) {
205
- e.stopPropagation = () => {
206
- };
207
- }
208
- return originalOnKeyDown(e);
209
- };
210
- }
211
- const Component = as || "div";
212
- const slots = useMemo(
213
- () => autocomplete({
214
- ...variantProps,
215
- isClearable,
216
- disableAnimation,
217
- className
218
- }),
219
- [objectToDeps(variantProps), isClearable, disableAnimation, className]
220
- );
221
- const getBaseProps = () => ({
222
- "data-invalid": dataAttr(isInvalid),
223
- "data-open": dataAttr(state.isOpen),
224
- className: slots.base({ class: baseStyles })
225
- });
226
- const getSelectorButtonProps = () => {
227
- var _a2;
228
- return {
229
- ref: buttonRef,
230
- ...mergeProps(buttonProps, slotsProps.selectorButtonProps),
231
- "data-open": dataAttr(state.isOpen),
232
- className: slots.selectorButton({
233
- class: clsx(classNames == null ? void 0 : classNames.selectorButton, (_a2 = slotsProps.selectorButtonProps) == null ? void 0 : _a2.className)
234
- })
235
- };
236
- };
237
- const getClearButtonProps = () => {
238
- var _a2, _b2;
239
- return {
240
- ...mergeProps(buttonProps, slotsProps.clearButtonProps),
241
- onPressStart: () => {
242
- var _a3;
243
- (_a3 = inputRef.current) == null ? void 0 : _a3.focus();
244
- },
245
- onPress: (e) => {
246
- var _a3, _b3;
247
- (_b3 = (_a3 = slotsProps.clearButtonProps) == null ? void 0 : _a3.onPress) == null ? void 0 : _b3.call(_a3, e);
248
- if (state.selectedItem) {
249
- state.setSelectedKey(null);
250
- }
251
- state.setInputValue("");
252
- state.open();
253
- },
254
- "data-visible": !!state.selectedItem || ((_a2 = state.inputValue) == null ? void 0 : _a2.length) > 0,
255
- className: slots.clearButton({
256
- class: clsx(classNames == null ? void 0 : classNames.clearButton, (_b2 = slotsProps.clearButtonProps) == null ? void 0 : _b2.className)
257
- })
258
- };
259
- };
260
- const hasUncommittedValidation = validationBehavior === "native" && state.displayValidation.isInvalid === false && state.realtimeValidation.isInvalid === true;
261
- const getInputProps = () => ({
262
- ...otherProps,
263
- ...inputProps,
264
- ...slotsProps.inputProps,
265
- isInvalid: hasUncommittedValidation ? void 0 : isInvalid,
266
- validationBehavior,
267
- errorMessage: typeof errorMessage === "function" ? errorMessage({ isInvalid, validationErrors, validationDetails }) : errorMessage || (validationErrors == null ? void 0 : validationErrors.join(" ")),
268
- onClick: chain(slotsProps.inputProps.onClick, otherProps.onClick)
269
- });
270
- const getListBoxProps = () => {
271
- const shouldVirtualize = isVirtualized != null ? isVirtualized : state.collection.size > 50;
272
- return {
273
- state,
274
- ref: listBoxRef,
275
- isVirtualized: shouldVirtualize,
276
- virtualization: shouldVirtualize ? {
277
- maxListboxHeight,
278
- itemHeight
279
- } : void 0,
280
- scrollShadowProps: slotsProps.scrollShadowProps,
281
- ...mergeProps(slotsProps.listboxProps, listBoxProps, {
282
- shouldHighlightOnFocus: true
283
- })
284
- };
285
- };
286
- const getPopoverProps = (props2 = {}) => {
287
- var _a2, _b2, _c2;
288
- const popoverProps2 = mergeProps(slotsProps.popoverProps, props2);
289
- return {
290
- state,
291
- ref: popoverRef,
292
- triggerRef: inputWrapperRef,
293
- scrollRef: listBoxRef,
294
- triggerType: "listbox",
295
- ...popoverProps2,
296
- classNames: {
297
- ...(_a2 = slotsProps.popoverProps) == null ? void 0 : _a2.classNames,
298
- content: slots.popoverContent({
299
- class: clsx(
300
- classNames == null ? void 0 : classNames.popoverContent,
301
- (_c2 = (_b2 = slotsProps.popoverProps) == null ? void 0 : _b2.classNames) == null ? void 0 : _c2["content"],
302
- props2.className
303
- )
304
- })
305
- },
306
- shouldCloseOnInteractOutside: (popoverProps2 == null ? void 0 : popoverProps2.shouldCloseOnInteractOutside) ? popoverProps2.shouldCloseOnInteractOutside : (element) => ariaShouldCloseOnInteractOutside(element, inputWrapperRef, state),
307
- disableDialogFocus: true
308
- };
309
- };
310
- const getEmptyPopoverProps = () => {
311
- return {
312
- ref: popoverRef,
313
- className: "hidden"
314
- };
315
- };
316
- const getListBoxWrapperProps = (props2 = {}) => {
317
- var _a2, _b2;
318
- return {
319
- ...mergeProps(slotsProps.scrollShadowProps, props2),
320
- className: slots.listboxWrapper({
321
- class: clsx(
322
- classNames == null ? void 0 : classNames.listboxWrapper,
323
- (_a2 = slotsProps.scrollShadowProps) == null ? void 0 : _a2.className,
324
- props2 == null ? void 0 : props2.className
325
- )
326
- }),
327
- style: {
328
- maxHeight: (_b2 = originalProps.maxListboxHeight) != null ? _b2 : 256
329
- }
330
- };
331
- };
332
- const getEndContentWrapperProps = (props2 = {}) => ({
333
- className: slots.endContentWrapper({
334
- class: clsx(classNames == null ? void 0 : classNames.endContentWrapper, props2 == null ? void 0 : props2.className)
335
- }),
336
- onPointerDown: chain(props2.onPointerDown, (e) => {
337
- var _a2;
338
- if (e.button === 0 && e.currentTarget === e.target) {
339
- (_a2 = inputRef.current) == null ? void 0 : _a2.focus();
340
- }
341
- }),
342
- onMouseDown: chain(props2.onMouseDown, (e) => {
343
- if (e.button === 0 && e.currentTarget === e.target) {
344
- e.preventDefault();
345
- }
346
- })
347
- });
348
- return {
349
- Component,
350
- inputRef,
351
- label,
352
- state,
353
- slots,
354
- classNames,
355
- isLoading,
356
- clearIcon,
357
- isOpen,
358
- endContent,
359
- isClearable,
360
- disableAnimation,
361
- allowsCustomValue,
362
- selectorIcon,
363
- getBaseProps,
364
- getInputProps,
365
- getListBoxProps,
366
- getPopoverProps,
367
- getEmptyPopoverProps,
368
- getClearButtonProps,
369
- getSelectorButtonProps,
370
- getListBoxWrapperProps,
371
- getEndContentWrapperProps
372
- };
373
- }
374
-
375
- export {
376
- useAutocomplete
377
- };
@@ -1,55 +0,0 @@
1
- "use client";
2
- import {
3
- useAutocomplete
4
- } from "./chunk-QZCVHGAT.mjs";
5
-
6
- // src/autocomplete.tsx
7
- import { forwardRef } from "@heroui/system";
8
- import { FreeSoloPopover } from "@heroui/popover";
9
- import { ScrollShadow } from "@heroui/scroll-shadow";
10
- import { ChevronDownIcon, CloseIcon } from "@heroui/shared-icons";
11
- import { Listbox } from "@heroui/listbox";
12
- import { Button } from "@heroui/button";
13
- import { Input } from "@heroui/input";
14
- import { AnimatePresence } from "framer-motion";
15
- import { jsx, jsxs } from "react/jsx-runtime";
16
- var Autocomplete = forwardRef(function Autocomplete2(props, ref) {
17
- var _a;
18
- const {
19
- Component,
20
- isOpen,
21
- disableAnimation,
22
- selectorIcon = /* @__PURE__ */ jsx(ChevronDownIcon, {}),
23
- clearIcon = /* @__PURE__ */ jsx(CloseIcon, {}),
24
- endContent,
25
- getBaseProps,
26
- getSelectorButtonProps,
27
- getInputProps,
28
- getListBoxProps,
29
- getPopoverProps,
30
- getEmptyPopoverProps,
31
- getClearButtonProps,
32
- getListBoxWrapperProps,
33
- getEndContentWrapperProps
34
- } = useAutocomplete({ ...props, ref });
35
- const listboxProps = getListBoxProps();
36
- const popoverContent = isOpen ? /* @__PURE__ */ jsx(FreeSoloPopover, { ...getPopoverProps(), children: /* @__PURE__ */ jsx(ScrollShadow, { ...getListBoxWrapperProps(), children: /* @__PURE__ */ jsx(Listbox, { ...listboxProps }) }) }) : ((_a = listboxProps.state) == null ? void 0 : _a.collection.size) === 0 ? /* @__PURE__ */ jsx("div", { ...getEmptyPopoverProps() }) : null;
37
- return /* @__PURE__ */ jsxs(Component, { ...getBaseProps(), children: [
38
- /* @__PURE__ */ jsx(
39
- Input,
40
- {
41
- ...getInputProps(),
42
- endContent: /* @__PURE__ */ jsxs("div", { ...getEndContentWrapperProps(), children: [
43
- endContent || /* @__PURE__ */ jsx(Button, { ...getClearButtonProps(), children: clearIcon }),
44
- /* @__PURE__ */ jsx(Button, { ...getSelectorButtonProps(), children: selectorIcon })
45
- ] })
46
- }
47
- ),
48
- disableAnimation ? popoverContent : /* @__PURE__ */ jsx(AnimatePresence, { children: popoverContent })
49
- ] });
50
- });
51
- var autocomplete_default = Autocomplete;
52
-
53
- export {
54
- autocomplete_default
55
- };
package/dist/index.d.ts DELETED
@@ -1,19 +0,0 @@
1
- export { ListboxItem as AutocompleteItem, ListboxItemProps as AutocompleteItemProps, ListboxSection as AutocompleteSection, ListboxSectionProps as AutocompleteSectionProps } from '@heroui/listbox';
2
- import { MenuTriggerAction as MenuTriggerAction$1 } from '@react-types/combobox';
3
- export { default as Autocomplete, AutocompleteProps } from './autocomplete.js';
4
- export { useAutocomplete } from './use-autocomplete.js';
5
- import 'react';
6
- import 'tailwind-variants';
7
- import '@react-stately/combobox';
8
- import '@heroui/system';
9
- import '@heroui/theme';
10
- import '@heroui/react-utils';
11
- import '@heroui/popover';
12
- import '@heroui/input';
13
- import '@heroui/scroll-shadow';
14
- import '@heroui/button';
15
- import '@react-types/shared';
16
-
17
- type MenuTriggerAction = MenuTriggerAction$1 | undefined;
18
-
19
- export { MenuTriggerAction };