Skip to content

Commit

Permalink
Merge pull request #244 from anubra266/feature/fixValueProp
Browse files Browse the repository at this point in the history
Fix `value` prop does nothing
  • Loading branch information
Kysluss committed Feb 10, 2024
2 parents fe3693d + 38ceebf commit 7941c9a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/autocomplete-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export const AutoCompleteInput = forwardRef<AutoCompleteInputProps, "input">(
inputRef,
getInputProps,
tags,
setQuery
setQuery,
value,
itemList
} = useAutoCompleteContext();

// const ref = useMergeRefs(forwardedRef, inputRef);
Expand All @@ -59,14 +61,24 @@ export const AutoCompleteInput = forwardRef<AutoCompleteInputProps, "input">(
...rest
} = props;

const { value } = rest;
const { value: inputValue } = rest;

useEffect(() => {
if(value !== undefined && (typeof value === 'string' || value instanceof String)) {
setQuery(value);
const item = itemList.find(l => l.value === value);

const newQuery = item === undefined ? value : item.label;

setQuery(newQuery);
}
}, [value]);

useEffect(() => {
if(inputValue !== undefined && (typeof inputValue === 'string' || inputValue instanceof String)) {
setQuery(inputValue);
}
}, [inputValue]);

const themeInput: any = useMultiStyleConfig("Input", props);

let { wrapper, input: inputProps } = getInputProps(rest, themeInput);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export type UseAutoCompleteReturn = {
resetItems: (focusInput?: boolean) => void;
setQuery: Dispatch<SetStateAction<any>>;
tags: ItemTag[];
value: Item["value"];
values: Item["value"][];
};

Expand Down
1 change: 1 addition & 0 deletions src/use-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ export function useAutoComplete(
resetItems,
setQuery,
tags,
value,
values,
};
}

0 comments on commit 7941c9a

Please sign in to comment.