From 54669cfdbae4a15fcefe705ef4800115a8723767 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Mon, 21 Jun 2021 10:28:58 +0200 Subject: [PATCH] Fix useInput incorrectly set default value for numbers --- packages/ra-core/src/form/useInput.spec.tsx | 121 +++++++++++++++++++- packages/ra-core/src/form/useInput.ts | 6 +- 2 files changed, 123 insertions(+), 4 deletions(-) diff --git a/packages/ra-core/src/form/useInput.spec.tsx b/packages/ra-core/src/form/useInput.spec.tsx index 9cf6f086cb1..8b380691e20 100644 --- a/packages/ra-core/src/form/useInput.spec.tsx +++ b/packages/ra-core/src/form/useInput.spec.tsx @@ -2,9 +2,10 @@ import * as React from 'react'; import { FunctionComponent, ReactElement } from 'react'; import { render, fireEvent } from '@testing-library/react'; import { Form } from 'react-final-form'; - +import FormWithRedirect from './FormWithRedirect'; import useInput, { InputProps } from './useInput'; import { required } from './validate'; +import { renderWithRedux } from '../../../ra-test/esm'; const Input: FunctionComponent< { children: (props: any) => ReactElement } & InputProps @@ -116,4 +117,122 @@ describe('useInput', () => { expect(handleBlur).toHaveBeenCalled(); expect(formApi.getState().active).toBeUndefined(); }); + + it('applies the defaultValue when input does not have a value', () => { + const { queryByDisplayValue } = renderWithRedux( + { + return ( + + {({ id, input }) => { + return ( + + ); + }} + + ); + }} + /> + ); + expect(queryByDisplayValue('foo')).not.toBeNull(); + }); + + it('does not apply the defaultValue when input has a value of 0', () => { + const { queryByDisplayValue } = renderWithRedux( + { + return ( + + {({ id, input }) => { + return ( + + ); + }} + + ); + }} + /> + ); + expect(queryByDisplayValue('99')).toBeNull(); + }); + + it('applies the initialValue when input does not have a value', () => { + const { queryByDisplayValue } = renderWithRedux( + { + return ( + + {({ id, input }) => { + return ( + + ); + }} + + ); + }} + /> + ); + expect(queryByDisplayValue('foo')).not.toBeNull(); + }); + + it('does not apply the initialValue when input has a value of 0', () => { + const { queryByDisplayValue } = renderWithRedux( + { + return ( + + {({ id, input }) => { + return ( + + ); + }} + + ); + }} + /> + ); + expect(queryByDisplayValue('99')).toBeNull(); + }); }); diff --git a/packages/ra-core/src/form/useInput.ts b/packages/ra-core/src/form/useInput.ts index f8e02520c68..348fcdda916 100644 --- a/packages/ra-core/src/form/useInput.ts +++ b/packages/ra-core/src/form/useInput.ts @@ -115,17 +115,17 @@ const useInput = ({ const form = useForm(); const recordId = record?.id; useEffect(() => { - if (!!input.value) { + if (input.value != null && input.value !== '') { return; } // Apply the default value if provided // We use a change here which will make the form dirty but this is expected // and identical to what FinalForm does (https://final-form.org/docs/final-form/types/FieldConfig#defaultvalue) - if (!!defaultValue) { + if (defaultValue != null) { form.change(source, defaultValue); } - if (!!initialValue) { + if (initialValue != null) { form.batch(() => { form.change(source, initialValue); form.resetFieldState(source);