Skip to content

Commit b767f5a

Browse files
authored
Merge pull request #8144 from marmelab/fix-zero-in-autocompleteinput
Fix allow zero as value in AutocompleteInput
2 parents fbbec14 + 462b5a8 commit b767f5a

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx

+17-3
Original file line numberDiff line numberDiff line change
@@ -901,18 +901,32 @@ describe('<AutocompleteInput />', () => {
901901
);
902902
});
903903

904-
it('should accept 0 as an input value', () => {
904+
it('should accept 0 as an input value', async () => {
905905
render(
906906
<AdminContext dataProvider={testDataProvider()}>
907-
<SimpleForm onSubmit={jest.fn()} defaultValues={{ role: 0 }}>
907+
<SimpleForm onSubmit={jest.fn()}>
908908
<AutocompleteInput
909909
{...defaultProps}
910910
choices={[{ id: 0, name: 'foo' }]}
911911
/>
912912
</SimpleForm>
913913
</AdminContext>
914914
);
915-
expect(screen.queryByDisplayValue('foo')).not.toBeNull();
915+
const input = screen.getByLabelText(
916+
'resources.users.fields.role'
917+
) as HTMLInputElement;
918+
input.focus();
919+
fireEvent.change(input, { target: { value: 'foo' } });
920+
await waitFor(
921+
() => {
922+
expect(screen.getByRole('listbox').children).toHaveLength(1);
923+
},
924+
{ timeout: 2000 }
925+
);
926+
fireEvent.click(screen.getByText('foo'));
927+
await waitFor(() => {
928+
expect(input.value).toEqual('foo');
929+
});
916930
});
917931

918932
it('should support creation of a new choice through the onCreate event', async () => {

packages/ra-ui-materialui/src/input/AutocompleteInput.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ If you provided a React element for the optionText prop, you must also provide t
278278
]);
279279
}
280280
} else {
281-
field.onChange(getChoiceValue(newValue) || '');
281+
field.onChange(getChoiceValue(newValue) ?? '');
282282
}
283283
};
284284

0 commit comments

Comments
 (0)