You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/utils/src/mergeDefaultsWithFormData.ts
+33-11Lines changed: 33 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -19,47 +19,69 @@ import { GenericObjectType } from '../src';
19
19
* @param [formData] - The form data into which the defaults will be merged
20
20
* @param [mergeExtraArrayDefaults=false] - If true, any additional default array entries are appended onto the formData
21
21
* @param [defaultSupercedesUndefined=false] - If true, an explicit undefined value will be overwritten by the default value
22
+
* @param [overrideFormDataWithDefaults=false] - If true, the default value will overwrite the form data value. If the value doesn't exist in the default, we take it from formData and in case where the value is set to undefined in formData. This is useful when we have already merged formData with defaults and want to add an additional field from formData that does not exist in defaults.
22
23
* @returns - The resulting merged form data with defaults
/** Optional flag, if true, indicates this schema was required in the parent schema. */
170
173
required?: boolean;
174
+
/** Optional flag, if true, It will merge defaults into formData.
175
+
* The formData should take precedence unless it's not valid. This is useful when for example the value from formData does not exist in the schema 'enum' property, in such cases we take the value from the defaults because the value from the formData is not valid.
176
+
*/
177
+
shouldMergeDefaultsIntoFormData?: boolean;
171
178
}
172
179
173
180
/** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
@@ -192,6 +199,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
// Get the computed defaults with 'shouldMergeDefaultsIntoFormData' set to true to merge defaults into formData.
646
+
// This is done when for example the value from formData does not exist in the schema 'enum' property, in such cases we take the value from the defaults because the value from the formData is not valid.
// If the formData is an object or an array, add additional properties from formData and override formData with defaults since the defaults are already merged with formData.
0 commit comments