Skip to content

Commit 6ed6034

Browse files
authored
Merge pull request #8303 from marmelab/fix-autocompleteinput-in-referenceinput-repopulate-suggestions
Fix suggestions do not repopulate on blur in `AutocompleteInput` when used inside `ReferenceInput`
2 parents a2a2b3b + 014e34f commit 6ed6034

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,30 @@ describe('<AutocompleteInput />', () => {
13301330
expect(screen.queryByText('Leo Tolstoy')).toBeNull();
13311331
});
13321332

1333+
it('should repopulate the suggestions after the suggestions are dismissed', async () => {
1334+
render(<InsideReferenceInput />);
1335+
const input = await screen.findByLabelText('Author');
1336+
fireEvent.focus(input);
1337+
await waitFor(() => {
1338+
expect(screen.queryByText('Victor Hugo')).not.toBeNull();
1339+
});
1340+
fireEvent.change(input, { target: { value: 'bar' } });
1341+
await waitFor(
1342+
() => {
1343+
expect(screen.queryByText('Victor Hugo')).toBeNull();
1344+
},
1345+
{ timeout: 2000 }
1346+
);
1347+
fireEvent.blur(input);
1348+
fireEvent.focus(input);
1349+
await waitFor(
1350+
() => {
1351+
expect(screen.queryByText('Victor Hugo')).not.toBeNull();
1352+
},
1353+
{ timeout: 2000 }
1354+
);
1355+
});
1356+
13331357
it('should not change an undefined value to empty string', async () => {
13341358
const onSuccess = jest.fn();
13351359
render(<InsideReferenceInputDefaultValue onSuccess={onSuccess} />);

packages/ra-ui-materialui/src/input/AutocompleteInput.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,20 @@ If you provided a React element for the optionText prop, you must also provide t
409409
const finalOnBlur = useCallback((): void => {
410410
if (clearOnBlur) {
411411
const optionLabel = getOptionLabel(selectedChoice);
412-
setFilterValue(optionLabel);
412+
if (!isEqual(optionLabel, filterValue)) {
413+
setFilterValue(optionLabel);
414+
debouncedSetFilter('');
415+
}
413416
}
414417
field.onBlur();
415-
}, [clearOnBlur, field, selectedChoice, getOptionLabel]);
418+
}, [
419+
clearOnBlur,
420+
field,
421+
getOptionLabel,
422+
selectedChoice,
423+
filterValue,
424+
debouncedSetFilter,
425+
]);
416426

417427
useEffect(() => {
418428
if (!multiple) {

0 commit comments

Comments
 (0)