From 103afbc87fd808a549e9879ceb4dd4c7f99f0856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C3=ADbal=20Svarcas?= Date: Tue, 6 Sep 2022 08:52:11 -0300 Subject: [PATCH 1/3] Fix allow zero as value in AutocompleteInput --- packages/ra-ui-materialui/src/input/AutocompleteInput.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx index 5026c23a9a9..cfc7aedf909 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx @@ -278,7 +278,8 @@ If you provided a React element for the optionText prop, you must also provide t ]); } } else { - field.onChange(getChoiceValue(newValue) || ''); + const finalValue = getChoiceValue(newValue); + field.onChange(finalValue ?? ''); } }; From 02bdac24c597d915e41b5e17afceab38a2d1caa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C3=ADbal=20Svarcas?= Date: Tue, 6 Sep 2022 08:58:09 -0300 Subject: [PATCH 2/3] Simplify expression --- packages/ra-ui-materialui/src/input/AutocompleteInput.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx index cfc7aedf909..5f01ebbe1fd 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx @@ -278,8 +278,7 @@ If you provided a React element for the optionText prop, you must also provide t ]); } } else { - const finalValue = getChoiceValue(newValue); - field.onChange(finalValue ?? ''); + field.onChange(getChoiceValue(newValue) ?? ''); } }; From 462b5a833ac446e1e586411f973fbe87cd980734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C3=ADbal=20Svarcas?= Date: Tue, 6 Sep 2022 11:43:03 -0300 Subject: [PATCH 3/3] Fix test --- .../src/input/AutocompleteInput.spec.tsx | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx index e153104e22d..ef01ff4dad4 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx @@ -901,10 +901,10 @@ describe('', () => { ); }); - it('should accept 0 as an input value', () => { + it('should accept 0 as an input value', async () => { render( - + ', () => { ); - 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 () => {