Skip to content

Commit 8dc0492

Browse files
authored
fix: handle the nullish value input (AutocompleteInput) (#774)
Ref: HDX-1639
1 parent 92a4800 commit 8dc0492

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

packages/app/src/AutocompleteInput.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default function AutocompleteInput({
2727
queryHistoryType,
2828
}: {
2929
inputRef: React.RefObject<HTMLInputElement>;
30-
value: string;
30+
value?: string;
3131
onChange: (value: string) => void;
3232
onSubmit?: () => void;
3333
placeholder?: string;
@@ -64,7 +64,7 @@ export default function AutocompleteInput({
6464
label: q,
6565
};
6666
});
67-
}, [queryHistory]);
67+
}, [queryHistory, queryHistoryType]);
6868

6969
useEffect(() => {
7070
if (isSearchInputFocused) {
@@ -74,7 +74,12 @@ export default function AutocompleteInput({
7474

7575
useEffect(() => {
7676
// only show search history when: 1.no input, 2.has search type, 3.has history list
77-
if (value.length === 0 && queryHistoryList.length > 0 && queryHistoryType) {
77+
if (
78+
value != null &&
79+
value.length === 0 &&
80+
queryHistoryList.length > 0 &&
81+
queryHistoryType
82+
) {
7883
setShowSearchHistory(true);
7984
} else {
8085
setShowSearchHistory(false);
@@ -114,8 +119,10 @@ export default function AutocompleteInput({
114119
setSelectedAutocompleteIndex(-1);
115120

116121
const newValue =
117-
value.split(' ').slice(0, -1).join(' ') +
118-
`${value.split(' ').length > 1 ? ' ' : ''}${suggestion}`;
122+
value == null
123+
? suggestion
124+
: value.split(' ').slice(0, -1).join(' ') +
125+
`${value.split(' ').length > 1 ? ' ' : ''}${suggestion}`;
119126
onChange(newValue);
120127
inputRef.current?.focus();
121128
};
@@ -274,7 +281,7 @@ export default function AutocompleteInput({
274281
suggestedProperties[selectedAutocompleteIndex].value,
275282
);
276283
} else {
277-
if (queryHistoryType) {
284+
if (queryHistoryType && value) {
278285
setQueryHistory(value);
279286
}
280287
onSubmit?.();

0 commit comments

Comments
 (0)