From a5d2e5268c9f0b99b8d6cbdd4425f9f4aa37d20d Mon Sep 17 00:00:00 2001 From: Thomas Dekiere Date: Mon, 24 Jan 2022 16:45:49 +0100 Subject: [PATCH 1/3] fix: initialValue prop not used when input value is null fixes #7132 --- packages/ra-core/src/form/useInput.spec.tsx | 32 +++++++++++++++++++++ packages/ra-core/src/form/useInput.ts | 7 +++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/ra-core/src/form/useInput.spec.tsx b/packages/ra-core/src/form/useInput.spec.tsx index a6dc3a3b9f4..39645dd8915 100644 --- a/packages/ra-core/src/form/useInput.spec.tsx +++ b/packages/ra-core/src/form/useInput.spec.tsx @@ -206,6 +206,38 @@ describe('useInput', () => { expect(queryByDisplayValue('foo')).not.toBeNull(); }); + it('applies the initialValue when input has null 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( Date: Mon, 24 Jan 2022 19:54:32 +0100 Subject: [PATCH 2/3] use lodash get method to get the value of the record --- packages/ra-core/src/form/useInput.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ra-core/src/form/useInput.ts b/packages/ra-core/src/form/useInput.ts index b4c2d126a26..5fb77d79dc5 100644 --- a/packages/ra-core/src/form/useInput.ts +++ b/packages/ra-core/src/form/useInput.ts @@ -5,6 +5,7 @@ import { FieldRenderProps, FieldInputProps, } from 'react-final-form'; +import get from 'lodash/get'; import { Validator, composeValidators } from './validate'; import isRequired from './isRequired'; import { useFormGroupContext } from './useFormGroupContext'; @@ -73,7 +74,7 @@ const useInput = ({ // This ensure dynamically added inputs have their value set correctly (ArrayInput for example). // We don't do this for the form level initialValues so that it works as it should in final-form // (ie. field level initialValue override form level initialValues for this field). - let inputInitialValue = record ? record[source] : undefined; + let inputInitialValue = get(record, source); if (inputInitialValue === null || inputInitialValue === undefined) { inputInitialValue = initialValue; } From 82249c7bc53a12aa753c535e987f0b237b89a1ce Mon Sep 17 00:00:00 2001 From: Thomas Dekiere Date: Tue, 25 Jan 2022 07:22:46 +0100 Subject: [PATCH 3/3] use nullish coalescing operator to provide fallback value for null values --- packages/ra-core/src/form/useInput.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/ra-core/src/form/useInput.ts b/packages/ra-core/src/form/useInput.ts index 5fb77d79dc5..2d42ca61bdd 100644 --- a/packages/ra-core/src/form/useInput.ts +++ b/packages/ra-core/src/form/useInput.ts @@ -74,12 +74,9 @@ const useInput = ({ // This ensure dynamically added inputs have their value set correctly (ArrayInput for example). // We don't do this for the form level initialValues so that it works as it should in final-form // (ie. field level initialValue override form level initialValues for this field). - let inputInitialValue = get(record, source); - if (inputInitialValue === null || inputInitialValue === undefined) { - inputInitialValue = initialValue; - } + const inputInitialValue = get(record, source, initialValue); const { input, meta } = useFinalFormField(finalName, { - initialValue: inputInitialValue, + initialValue: inputInitialValue ?? initialValue, defaultValue, validate: sanitizedValidate, ...options,