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

Set AutocompleteInput value to an empty string when it's selected item is null #4730

Merged
merged 2 commits into from
Apr 30, 2020
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
26 changes: 26 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import { Form } from 'react-final-form';
import { TestTranslationProvider } from 'ra-core';

describe('<AutocompleteInput />', () => {
//Fix document.createRange is not a function error on fireEvent usage (Fixed in jsdom v16.0.0)
//reported by https://github.com/mui-org/material-ui/issues/15726#issuecomment-493124813
global.document.createRange = () => ({
setStart: () => {},
setEnd: () => {},
commonAncestorContainer: {
nodeName: 'BODY',
ownerDocument: document,
},
});

const defaultProps = {
source: 'role',
resource: 'users',
Expand All @@ -34,6 +45,21 @@ describe('<AutocompleteInput />', () => {
expect(getByRole('combobox')).not.toBeNull();
});

it('should set AutocompleteInput value to an empty string when the selected item is null', () => {
const { queryByDisplayValue } = render(
<Form
onSubmit={jest.fn()}
render={() => (
<AutocompleteInput
{...defaultProps}
choices={[{ id: 2, name: 'foo' }]}
/>
)}
/>
);
expect(queryByDisplayValue('')).not.toBeNull();
});

it('should use the input parameter value as the initial state and input searchText', () => {
const { queryByDisplayValue } = render(
<Form
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ const AutocompleteInput: FunctionComponent<
// If we have a value, set the filter to its text so that
// Downshift displays it correctly
setFilterValue(
typeof input.value === 'undefined' || input.value === null
typeof input.value === 'undefined' ||
input.value === null ||
selectedItem === null
? ''
: inputText
? inputText(getChoiceText(selectedItem).props.record)
Expand Down