Skip to content

Commit 895e480

Browse files
committed
improvement based on feedback.
1 parent 6817fa0 commit 895e480

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

packages/utils/src/mergeDefaultsWithFormData.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,20 @@ export default function mergeDefaultsWithFormData<T = any>(
7878
return acc;
7979
}, acc);
8080
}
81+
82+
/**
83+
* If the defaultSupercedesUndefined flag is true
84+
* And formData is set to undefined or null and defaults are defined
85+
* Or if formData is a number and is NaN return defaults
86+
* Or if overrideFormDataWithDefaults flag is true and formData is set to not undefined/null return defaults
87+
*/
8188
if (
82-
defaultSupercedesUndefined &&
83-
((!isNil(defaults) && isNil(formData)) || (typeof formData === 'number' && isNaN(formData)))
89+
(defaultSupercedesUndefined &&
90+
((!isNil(defaults) && isNil(formData)) || (typeof formData === 'number' && isNaN(formData)))) ||
91+
(overrideFormDataWithDefaults && !isNil(formData))
8492
) {
8593
return defaults;
86-
} else if (overrideFormDataWithDefaults && isNil(formData)) {
87-
// If the overrideFormDataWithDefaults flag is true and formData is set to undefined or null return formData
88-
return formData;
8994
}
90-
return overrideFormDataWithDefaults ? defaults : formData;
95+
96+
return formData;
9197
}

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
354354

355355
/**
356356
* Ensure that the formData matches the given schema. If it's not matching in the case of a selectField, we change it to match the schema.
357+
*
357358
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
358359
* @param schema - The schema for which the formData state is desired
359360
* @param rootSchema - The root schema, used to primarily to look up `$ref`s
@@ -383,7 +384,7 @@ export function ensureFormDataMatchingSchema<
383384
// Override the formData with the const if the constAsDefaults is set to always
384385
const constTakesPrecedence = schema[CONST_KEY] && experimental_defaultFormStateBehavior?.constAsDefaults === 'always';
385386
if (constTakesPrecedence) {
386-
validFormData = schema.const as any;
387+
validFormData = schema.const as T;
387388
}
388389

389390
return validFormData;
@@ -704,8 +705,8 @@ export default function getDefaultFormState<
704705
if (isObject(formData) || Array.isArray(formData)) {
705706
const { mergeDefaultsIntoFormData } = experimental_defaultFormStateBehavior || {};
706707
const defaultSupercedesUndefined = mergeDefaultsIntoFormData === 'useDefaultIfFormDataUndefined';
707-
const result = mergeDefaultsWithFormData<T>(
708-
defaults as T,
708+
const result = mergeDefaultsWithFormData<T | T[]>(
709+
defaults,
709710
formData,
710711
true, // set to true to add any additional default array entries.
711712
defaultSupercedesUndefined,

0 commit comments

Comments
 (0)