@homecode/ui 4.20.2 → 4.20.4
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.
|
@@ -61,6 +61,7 @@ function Autocomplete(props) {
|
|
|
61
61
|
const isMounted = useIsMounted();
|
|
62
62
|
const [options, setOptions] = useState([]);
|
|
63
63
|
const [isLoading, setIsLoading] = useState(false);
|
|
64
|
+
const isFocusedRef = useRef(false);
|
|
64
65
|
const searchValRef = useRef(value);
|
|
65
66
|
const [searchValue, _setSearchValue] = useState(value);
|
|
66
67
|
const setSearchValue = (val) => {
|
|
@@ -72,6 +73,14 @@ function Autocomplete(props) {
|
|
|
72
73
|
const inputRef = useRef(null);
|
|
73
74
|
const isOpen = options.length > 0;
|
|
74
75
|
const classes = cn(S.root, className);
|
|
76
|
+
const handleFocus = (e) => {
|
|
77
|
+
isFocusedRef.current = true;
|
|
78
|
+
inputProps?.onFocus?.(e);
|
|
79
|
+
};
|
|
80
|
+
const handleBlur = (e) => {
|
|
81
|
+
isFocusedRef.current = false;
|
|
82
|
+
inputProps?.onBlur?.(e);
|
|
83
|
+
};
|
|
75
84
|
const handleInputChange = (e, value) => {
|
|
76
85
|
const val = (value || e?.target.value) ?? '';
|
|
77
86
|
setOptions([]);
|
|
@@ -86,7 +95,7 @@ function Autocomplete(props) {
|
|
|
86
95
|
onSelect(value);
|
|
87
96
|
// set input caret to the end
|
|
88
97
|
requestAnimationFrame(() => {
|
|
89
|
-
const input = inputRef.current
|
|
98
|
+
const input = inputRef.current;
|
|
90
99
|
if (!input)
|
|
91
100
|
return;
|
|
92
101
|
input.focus();
|
|
@@ -123,13 +132,10 @@ function Autocomplete(props) {
|
|
|
123
132
|
}
|
|
124
133
|
}, debounceDelay);
|
|
125
134
|
useEffect(() => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
else {
|
|
131
|
-
setSearchValue(value);
|
|
132
|
-
}
|
|
135
|
+
setSearchValue(value);
|
|
136
|
+
setOptions([]);
|
|
137
|
+
if (isFocusedRef.current)
|
|
138
|
+
fetchOptions(value);
|
|
133
139
|
}, [value]);
|
|
134
140
|
const optionsList = useMemo(() => {
|
|
135
141
|
if (!options.length)
|
|
@@ -138,7 +144,7 @@ function Autocomplete(props) {
|
|
|
138
144
|
}, [options, focusedIndex]);
|
|
139
145
|
return (jsx(Popup, { className: classes, isOpen: isOpen, focusControl: true, size: size, direction: "bottom", ...popupProps, trigger: jsxs("div", { className: inputWrapperClassName, children: [jsx(Input, { ref: inputRef,
|
|
140
146
|
// @ts-ignore
|
|
141
|
-
size: size, ...inputProps, value: searchValue, onChange: handleInputChange, className: inputProps.className }), isLoading && (jsx(Shimmer, { className: S.shimmer, size: size, round: inputProps?.round }))] }), content: optionsList, contentProps: {
|
|
147
|
+
size: size, ...inputProps, value: searchValue, onChange: handleInputChange, onFocus: handleFocus, onBlur: handleBlur, className: inputProps.className }), isLoading && (jsx(Shimmer, { className: S.shimmer, size: size, round: inputProps?.round }))] }), content: optionsList, contentProps: {
|
|
142
148
|
className: S.popupContent,
|
|
143
149
|
} }));
|
|
144
150
|
}
|