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 AutocompleteArrayInput when value is not an array #8608

Merged
merged 1 commit into from
Jan 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -988,4 +988,43 @@ describe('<AutocompleteArrayInput />', () => {

expect(noOptionsAppeared).toBe(false);
});

it('should not crash if its value is not an array', () => {
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm
onSubmit={jest.fn()}
defaultValues={{ tags: 'programming' }}
>
<AutocompleteArrayInput
choices={[
{ id: 'programming', name: 'Programming' },
{ id: 'lifestyle', name: 'Lifestyle' },
{ id: 'photography', name: 'Photography' },
]}
{...defaultProps}
/>
</SimpleForm>
</AdminContext>
);
expect(screen.queryByRole('textbox')).not.toBeNull();
});

it('should not crash if its value is not an array and is empty', () => {
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={jest.fn()} defaultValues={{ tags: '' }}>
<AutocompleteArrayInput
choices={[
{ id: 'programming', name: 'Programming' },
{ id: 'lifestyle', name: 'Lifestyle' },
{ id: 'photography', name: 'Photography' },
]}
{...defaultProps}
/>
</SimpleForm>
</AdminContext>
);
expect(screen.queryByRole('textbox')).not.toBeNull();
});
});
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 @@ -742,7 +742,7 @@ const getSelectedItems = (
multiple
) => {
if (multiple) {
return (Array.isArray(value || []) ? value : [value])
return (Array.isArray(value ?? []) ? value : [value])
.map(item =>
choices.find(
choice => String(item) === String(get(choice, optionValue))
Expand Down