@tecsinapse/cortex-react 2.2.0-beta.4 → 2.2.0-beta.6
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/cjs/components/Autocomplete/GroupedOptions.js +32 -0
- package/dist/cjs/components/Autocomplete/Option.js +38 -0
- package/dist/cjs/components/Autocomplete/Options.js +31 -0
- package/dist/cjs/components/Autocomplete/Popover.js +34 -0
- package/dist/cjs/components/Autocomplete/Root.js +34 -0
- package/dist/cjs/components/Autocomplete/Trigger.js +48 -0
- package/dist/cjs/components/Autocomplete/context.js +9 -0
- package/dist/cjs/components/Autocomplete/index.js +19 -0
- package/dist/cjs/hooks/useAutocompleteGroupedOptions.js +35 -0
- package/dist/cjs/hooks/useAutocompleteOptions.js +35 -0
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/provider/ManagerContext.js +3 -0
- package/dist/esm/components/Autocomplete/GroupedOptions.js +30 -0
- package/dist/esm/components/Autocomplete/Option.js +36 -0
- package/dist/esm/components/Autocomplete/Options.js +29 -0
- package/dist/esm/components/Autocomplete/Popover.js +32 -0
- package/dist/esm/components/Autocomplete/Root.js +32 -0
- package/dist/esm/components/Autocomplete/Trigger.js +46 -0
- package/dist/esm/components/Autocomplete/context.js +7 -0
- package/dist/esm/components/Autocomplete/index.js +17 -0
- package/dist/esm/hooks/useAutocompleteGroupedOptions.js +33 -0
- package/dist/esm/hooks/useAutocompleteOptions.js +33 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/provider/ManagerContext.js +3 -0
- package/dist/types/components/Autocomplete/GroupedOptions.d.ts +2 -0
- package/dist/types/components/Autocomplete/Option.d.ts +2 -0
- package/dist/types/components/Autocomplete/Options.d.ts +2 -0
- package/dist/types/components/Autocomplete/Popover.d.ts +2 -0
- package/dist/types/components/Autocomplete/Root.d.ts +2 -0
- package/dist/types/components/Autocomplete/Trigger.d.ts +2 -0
- package/dist/types/components/Autocomplete/context.d.ts +9 -0
- package/dist/types/components/Autocomplete/index.d.ts +9 -1
- package/dist/types/components/Autocomplete/types.d.ts +46 -0
- package/dist/types/hooks/useAutocompleteGroupedOptions.d.ts +10 -0
- package/dist/types/hooks/useAutocompleteOptions.d.ts +10 -0
- package/package.json +2 -2
- package/dist/cjs/components/Autocomplete/Autocomplete.js +0 -85
- package/dist/esm/components/Autocomplete/Autocomplete.js +0 -83
- package/dist/types/components/Autocomplete/Autocomplete.d.ts +0 -17
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var cortexCore = require('@tecsinapse/cortex-core');
|
|
5
|
+
var Option = require('./Option.js');
|
|
6
|
+
var useAutocompleteGroupedOptions = require('../../hooks/useAutocompleteGroupedOptions.js');
|
|
7
|
+
var SkeletonOptions = require('../Select/SkeletonOptions.js');
|
|
8
|
+
|
|
9
|
+
const { groupedTitle, list } = cortexCore.selectVariants();
|
|
10
|
+
const AutocompleteGroupedOptions = ({
|
|
11
|
+
onSelect,
|
|
12
|
+
groupedLabelExtractor,
|
|
13
|
+
options
|
|
14
|
+
}) => {
|
|
15
|
+
const { options: _options, isLoading } = useAutocompleteGroupedOptions.useAutocompleteGroupedOptions({
|
|
16
|
+
options
|
|
17
|
+
});
|
|
18
|
+
return isLoading ? /* @__PURE__ */ jsxRuntime.jsx(SkeletonOptions.SkeletonOptions, {}) : /* @__PURE__ */ jsxRuntime.jsx("ul", { role: "listbox", className: list(), children: [..._options ?? []].map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
19
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: groupedTitle(), children: groupedLabelExtractor?.(key) }),
|
|
20
|
+
value.map((option) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
21
|
+
Option.AutocompleteOption,
|
|
22
|
+
{
|
|
23
|
+
grouped: true,
|
|
24
|
+
option,
|
|
25
|
+
onSelect
|
|
26
|
+
},
|
|
27
|
+
option.value
|
|
28
|
+
))
|
|
29
|
+
] }, key)) });
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
exports.AutocompleteGroupedOptions = AutocompleteGroupedOptions;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var cortexCore = require('@tecsinapse/cortex-core');
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var context = require('./context.js');
|
|
7
|
+
|
|
8
|
+
const AutocompleteOption = ({
|
|
9
|
+
option,
|
|
10
|
+
onSelect,
|
|
11
|
+
grouped = false
|
|
12
|
+
}) => {
|
|
13
|
+
const context$1 = React.useContext(context.AutocompleteContext);
|
|
14
|
+
if (!context$1)
|
|
15
|
+
throw new Error("AutocompleteOptions must be used within AutocompleteRoot");
|
|
16
|
+
const { setOpen } = context$1;
|
|
17
|
+
const handleSelect = React.useCallback(
|
|
18
|
+
(option2) => {
|
|
19
|
+
setOpen(false);
|
|
20
|
+
onSelect?.(option2);
|
|
21
|
+
},
|
|
22
|
+
[onSelect, setOpen]
|
|
23
|
+
);
|
|
24
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
25
|
+
"li",
|
|
26
|
+
{
|
|
27
|
+
onClick: () => handleSelect(option),
|
|
28
|
+
className: cortexCore.option({
|
|
29
|
+
selected: false,
|
|
30
|
+
grouped
|
|
31
|
+
}),
|
|
32
|
+
role: "option",
|
|
33
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "truncate flex-1", children: option.label })
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.AutocompleteOption = AutocompleteOption;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var cortexCore = require('@tecsinapse/cortex-core');
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var Option = require('./Option.js');
|
|
7
|
+
var useAutocompleteOptions = require('../../hooks/useAutocompleteOptions.js');
|
|
8
|
+
var SkeletonOptions = require('../Select/SkeletonOptions.js');
|
|
9
|
+
|
|
10
|
+
const { list } = cortexCore.selectVariants();
|
|
11
|
+
const AutocompleteOptions = ({
|
|
12
|
+
options,
|
|
13
|
+
onSelect,
|
|
14
|
+
children
|
|
15
|
+
}) => {
|
|
16
|
+
const { options: _options, isLoading } = useAutocompleteOptions.useAutocompleteOptions({ options });
|
|
17
|
+
const defaultOptions = React.useMemo(
|
|
18
|
+
() => _options?.map((option) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
19
|
+
Option.AutocompleteOption,
|
|
20
|
+
{
|
|
21
|
+
option,
|
|
22
|
+
onSelect
|
|
23
|
+
},
|
|
24
|
+
option.value
|
|
25
|
+
)) ?? [],
|
|
26
|
+
[_options, onSelect]
|
|
27
|
+
);
|
|
28
|
+
return isLoading ? /* @__PURE__ */ jsxRuntime.jsx(SkeletonOptions.SkeletonOptions, {}) : /* @__PURE__ */ jsxRuntime.jsx("ul", { role: "listbox", className: list(), children: children ?? defaultOptions });
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
exports.AutocompleteOptions = AutocompleteOptions;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var react = require('@floating-ui/react');
|
|
6
|
+
var index = require('../Popover/index.js');
|
|
7
|
+
var context = require('./context.js');
|
|
8
|
+
var clsx = require('clsx');
|
|
9
|
+
|
|
10
|
+
const AutocompletePopover = ({
|
|
11
|
+
children,
|
|
12
|
+
className
|
|
13
|
+
}) => {
|
|
14
|
+
const context$1 = React.useContext(context.AutocompleteContext);
|
|
15
|
+
if (!context$1)
|
|
16
|
+
throw new Error("AutocompletePopover must be used within AutocompleteRoot");
|
|
17
|
+
const { triggerWidth } = context$1;
|
|
18
|
+
return /* @__PURE__ */ jsxRuntime.jsx(react.FloatingPortal, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
19
|
+
index.Popover.Content,
|
|
20
|
+
{
|
|
21
|
+
className: clsx(
|
|
22
|
+
"bg-white shadow-md rounded-md overflow-y-auto max-h-[30vh] outline-none z-9999",
|
|
23
|
+
className
|
|
24
|
+
),
|
|
25
|
+
style: {
|
|
26
|
+
width: triggerWidth ? `${triggerWidth}px` : "auto"
|
|
27
|
+
},
|
|
28
|
+
initialFocus: -1,
|
|
29
|
+
children
|
|
30
|
+
}
|
|
31
|
+
) });
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
exports.AutocompletePopover = AutocompletePopover;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var index = require('../Popover/index.js');
|
|
6
|
+
var context = require('./context.js');
|
|
7
|
+
|
|
8
|
+
const AutocompleteRoot = ({ children }) => {
|
|
9
|
+
const [triggerWidth, setTriggerWidth] = React.useState();
|
|
10
|
+
const [open, setOpen] = React.useState(false);
|
|
11
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
12
|
+
context.AutocompleteContext.Provider,
|
|
13
|
+
{
|
|
14
|
+
value: {
|
|
15
|
+
open,
|
|
16
|
+
setOpen,
|
|
17
|
+
triggerWidth,
|
|
18
|
+
setTriggerWidth
|
|
19
|
+
},
|
|
20
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
21
|
+
index.Popover.Root,
|
|
22
|
+
{
|
|
23
|
+
placement: "bottom-start",
|
|
24
|
+
controlled: true,
|
|
25
|
+
isOpen: open,
|
|
26
|
+
setIsOpen: setOpen,
|
|
27
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "relative w-full h-full", children })
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
exports.AutocompleteRoot = AutocompleteRoot;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var index = require('../Popover/index.js');
|
|
6
|
+
var index$1 = require('../Input/index.js');
|
|
7
|
+
var context = require('./context.js');
|
|
8
|
+
|
|
9
|
+
const AutocompleteTrigger = ({
|
|
10
|
+
inputValue,
|
|
11
|
+
onChange,
|
|
12
|
+
label,
|
|
13
|
+
disabled,
|
|
14
|
+
placeholder
|
|
15
|
+
}) => {
|
|
16
|
+
const context$1 = React.useContext(context.AutocompleteContext);
|
|
17
|
+
if (!context$1)
|
|
18
|
+
throw new Error("AutocompleteTrigger must be used within AutocompleteRoot");
|
|
19
|
+
const { setTriggerWidth, setOpen } = context$1;
|
|
20
|
+
const triggerRef = React.useRef(null);
|
|
21
|
+
React.useEffect(() => {
|
|
22
|
+
if (triggerRef.current && setTriggerWidth) {
|
|
23
|
+
const width = triggerRef.current.getBoundingClientRect().width;
|
|
24
|
+
setTriggerWidth(width);
|
|
25
|
+
}
|
|
26
|
+
}, [triggerRef.current, setTriggerWidth]);
|
|
27
|
+
const handleChange = (event) => {
|
|
28
|
+
onChange(event);
|
|
29
|
+
if (event.target.value.length > 0) {
|
|
30
|
+
setOpen(true);
|
|
31
|
+
} else {
|
|
32
|
+
setOpen(false);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
return /* @__PURE__ */ jsxRuntime.jsx(index.Popover.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
36
|
+
index$1.Input.Root,
|
|
37
|
+
{
|
|
38
|
+
label: label ?? "Selecione uma op\xE7\xE3o",
|
|
39
|
+
value: inputValue,
|
|
40
|
+
onChange: handleChange,
|
|
41
|
+
disabled,
|
|
42
|
+
placeholder,
|
|
43
|
+
ref: triggerRef
|
|
44
|
+
}
|
|
45
|
+
) });
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
exports.AutocompleteTrigger = AutocompleteTrigger;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Root = require('./Root.js');
|
|
4
|
+
var Trigger = require('./Trigger.js');
|
|
5
|
+
var Popover = require('./Popover.js');
|
|
6
|
+
var Options = require('./Options.js');
|
|
7
|
+
var Option = require('./Option.js');
|
|
8
|
+
var GroupedOptions = require('./GroupedOptions.js');
|
|
9
|
+
|
|
10
|
+
const Autocomplete = {
|
|
11
|
+
Root: Root.AutocompleteRoot,
|
|
12
|
+
Trigger: Trigger.AutocompleteTrigger,
|
|
13
|
+
Popover: Popover.AutocompletePopover,
|
|
14
|
+
Options: Options.AutocompleteOptions,
|
|
15
|
+
Option: Option.AutocompleteOption,
|
|
16
|
+
GroupedOptions: GroupedOptions.AutocompleteGroupedOptions
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
exports.Autocomplete = Autocomplete;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
const useAutocompleteGroupedOptions = ({
|
|
6
|
+
options: _options
|
|
7
|
+
}) => {
|
|
8
|
+
const [options, setOptions] = React.useState();
|
|
9
|
+
const [isLoading, setIsLoading] = React.useState();
|
|
10
|
+
const [error, setError] = React.useState();
|
|
11
|
+
const initData = async (fetch) => {
|
|
12
|
+
setIsLoading(true);
|
|
13
|
+
try {
|
|
14
|
+
const result = await fetch();
|
|
15
|
+
if (result) {
|
|
16
|
+
setOptions(result ?? []);
|
|
17
|
+
}
|
|
18
|
+
} catch (e) {
|
|
19
|
+
setError(String(e));
|
|
20
|
+
} finally {
|
|
21
|
+
setIsLoading(false);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
React.useEffect(() => {
|
|
25
|
+
if (typeof _options === "function") initData(_options);
|
|
26
|
+
else setOptions(_options);
|
|
27
|
+
}, [_options]);
|
|
28
|
+
return {
|
|
29
|
+
isLoading,
|
|
30
|
+
options,
|
|
31
|
+
error
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
exports.useAutocompleteGroupedOptions = useAutocompleteGroupedOptions;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
const useAutocompleteOptions = ({
|
|
6
|
+
options: _options
|
|
7
|
+
}) => {
|
|
8
|
+
const [options, setOptions] = React.useState();
|
|
9
|
+
const [isLoading, setIsLoading] = React.useState();
|
|
10
|
+
const [error, setError] = React.useState();
|
|
11
|
+
const initData = React.useCallback(async (fetch) => {
|
|
12
|
+
setIsLoading(true);
|
|
13
|
+
try {
|
|
14
|
+
const result = await fetch();
|
|
15
|
+
if (result) {
|
|
16
|
+
setOptions(result ?? []);
|
|
17
|
+
}
|
|
18
|
+
} catch (e) {
|
|
19
|
+
setError(String(e));
|
|
20
|
+
} finally {
|
|
21
|
+
setIsLoading(false);
|
|
22
|
+
}
|
|
23
|
+
}, []);
|
|
24
|
+
React.useEffect(() => {
|
|
25
|
+
if (typeof _options === "function") initData(_options);
|
|
26
|
+
else setOptions(_options);
|
|
27
|
+
}, [_options]);
|
|
28
|
+
return {
|
|
29
|
+
isLoading,
|
|
30
|
+
options,
|
|
31
|
+
error
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
exports.useAutocompleteOptions = useAutocompleteOptions;
|
package/dist/cjs/index.js
CHANGED
|
@@ -44,7 +44,7 @@ var TimeFieldInput = require('./components/TimePicker/TimeFieldInput.js');
|
|
|
44
44
|
var Toggle = require('./components/Toggle.js');
|
|
45
45
|
var Tooltip = require('./components/Tooltip.js');
|
|
46
46
|
var index$7 = require('./components/Uploader/index.js');
|
|
47
|
-
var
|
|
47
|
+
var index$8 = require('./components/Autocomplete/index.js');
|
|
48
48
|
var useCalendar = require('./hooks/useCalendar.js');
|
|
49
49
|
var useCalendarCell = require('./hooks/useCalendarCell.js');
|
|
50
50
|
var useCalendarGrid = require('./hooks/useCalendarGrid.js');
|
|
@@ -128,7 +128,7 @@ exports.TimeFieldInput = TimeFieldInput.TimeFieldInput;
|
|
|
128
128
|
exports.Toggle = Toggle.Toggle;
|
|
129
129
|
exports.Tooltip = Tooltip.Tooltip;
|
|
130
130
|
exports.Uploader = index$7.Uploader;
|
|
131
|
-
exports.Autocomplete =
|
|
131
|
+
exports.Autocomplete = index$8.Autocomplete;
|
|
132
132
|
exports.useCalendar = useCalendar.useCalendar;
|
|
133
133
|
exports.useCalendarCell = useCalendarCell.useCalendarCell;
|
|
134
134
|
exports.useCalendarGrid = useCalendarGrid.useCalendarGrid;
|
|
@@ -66,6 +66,9 @@ require('../components/Tooltip.js');
|
|
|
66
66
|
require('react-icons/hi2');
|
|
67
67
|
require('react-icons/fa6');
|
|
68
68
|
require('react-dom');
|
|
69
|
+
require('../components/Autocomplete/context.js');
|
|
70
|
+
require('../components/Autocomplete/Options.js');
|
|
71
|
+
require('../components/Autocomplete/GroupedOptions.js');
|
|
69
72
|
|
|
70
73
|
const ManagerContext = React.createContext(null);
|
|
71
74
|
const ManagerProvider = ({ children }) => {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
2
|
+
import { selectVariants } from '@tecsinapse/cortex-core';
|
|
3
|
+
import { AutocompleteOption } from './Option.js';
|
|
4
|
+
import { useAutocompleteGroupedOptions } from '../../hooks/useAutocompleteGroupedOptions.js';
|
|
5
|
+
import { SkeletonOptions } from '../Select/SkeletonOptions.js';
|
|
6
|
+
|
|
7
|
+
const { groupedTitle, list } = selectVariants();
|
|
8
|
+
const AutocompleteGroupedOptions = ({
|
|
9
|
+
onSelect,
|
|
10
|
+
groupedLabelExtractor,
|
|
11
|
+
options
|
|
12
|
+
}) => {
|
|
13
|
+
const { options: _options, isLoading } = useAutocompleteGroupedOptions({
|
|
14
|
+
options
|
|
15
|
+
});
|
|
16
|
+
return isLoading ? /* @__PURE__ */ jsx(SkeletonOptions, {}) : /* @__PURE__ */ jsx("ul", { role: "listbox", className: list(), children: [..._options ?? []].map(([key, value]) => /* @__PURE__ */ jsxs("div", { children: [
|
|
17
|
+
/* @__PURE__ */ jsx("span", { className: groupedTitle(), children: groupedLabelExtractor?.(key) }),
|
|
18
|
+
value.map((option) => /* @__PURE__ */ jsx(
|
|
19
|
+
AutocompleteOption,
|
|
20
|
+
{
|
|
21
|
+
grouped: true,
|
|
22
|
+
option,
|
|
23
|
+
onSelect
|
|
24
|
+
},
|
|
25
|
+
option.value
|
|
26
|
+
))
|
|
27
|
+
] }, key)) });
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { AutocompleteGroupedOptions };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { option } from '@tecsinapse/cortex-core';
|
|
3
|
+
import { useContext, useCallback } from 'react';
|
|
4
|
+
import { AutocompleteContext } from './context.js';
|
|
5
|
+
|
|
6
|
+
const AutocompleteOption = ({
|
|
7
|
+
option: option$1,
|
|
8
|
+
onSelect,
|
|
9
|
+
grouped = false
|
|
10
|
+
}) => {
|
|
11
|
+
const context = useContext(AutocompleteContext);
|
|
12
|
+
if (!context)
|
|
13
|
+
throw new Error("AutocompleteOptions must be used within AutocompleteRoot");
|
|
14
|
+
const { setOpen } = context;
|
|
15
|
+
const handleSelect = useCallback(
|
|
16
|
+
(option2) => {
|
|
17
|
+
setOpen(false);
|
|
18
|
+
onSelect?.(option2);
|
|
19
|
+
},
|
|
20
|
+
[onSelect, setOpen]
|
|
21
|
+
);
|
|
22
|
+
return /* @__PURE__ */ jsx(
|
|
23
|
+
"li",
|
|
24
|
+
{
|
|
25
|
+
onClick: () => handleSelect(option$1),
|
|
26
|
+
className: option({
|
|
27
|
+
selected: false,
|
|
28
|
+
grouped
|
|
29
|
+
}),
|
|
30
|
+
role: "option",
|
|
31
|
+
children: /* @__PURE__ */ jsx("span", { className: "truncate flex-1", children: option$1.label })
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export { AutocompleteOption };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { selectVariants } from '@tecsinapse/cortex-core';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
import { AutocompleteOption } from './Option.js';
|
|
5
|
+
import { useAutocompleteOptions } from '../../hooks/useAutocompleteOptions.js';
|
|
6
|
+
import { SkeletonOptions } from '../Select/SkeletonOptions.js';
|
|
7
|
+
|
|
8
|
+
const { list } = selectVariants();
|
|
9
|
+
const AutocompleteOptions = ({
|
|
10
|
+
options,
|
|
11
|
+
onSelect,
|
|
12
|
+
children
|
|
13
|
+
}) => {
|
|
14
|
+
const { options: _options, isLoading } = useAutocompleteOptions({ options });
|
|
15
|
+
const defaultOptions = useMemo(
|
|
16
|
+
() => _options?.map((option) => /* @__PURE__ */ jsx(
|
|
17
|
+
AutocompleteOption,
|
|
18
|
+
{
|
|
19
|
+
option,
|
|
20
|
+
onSelect
|
|
21
|
+
},
|
|
22
|
+
option.value
|
|
23
|
+
)) ?? [],
|
|
24
|
+
[_options, onSelect]
|
|
25
|
+
);
|
|
26
|
+
return isLoading ? /* @__PURE__ */ jsx(SkeletonOptions, {}) : /* @__PURE__ */ jsx("ul", { role: "listbox", className: list(), children: children ?? defaultOptions });
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { AutocompleteOptions };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { useContext } from 'react';
|
|
3
|
+
import { FloatingPortal } from '@floating-ui/react';
|
|
4
|
+
import { Popover } from '../Popover/index.js';
|
|
5
|
+
import { AutocompleteContext } from './context.js';
|
|
6
|
+
import clsx from 'clsx';
|
|
7
|
+
|
|
8
|
+
const AutocompletePopover = ({
|
|
9
|
+
children,
|
|
10
|
+
className
|
|
11
|
+
}) => {
|
|
12
|
+
const context = useContext(AutocompleteContext);
|
|
13
|
+
if (!context)
|
|
14
|
+
throw new Error("AutocompletePopover must be used within AutocompleteRoot");
|
|
15
|
+
const { triggerWidth } = context;
|
|
16
|
+
return /* @__PURE__ */ jsx(FloatingPortal, { children: /* @__PURE__ */ jsx(
|
|
17
|
+
Popover.Content,
|
|
18
|
+
{
|
|
19
|
+
className: clsx(
|
|
20
|
+
"bg-white shadow-md rounded-md overflow-y-auto max-h-[30vh] outline-none z-9999",
|
|
21
|
+
className
|
|
22
|
+
),
|
|
23
|
+
style: {
|
|
24
|
+
width: triggerWidth ? `${triggerWidth}px` : "auto"
|
|
25
|
+
},
|
|
26
|
+
initialFocus: -1,
|
|
27
|
+
children
|
|
28
|
+
}
|
|
29
|
+
) });
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { AutocompletePopover };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { Popover } from '../Popover/index.js';
|
|
4
|
+
import { AutocompleteContext } from './context.js';
|
|
5
|
+
|
|
6
|
+
const AutocompleteRoot = ({ children }) => {
|
|
7
|
+
const [triggerWidth, setTriggerWidth] = useState();
|
|
8
|
+
const [open, setOpen] = useState(false);
|
|
9
|
+
return /* @__PURE__ */ jsx(
|
|
10
|
+
AutocompleteContext.Provider,
|
|
11
|
+
{
|
|
12
|
+
value: {
|
|
13
|
+
open,
|
|
14
|
+
setOpen,
|
|
15
|
+
triggerWidth,
|
|
16
|
+
setTriggerWidth
|
|
17
|
+
},
|
|
18
|
+
children: /* @__PURE__ */ jsx(
|
|
19
|
+
Popover.Root,
|
|
20
|
+
{
|
|
21
|
+
placement: "bottom-start",
|
|
22
|
+
controlled: true,
|
|
23
|
+
isOpen: open,
|
|
24
|
+
setIsOpen: setOpen,
|
|
25
|
+
children: /* @__PURE__ */ jsx("div", { className: "relative w-full h-full", children })
|
|
26
|
+
}
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { AutocompleteRoot };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { useContext, useRef, useEffect } from 'react';
|
|
3
|
+
import { Popover } from '../Popover/index.js';
|
|
4
|
+
import { Input } from '../Input/index.js';
|
|
5
|
+
import { AutocompleteContext } from './context.js';
|
|
6
|
+
|
|
7
|
+
const AutocompleteTrigger = ({
|
|
8
|
+
inputValue,
|
|
9
|
+
onChange,
|
|
10
|
+
label,
|
|
11
|
+
disabled,
|
|
12
|
+
placeholder
|
|
13
|
+
}) => {
|
|
14
|
+
const context = useContext(AutocompleteContext);
|
|
15
|
+
if (!context)
|
|
16
|
+
throw new Error("AutocompleteTrigger must be used within AutocompleteRoot");
|
|
17
|
+
const { setTriggerWidth, setOpen } = context;
|
|
18
|
+
const triggerRef = useRef(null);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (triggerRef.current && setTriggerWidth) {
|
|
21
|
+
const width = triggerRef.current.getBoundingClientRect().width;
|
|
22
|
+
setTriggerWidth(width);
|
|
23
|
+
}
|
|
24
|
+
}, [triggerRef.current, setTriggerWidth]);
|
|
25
|
+
const handleChange = (event) => {
|
|
26
|
+
onChange(event);
|
|
27
|
+
if (event.target.value.length > 0) {
|
|
28
|
+
setOpen(true);
|
|
29
|
+
} else {
|
|
30
|
+
setOpen(false);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
return /* @__PURE__ */ jsx(Popover.Trigger, { children: /* @__PURE__ */ jsx(
|
|
34
|
+
Input.Root,
|
|
35
|
+
{
|
|
36
|
+
label: label ?? "Selecione uma op\xE7\xE3o",
|
|
37
|
+
value: inputValue,
|
|
38
|
+
onChange: handleChange,
|
|
39
|
+
disabled,
|
|
40
|
+
placeholder,
|
|
41
|
+
ref: triggerRef
|
|
42
|
+
}
|
|
43
|
+
) });
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export { AutocompleteTrigger };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AutocompleteRoot } from './Root.js';
|
|
2
|
+
import { AutocompleteTrigger } from './Trigger.js';
|
|
3
|
+
import { AutocompletePopover } from './Popover.js';
|
|
4
|
+
import { AutocompleteOptions } from './Options.js';
|
|
5
|
+
import { AutocompleteOption } from './Option.js';
|
|
6
|
+
import { AutocompleteGroupedOptions } from './GroupedOptions.js';
|
|
7
|
+
|
|
8
|
+
const Autocomplete = {
|
|
9
|
+
Root: AutocompleteRoot,
|
|
10
|
+
Trigger: AutocompleteTrigger,
|
|
11
|
+
Popover: AutocompletePopover,
|
|
12
|
+
Options: AutocompleteOptions,
|
|
13
|
+
Option: AutocompleteOption,
|
|
14
|
+
GroupedOptions: AutocompleteGroupedOptions
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { Autocomplete };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
const useAutocompleteGroupedOptions = ({
|
|
4
|
+
options: _options
|
|
5
|
+
}) => {
|
|
6
|
+
const [options, setOptions] = useState();
|
|
7
|
+
const [isLoading, setIsLoading] = useState();
|
|
8
|
+
const [error, setError] = useState();
|
|
9
|
+
const initData = async (fetch) => {
|
|
10
|
+
setIsLoading(true);
|
|
11
|
+
try {
|
|
12
|
+
const result = await fetch();
|
|
13
|
+
if (result) {
|
|
14
|
+
setOptions(result ?? []);
|
|
15
|
+
}
|
|
16
|
+
} catch (e) {
|
|
17
|
+
setError(String(e));
|
|
18
|
+
} finally {
|
|
19
|
+
setIsLoading(false);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (typeof _options === "function") initData(_options);
|
|
24
|
+
else setOptions(_options);
|
|
25
|
+
}, [_options]);
|
|
26
|
+
return {
|
|
27
|
+
isLoading,
|
|
28
|
+
options,
|
|
29
|
+
error
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { useAutocompleteGroupedOptions };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
const useAutocompleteOptions = ({
|
|
4
|
+
options: _options
|
|
5
|
+
}) => {
|
|
6
|
+
const [options, setOptions] = useState();
|
|
7
|
+
const [isLoading, setIsLoading] = useState();
|
|
8
|
+
const [error, setError] = useState();
|
|
9
|
+
const initData = useCallback(async (fetch) => {
|
|
10
|
+
setIsLoading(true);
|
|
11
|
+
try {
|
|
12
|
+
const result = await fetch();
|
|
13
|
+
if (result) {
|
|
14
|
+
setOptions(result ?? []);
|
|
15
|
+
}
|
|
16
|
+
} catch (e) {
|
|
17
|
+
setError(String(e));
|
|
18
|
+
} finally {
|
|
19
|
+
setIsLoading(false);
|
|
20
|
+
}
|
|
21
|
+
}, []);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (typeof _options === "function") initData(_options);
|
|
24
|
+
else setOptions(_options);
|
|
25
|
+
}, [_options]);
|
|
26
|
+
return {
|
|
27
|
+
isLoading,
|
|
28
|
+
options,
|
|
29
|
+
error
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { useAutocompleteOptions };
|
package/dist/esm/index.js
CHANGED
|
@@ -42,7 +42,7 @@ export { TimeFieldInput } from './components/TimePicker/TimeFieldInput.js';
|
|
|
42
42
|
export { Toggle } from './components/Toggle.js';
|
|
43
43
|
export { Tooltip } from './components/Tooltip.js';
|
|
44
44
|
export { Uploader } from './components/Uploader/index.js';
|
|
45
|
-
export { Autocomplete } from './components/Autocomplete/
|
|
45
|
+
export { Autocomplete } from './components/Autocomplete/index.js';
|
|
46
46
|
export { useCalendar } from './hooks/useCalendar.js';
|
|
47
47
|
export { useCalendarCell } from './hooks/useCalendarCell.js';
|
|
48
48
|
export { useCalendarGrid } from './hooks/useCalendarGrid.js';
|
|
@@ -64,6 +64,9 @@ import '../components/Tooltip.js';
|
|
|
64
64
|
import 'react-icons/hi2';
|
|
65
65
|
import 'react-icons/fa6';
|
|
66
66
|
import 'react-dom';
|
|
67
|
+
import '../components/Autocomplete/context.js';
|
|
68
|
+
import '../components/Autocomplete/Options.js';
|
|
69
|
+
import '../components/Autocomplete/GroupedOptions.js';
|
|
67
70
|
|
|
68
71
|
const ManagerContext = createContext(null);
|
|
69
72
|
const ManagerProvider = ({ children }) => {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
+
interface AutocompleteContextType {
|
|
3
|
+
open: boolean;
|
|
4
|
+
setOpen: (open: boolean) => void;
|
|
5
|
+
triggerWidth: number | undefined;
|
|
6
|
+
setTriggerWidth: Dispatch<SetStateAction<number | undefined>>;
|
|
7
|
+
}
|
|
8
|
+
export declare const AutocompleteContext: import("react").Context<AutocompleteContextType | undefined>;
|
|
9
|
+
export {};
|
|
@@ -1 +1,9 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare const Autocomplete: {
|
|
2
|
+
Root: ({ children }: import("./types").AutocompleteRootProps) => import("react/jsx-runtime").JSX.Element;
|
|
3
|
+
Trigger: ({ inputValue, onChange, label, disabled, placeholder, }: import("./types").AutocompleteTriggerProps) => import("react/jsx-runtime").JSX.Element;
|
|
4
|
+
Popover: ({ children, className, }: import("./types").AutocompletePopoverProps) => import("react/jsx-runtime").JSX.Element;
|
|
5
|
+
Options: ({ options, onSelect, children, }: import("./types").AutocompleteOptionsProps) => import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
Option: ({ option, onSelect, grouped, }: import("./types").AutocompleteOptionProps) => import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
GroupedOptions: ({ onSelect, groupedLabelExtractor, options, }: import("./types").AutocompleteGroupedOptionsProps) => import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
};
|
|
9
|
+
export * from './types';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import React, { ChangeEventHandler, ReactNode } from 'react';
|
|
2
|
+
export interface Option {
|
|
3
|
+
label: string;
|
|
4
|
+
value: string;
|
|
5
|
+
}
|
|
6
|
+
export interface AutocompleteRootProps {
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
className?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface AutocompleteTriggerProps {
|
|
11
|
+
inputValue: string;
|
|
12
|
+
onChange: ChangeEventHandler<HTMLInputElement>;
|
|
13
|
+
label?: string;
|
|
14
|
+
placeholder?: string;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
className?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface AutocompletePopoverProps {
|
|
19
|
+
children: ReactNode;
|
|
20
|
+
className?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface AutocompleteOptionsProps {
|
|
23
|
+
options?: Option[] | (() => Promise<Option[]>);
|
|
24
|
+
onSelect?: (option: Option) => void;
|
|
25
|
+
children?: ReactNode;
|
|
26
|
+
}
|
|
27
|
+
export interface AutocompleteGroupedOptionsProps {
|
|
28
|
+
options?: Map<string, Option[]> | (() => Promise<Map<string, Option[]>>);
|
|
29
|
+
groupedLabelExtractor: (value: string) => string;
|
|
30
|
+
onSelect?: (option: Option) => void;
|
|
31
|
+
}
|
|
32
|
+
export interface AutocompleteOptionProps {
|
|
33
|
+
option: Option;
|
|
34
|
+
onSelect?: (option: Option) => void;
|
|
35
|
+
grouped?: boolean;
|
|
36
|
+
}
|
|
37
|
+
export interface AutocompleteProps {
|
|
38
|
+
inputValue: string;
|
|
39
|
+
setInputValue: React.Dispatch<React.SetStateAction<string>>;
|
|
40
|
+
onChange: ChangeEventHandler<HTMLInputElement>;
|
|
41
|
+
options: Option[] | (() => Promise<Option[]>);
|
|
42
|
+
onSelect?: (option: Option) => void;
|
|
43
|
+
label?: string;
|
|
44
|
+
placeholder?: string;
|
|
45
|
+
disabled?: boolean;
|
|
46
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Option } from '../components/Autocomplete/types';
|
|
2
|
+
interface useAutocompleteGroupedOptionsProps {
|
|
3
|
+
options?: Map<string, Option[]> | (() => Promise<Map<string, Option[]>>);
|
|
4
|
+
}
|
|
5
|
+
export declare const useAutocompleteGroupedOptions: ({ options: _options, }: useAutocompleteGroupedOptionsProps) => {
|
|
6
|
+
isLoading: boolean | undefined;
|
|
7
|
+
options: Map<string, Option[]> | undefined;
|
|
8
|
+
error: string | undefined;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Option } from '../components/Autocomplete/types';
|
|
2
|
+
interface useAutocompleteOptionsProps {
|
|
3
|
+
options?: Option[] | (() => Promise<Option[]>);
|
|
4
|
+
}
|
|
5
|
+
export declare const useAutocompleteOptions: ({ options: _options, }: useAutocompleteOptionsProps) => {
|
|
6
|
+
isLoading: boolean | undefined;
|
|
7
|
+
options: Option[] | undefined;
|
|
8
|
+
error: string | undefined;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tecsinapse/cortex-react",
|
|
3
|
-
"version": "2.2.0-beta.
|
|
3
|
+
"version": "2.2.0-beta.6",
|
|
4
4
|
"description": "React components based in @tecsinapse/cortex-core",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/esm/index.js",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"react-icons": ">=5.2.0",
|
|
49
49
|
"tailwindcss": "^4.1.16"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "dc4acfa02813924f4707075b33eec14db030b521"
|
|
52
52
|
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
-
var React = require('react');
|
|
5
|
-
var index = require('../Popover/index.js');
|
|
6
|
-
var index$1 = require('../Input/index.js');
|
|
7
|
-
var react = require('@floating-ui/react');
|
|
8
|
-
|
|
9
|
-
const Autocomplete = ({
|
|
10
|
-
inputValue,
|
|
11
|
-
label,
|
|
12
|
-
onChange,
|
|
13
|
-
onSelect,
|
|
14
|
-
disabled,
|
|
15
|
-
options
|
|
16
|
-
}) => {
|
|
17
|
-
const [triggerWidth, setTriggerWidth] = React.useState();
|
|
18
|
-
const [open, setOpen] = React.useState(true);
|
|
19
|
-
const filteredOptions = React.useMemo(() => {
|
|
20
|
-
const optionsList = (options ?? []).filter(
|
|
21
|
-
(op) => op.label.toLowerCase().includes(inputValue.toLowerCase())
|
|
22
|
-
);
|
|
23
|
-
return optionsList;
|
|
24
|
-
}, [options, inputValue]);
|
|
25
|
-
const handleSelect = (option) => {
|
|
26
|
-
setOpen(false);
|
|
27
|
-
onSelect?.(option);
|
|
28
|
-
};
|
|
29
|
-
const handleChange = (event) => {
|
|
30
|
-
onChange(event);
|
|
31
|
-
if (event.target.value.length > 0) {
|
|
32
|
-
setOpen(true);
|
|
33
|
-
} else {
|
|
34
|
-
setOpen(false);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
const triggerRef = React.useRef(void 0);
|
|
38
|
-
React.useEffect(() => {
|
|
39
|
-
if (triggerRef.current && setTriggerWidth) {
|
|
40
|
-
const width = triggerRef.current.getBoundingClientRect().width;
|
|
41
|
-
setTriggerWidth(width);
|
|
42
|
-
}
|
|
43
|
-
}, [triggerRef.current, setTriggerWidth]);
|
|
44
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
45
|
-
index.Popover.Root,
|
|
46
|
-
{
|
|
47
|
-
placement: "bottom-start",
|
|
48
|
-
controlled: true,
|
|
49
|
-
isOpen: open,
|
|
50
|
-
setIsOpen: setOpen,
|
|
51
|
-
children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "relative w-full h-full", children: [
|
|
52
|
-
/* @__PURE__ */ jsxRuntime.jsx(index.Popover.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
53
|
-
index$1.Input.Root,
|
|
54
|
-
{
|
|
55
|
-
label: label ?? "Selecione uma op\xE7\xE3o",
|
|
56
|
-
value: inputValue,
|
|
57
|
-
onChange: handleChange,
|
|
58
|
-
disabled,
|
|
59
|
-
ref: triggerRef
|
|
60
|
-
}
|
|
61
|
-
) }),
|
|
62
|
-
/* @__PURE__ */ jsxRuntime.jsx(react.FloatingPortal, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
63
|
-
index.Popover.Content,
|
|
64
|
-
{
|
|
65
|
-
className: "w-full bg-white shadow-md rounded-md overflow-hidden max-h-[30vh] outline-none z-9999",
|
|
66
|
-
style: {
|
|
67
|
-
width: triggerWidth ? `${triggerWidth}px` : "auto"
|
|
68
|
-
},
|
|
69
|
-
initialFocus: -1,
|
|
70
|
-
children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex flex-col overflow-y-auto", children: (options ?? filteredOptions ?? []).map((option) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
71
|
-
"div",
|
|
72
|
-
{
|
|
73
|
-
className: "flex w-full h-[3rem] items-center gap-centi p-centi cursor-pointer hover:bg-secondary-xlight bg-inherit",
|
|
74
|
-
onClick: () => handleSelect(option),
|
|
75
|
-
children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-base truncate", children: option.label })
|
|
76
|
-
}
|
|
77
|
-
)) })
|
|
78
|
-
}
|
|
79
|
-
) })
|
|
80
|
-
] })
|
|
81
|
-
}
|
|
82
|
-
);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
exports.Autocomplete = Autocomplete;
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
2
|
-
import { useState, useMemo, useRef, useEffect } from 'react';
|
|
3
|
-
import { Popover } from '../Popover/index.js';
|
|
4
|
-
import { Input } from '../Input/index.js';
|
|
5
|
-
import { FloatingPortal } from '@floating-ui/react';
|
|
6
|
-
|
|
7
|
-
const Autocomplete = ({
|
|
8
|
-
inputValue,
|
|
9
|
-
label,
|
|
10
|
-
onChange,
|
|
11
|
-
onSelect,
|
|
12
|
-
disabled,
|
|
13
|
-
options
|
|
14
|
-
}) => {
|
|
15
|
-
const [triggerWidth, setTriggerWidth] = useState();
|
|
16
|
-
const [open, setOpen] = useState(true);
|
|
17
|
-
const filteredOptions = useMemo(() => {
|
|
18
|
-
const optionsList = (options ?? []).filter(
|
|
19
|
-
(op) => op.label.toLowerCase().includes(inputValue.toLowerCase())
|
|
20
|
-
);
|
|
21
|
-
return optionsList;
|
|
22
|
-
}, [options, inputValue]);
|
|
23
|
-
const handleSelect = (option) => {
|
|
24
|
-
setOpen(false);
|
|
25
|
-
onSelect?.(option);
|
|
26
|
-
};
|
|
27
|
-
const handleChange = (event) => {
|
|
28
|
-
onChange(event);
|
|
29
|
-
if (event.target.value.length > 0) {
|
|
30
|
-
setOpen(true);
|
|
31
|
-
} else {
|
|
32
|
-
setOpen(false);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
const triggerRef = useRef(void 0);
|
|
36
|
-
useEffect(() => {
|
|
37
|
-
if (triggerRef.current && setTriggerWidth) {
|
|
38
|
-
const width = triggerRef.current.getBoundingClientRect().width;
|
|
39
|
-
setTriggerWidth(width);
|
|
40
|
-
}
|
|
41
|
-
}, [triggerRef.current, setTriggerWidth]);
|
|
42
|
-
return /* @__PURE__ */ jsx(
|
|
43
|
-
Popover.Root,
|
|
44
|
-
{
|
|
45
|
-
placement: "bottom-start",
|
|
46
|
-
controlled: true,
|
|
47
|
-
isOpen: open,
|
|
48
|
-
setIsOpen: setOpen,
|
|
49
|
-
children: /* @__PURE__ */ jsxs("div", { className: "relative w-full h-full", children: [
|
|
50
|
-
/* @__PURE__ */ jsx(Popover.Trigger, { children: /* @__PURE__ */ jsx(
|
|
51
|
-
Input.Root,
|
|
52
|
-
{
|
|
53
|
-
label: label ?? "Selecione uma op\xE7\xE3o",
|
|
54
|
-
value: inputValue,
|
|
55
|
-
onChange: handleChange,
|
|
56
|
-
disabled,
|
|
57
|
-
ref: triggerRef
|
|
58
|
-
}
|
|
59
|
-
) }),
|
|
60
|
-
/* @__PURE__ */ jsx(FloatingPortal, { children: /* @__PURE__ */ jsx(
|
|
61
|
-
Popover.Content,
|
|
62
|
-
{
|
|
63
|
-
className: "w-full bg-white shadow-md rounded-md overflow-hidden max-h-[30vh] outline-none z-9999",
|
|
64
|
-
style: {
|
|
65
|
-
width: triggerWidth ? `${triggerWidth}px` : "auto"
|
|
66
|
-
},
|
|
67
|
-
initialFocus: -1,
|
|
68
|
-
children: /* @__PURE__ */ jsx("div", { className: "w-full flex flex-col overflow-y-auto", children: (options ?? filteredOptions ?? []).map((option) => /* @__PURE__ */ jsx(
|
|
69
|
-
"div",
|
|
70
|
-
{
|
|
71
|
-
className: "flex w-full h-[3rem] items-center gap-centi p-centi cursor-pointer hover:bg-secondary-xlight bg-inherit",
|
|
72
|
-
onClick: () => handleSelect(option),
|
|
73
|
-
children: /* @__PURE__ */ jsx("span", { className: "text-base truncate", children: option.label })
|
|
74
|
-
}
|
|
75
|
-
)) })
|
|
76
|
-
}
|
|
77
|
-
) })
|
|
78
|
-
] })
|
|
79
|
-
}
|
|
80
|
-
);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export { Autocomplete };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import React, { ChangeEventHandler } from 'react';
|
|
2
|
-
interface AutocompleteProps {
|
|
3
|
-
inputValue: string;
|
|
4
|
-
setInputValue: React.Dispatch<React.SetStateAction<string>>;
|
|
5
|
-
onChange: ChangeEventHandler<HTMLInputElement>;
|
|
6
|
-
options: Option[];
|
|
7
|
-
onSelect?: (option: Option) => void;
|
|
8
|
-
label?: string;
|
|
9
|
-
placeholder?: string;
|
|
10
|
-
disabled?: boolean;
|
|
11
|
-
}
|
|
12
|
-
interface Option {
|
|
13
|
-
label: string;
|
|
14
|
-
value: string;
|
|
15
|
-
}
|
|
16
|
-
export declare const Autocomplete: ({ inputValue, label, onChange, onSelect, disabled, options, }: AutocompleteProps) => import("react/jsx-runtime").JSX.Element;
|
|
17
|
-
export {};
|