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 leading character in AutocompleteInput can not be deleted #7954

Merged
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
33 changes: 33 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,39 @@ describe('<AutocompleteInput />', () => {
});
});

it('should allow to clear the first character', async () => {
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={jest.fn()} defaultValues={{ role: 2 }}>
<AutocompleteInput
{...defaultProps}
optionText="foobar"
choices={[
{ id: 2, foobar: 'foo' },
{ id: 3, foobar: 'bar' },
]}
/>
</SimpleForm>
</AdminContext>
);

const input = screen.getByLabelText(
'resources.users.fields.role'
) as HTMLInputElement;

fireEvent.focus(input);

userEvent.type(input, 'f');
await waitFor(() => {
expect(input.value).toEqual('f');
});

userEvent.type(input, '{backspace}');
await waitFor(() => {
expect(input.value).toEqual('');
});
});

it('should use optionText with a string value including "." as text identifier', async () => {
render(
<AdminContext dataProvider={testDataProvider()}>
Expand Down
8 changes: 5 additions & 3 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,14 @@ If you provided a React element for the optionText prop, you must also provide t
newInputValue: string,
reason: string
) => {
if (!doesQueryMatchSelection(newInputValue)) {
if (!doesQueryMatchSelection(newInputValue, event?.type)) {
setFilterValue(newInputValue);
debouncedSetFilter(newInputValue);
}
};

const doesQueryMatchSelection = useCallback(
filter => {
(filter: string, eventType?: string) => {
let selectedItemTexts = [];

if (multiple) {
Expand All @@ -375,7 +375,9 @@ If you provided a React element for the optionText prop, you must also provide t
selectedItemTexts = [getOptionLabel(selectedChoice)];
}

return selectedItemTexts.includes(filter);
return eventType && eventType === 'change'
? selectedItemTexts.includes(filter) && selectedChoice
: selectedItemTexts.includes(filter);
},
[getOptionLabel, multiple, selectedChoice]
);
Expand Down