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 clearOnBlur has no effect #8123

Merged
merged 6 commits into from
Sep 1, 2022
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
28 changes: 27 additions & 1 deletion packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ describe('<AutocompleteInput />', () => {
fireEvent.blur(input);
fireEvent.focus(input);
await waitFor(() => {
expect(screen.queryAllByRole('option').length).toEqual(2);
expect(screen.queryAllByRole('option').length).toEqual(3);
});
});

Expand Down Expand Up @@ -1234,4 +1234,30 @@ describe('<AutocompleteInput />', () => {
screen.getByText(/Dalmatian #1050/);
});
});

it('should clear the input when its blurred, having an unmatching selection and clearOnBlur prop is true', async () => {
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={jest.fn()}>
<AutocompleteInput
{...defaultProps}
clearOnBlur
choices={[
{ id: 1, name: 'ab' },
{ id: 2, name: 'abc' },
{ id: 3, name: '123' },
]}
/>
</SimpleForm>
</AdminContext>
);
const input = screen.getByLabelText(
'resources.users.fields.role'
) as HTMLInputElement;
fireEvent.change(input, { target: { value: 'no match' } });
fireEvent.blur(input);
await waitFor(() => {
expect(input.value).toEqual('');
});
});
});
13 changes: 11 additions & 2 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export const AutocompleteInput = <
const {
choices: choicesProp,
className,
clearOnBlur = true,
clearText = 'ra.action.clear_input_value',
closeText = 'ra.action.close',
create,
Expand Down Expand Up @@ -350,6 +351,14 @@ If you provided a React element for the optionText prop, you must also provide t
[getChoiceText, inputText, createId]
);

const finalOnBlur = useCallback((): void => {
if (clearOnBlur) {
const optionLabel = getOptionLabel(selectedChoice);
setFilterValue(optionLabel);
}
field.onBlur();
}, [clearOnBlur, field, selectedChoice, getOptionLabel]);

useEffect(() => {
if (!multiple) {
const optionLabel = getOptionLabel(selectedChoice);
Expand Down Expand Up @@ -523,7 +532,7 @@ If you provided a React element for the optionText prop, you must also provide t
: noOptionsText
}
selectOnFocus
clearOnBlur
clearOnBlur={clearOnBlur}
{...sanitizeInputRestProps(rest)}
freeSolo={!!create || !!onCreate}
handleHomeEndKeys={!!create || !!onCreate}
Expand All @@ -543,7 +552,7 @@ If you provided a React element for the optionText prop, you must also provide t
}
value={selectedChoice}
onChange={handleAutocompleteChange}
onBlur={field.onBlur}
onBlur={finalOnBlur}
onInputChange={handleInputChange}
renderOption={(props, record: RaRecord) => {
(props as {
Expand Down