From 0da0ea6c865b3a5c6d27d82c74f5b8a66459478e Mon Sep 17 00:00:00 2001 From: Pedro Bini Date: Wed, 1 Jul 2020 14:20:12 -0300 Subject: [PATCH 1/2] Merge initialTouched generated object with options.initialTouched --- packages/core/src/utils/mapInitialTouched.ts | 25 +++++++++++--------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/core/src/utils/mapInitialTouched.ts b/packages/core/src/utils/mapInitialTouched.ts index c69e47a..65a0125 100644 --- a/packages/core/src/utils/mapInitialTouched.ts +++ b/packages/core/src/utils/mapInitialTouched.ts @@ -1,3 +1,5 @@ +import merge from 'merge'; + /** * Returns true if one value is possibly touched. * @@ -18,7 +20,11 @@ const evalIsTouched = (value: any) => !!( * Recursively maps touched fields in an object. * @param obj The object */ -const recursiveMapTouched = (obj: object): object => { +const recursiveMapTouched = (obj?: object): object => { + if (!obj) { + return {}; + } + const result = { ...(obj || {}), }; @@ -47,21 +53,18 @@ const recursiveMapTouched = (obj: object): object => { * which is optional. * * @param initialValues Value of initialValues - * @param initialTouched Value of initialTouched + * @param override Value of initialTouched */ const mapInitialTouched = ( initialValues?: object, - initialTouched?: object, + override?: object, ) => { - if (initialTouched) { - return initialTouched; - } - - if (!initialValues) { - return undefined; - } + const touchedFromInitialValues = recursiveMapTouched(initialValues); - return recursiveMapTouched(initialValues); + return merge.recursive( + (touchedFromInitialValues || {}), + (override || {}), + ); }; export default mapInitialTouched; From 405cc34691e77aa076e9d1032b86e08784aa156f Mon Sep 17 00:00:00 2001 From: Pedro Bini Date: Wed, 1 Jul 2020 14:20:57 -0300 Subject: [PATCH 2/2] Remove unnecessary coalesce --- packages/core/src/utils/mapInitialTouched.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/core/src/utils/mapInitialTouched.ts b/packages/core/src/utils/mapInitialTouched.ts index 65a0125..7cd0d98 100644 --- a/packages/core/src/utils/mapInitialTouched.ts +++ b/packages/core/src/utils/mapInitialTouched.ts @@ -21,10 +21,6 @@ const evalIsTouched = (value: any) => !!( * @param obj The object */ const recursiveMapTouched = (obj?: object): object => { - if (!obj) { - return {}; - } - const result = { ...(obj || {}), };