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 allow zero as value in AutocompleteInput #8144

Merged
merged 3 commits into from
Sep 6, 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
20 changes: 17 additions & 3 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -901,18 +901,32 @@ describe('<AutocompleteInput />', () => {
);
});

it('should accept 0 as an input value', () => {
it('should accept 0 as an input value', async () => {
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={jest.fn()} defaultValues={{ role: 0 }}>
<SimpleForm onSubmit={jest.fn()}>
<AutocompleteInput
{...defaultProps}
choices={[{ id: 0, name: 'foo' }]}
/>
</SimpleForm>
</AdminContext>
);
expect(screen.queryByDisplayValue('foo')).not.toBeNull();
const input = screen.getByLabelText(
'resources.users.fields.role'
) as HTMLInputElement;
input.focus();
fireEvent.change(input, { target: { value: 'foo' } });
await waitFor(
() => {
expect(screen.getByRole('listbox').children).toHaveLength(1);
},
{ timeout: 2000 }
);
fireEvent.click(screen.getByText('foo'));
await waitFor(() => {
expect(input.value).toEqual('foo');
});
});

it('should support creation of a new choice through the onCreate event', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ If you provided a React element for the optionText prop, you must also provide t
]);
}
} else {
field.onChange(getChoiceValue(newValue) || '');
field.onChange(getChoiceValue(newValue) ?? '');
}
};

Expand Down