Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ export const Autocomplete = ({
}
}, [options, onChange]);

React.useEffect(() => {
setUserInput(defaultValue);
}, [defaultValue]);

const handleClick = (evt: React.FormEvent<HTMLInputElement>) => {
setActiveOption(0);
setFilterList([]);
Expand Down
37 changes: 36 additions & 1 deletion src/autocomplete/__tests__/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,39 @@ describe('Autocomplete', () => {
expect(input).toBeInTheDocument();
});

it('When defaultValue prop is changed, Autocomplete component updates.', async () => {
const DummyChangeState = () => {
const [defaultValue, setDefaultValue] = React.useState('Apple');
const handleClick = (value: string) => {
setDefaultValue(value);
};
return (
<>
<Autocomplete
options={['Apple', 'Banana']}
defaultValue={defaultValue}
onSelected={value => handleClick(value)}
/>
<button onClick={() => handleClick('Orange')}>Orange</button>
</>
);
};

render(<DummyChangeState />);
await act(async () => {
await waitFor(() => {
const input: any = screen.getByRole('textbox');
expect(input.value).toBe('Apple');
});
});
const button = screen.getByRole('button');
userEvent.click(button);
await waitFor(() => {
const input: any = screen.getByRole('textbox');
expect(input.value).toBe('Orange');
});
});

it('should display available options', async () => {
const user = userEvent.setup();

Expand Down Expand Up @@ -831,7 +864,9 @@ describe('Autocomplete', () => {
return options
.filter(
(option: any) =>
optionName(option).toLowerCase().indexOf(queryStr) !== -1
optionName(option)
.toLowerCase()
.indexOf(queryStr) !== -1
)
.map((option: any) => {
const commonRank = 0;
Expand Down