Skip to content

Commit

Permalink
fix: when there is a query, the first result should be selected, rega…
Browse files Browse the repository at this point in the history
…rdless of defaultIndex

Re AB#17933
  • Loading branch information
cristinecula committed Nov 21, 2024
1 parent b5f9075 commit a6ef56c
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/autocomplete/use-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface Props<I> extends Base<I> {
disabled?: boolean;
onFocus?: (focused?: boolean) => void;
preserveOrder?: boolean;
defaultIndex?: number;
}

export const useAutocomplete = <I>({
Expand All @@ -57,6 +58,7 @@ export const useAutocomplete = <I>({
keepOpened,
keepQuery,
preserveOrder,
defaultIndex,
...thru
}: Props<I>) => {
const textual = useMemo(
Expand Down Expand Up @@ -178,6 +180,8 @@ export const useAutocomplete = <I>({
(val: I | I[]) => meta.onChange(without(val)(meta.value) as I[]),
[meta],
),
// whenever there is a query, override defaultIndex to 0, so the first result gets selected
defaultIndex: query?.length > 0 ? 0 : defaultIndex,
};
};

Expand Down

0 comments on commit a6ef56c

Please sign in to comment.