Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <AutocompleteInput> does not repopulate suggestions on blur in when used inside <ReferenceInput> #8303

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
24 changes: 24 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,30 @@ describe('<AutocompleteInput />', () => {
expect(screen.queryByText('Leo Tolstoy')).toBeNull();
});

it('should repopulate the suggestions after the suggestions are dismissed', async () => {
render(<InsideReferenceInput />);
const input = await screen.findByLabelText('Author');
fireEvent.focus(input);
await waitFor(() => {
expect(screen.queryByText('Victor Hugo')).not.toBeNull();
});
fireEvent.change(input, { target: { value: 'bar' } });
await waitFor(
() => {
expect(screen.queryByText('Victor Hugo')).toBeNull();
},
{ timeout: 2000 }
);
fireEvent.blur(input);
fireEvent.focus(input);
await waitFor(
() => {
expect(screen.queryByText('Victor Hugo')).not.toBeNull();
},
{ timeout: 2000 }
);
});

it('should not change an undefined value to empty string', async () => {
const onSuccess = jest.fn();
render(<InsideReferenceInputDefaultValue onSuccess={onSuccess} />);
Expand Down
14 changes: 12 additions & 2 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,20 @@ If you provided a React element for the optionText prop, you must also provide t
const finalOnBlur = useCallback((): void => {
if (clearOnBlur) {
const optionLabel = getOptionLabel(selectedChoice);
setFilterValue(optionLabel);
if (!isEqual(optionLabel, filterValue)) {
setFilterValue(optionLabel);
debouncedSetFilter('');
}
}
field.onBlur();
}, [clearOnBlur, field, selectedChoice, getOptionLabel]);
}, [
clearOnBlur,
field,
getOptionLabel,
selectedChoice,
filterValue,
debouncedSetFilter,
]);

useEffect(() => {
if (!multiple) {
Expand Down