Skip to content
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
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