Skip to content

Commit

Permalink
Merge pull request #10033 from marmelab/fix/issue-9993/AutoCompleteIn…
Browse files Browse the repository at this point in the history
…put-clear-button-does-not-clear-new-choice

Fix `<AutocompleteInput>` clear button does not clear new choice
  • Loading branch information
adguernier authored Jul 22, 2024
2 parents 4b3dff0 + bb59bb5 commit f574cd9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
30 changes: 30 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
OnChange,
InsideReferenceInputOnChange,
WithInputProps,
OnCreate,
} from './AutocompleteInput.stories';
import { act } from '@testing-library/react-hooks';
import { ReferenceArrayInput } from './ReferenceArrayInput';
Expand Down Expand Up @@ -1692,6 +1693,35 @@ describe('<AutocompleteInput />', () => {
});
});

it('should clear the input mutiple tiles with on create set', async () => {
render(<OnCreate />);
let input = (await screen.findByLabelText(
'Author'
)) as HTMLInputElement;

fireEvent.focus(input);
expect(screen.getAllByRole('option')).toHaveLength(5);
expect(screen.queryByText('Create New choice')).toBeNull();

userEvent.type(input, 'New choice');
expect(screen.getAllByRole('option')).toHaveLength(1);
expect(screen.getByText('Create New choice')).toBeDefined();

fireEvent.click(screen.getByLabelText('Clear value'));
expect(input.value).toEqual('');
expect(screen.getAllByRole('option')).toHaveLength(6);
expect(screen.queryByText('Create New choice')).toBeNull();

userEvent.type(input, 'New choice');
expect(screen.getAllByRole('option')).toHaveLength(1);
expect(screen.getByText('Create New choice')).toBeDefined();

fireEvent.click(screen.getByLabelText('Clear value'));
expect(screen.getAllByRole('option')).toHaveLength(6);
expect(screen.queryByText('Create New choice')).toBeNull();
expect(input.value).toEqual('');
});

it('should handle nullish values', async () => {
render(<NullishValuesSupport />);

Expand Down
20 changes: 12 additions & 8 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ If you provided a React element for the optionText prop, you must also provide t
setFilterValue(newInputValue);
debouncedSetFilter(newInputValue);
}

if (reason === 'clear') {
setFilterValue('');
debouncedSetFilter('');
}
onInputChange?.(event, newInputValue, reason);
};

Expand Down Expand Up @@ -523,13 +526,14 @@ If you provided a React element for the optionText prop, you must also provide t
return filteredOptions;
};

const handleAutocompleteChange = (
event: any,
newValue: any,
_reason: string
) => {
handleChangeWithCreateSupport(newValue != null ? newValue : emptyValue);
};
const handleAutocompleteChange = useCallback(
(event: any, newValue: any, _reason: string) => {
handleChangeWithCreateSupport(
newValue != null ? newValue : emptyValue
);
},
[emptyValue, handleChangeWithCreateSupport]
);

const oneSecondHasPassed = useTimeout(1000, filterValue);

Expand Down

0 comments on commit f574cd9

Please sign in to comment.