@rjsf/utils 6.0.0-beta.10 → 6.0.0-beta.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -63,6 +63,14 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
63
63
  this.experimental_customMergeAllOf = experimental_customMergeAllOf;
64
64
  }
65
65
 
66
+ /** Returns the `rootSchema` in the `SchemaUtilsType`
67
+ *
68
+ * @returns - The `rootSchema`
69
+ */
70
+ getRootSchema() {
71
+ return this.rootSchema;
72
+ }
73
+
66
74
  /** Returns the `ValidatorType` in the `SchemaUtilsType`
67
75
  *
68
76
  * @returns - The `ValidatorType`
@@ -67,8 +67,22 @@ export default function mergeDefaultsWithFormData<T = any>(
67
67
  const keyValue = get(formData, key);
68
68
  const keyExistsInDefaults = isObject(defaults) && key in (defaults as GenericObjectType);
69
69
  const keyExistsInFormData = key in (formData as GenericObjectType);
70
+ const keyDefault = get(defaults, key) ?? {};
71
+ const defaultValueIsNestedObject = keyExistsInDefaults && Object.entries(keyDefault).some(([, v]) => isObject(v));
72
+
73
+ const keyDefaultIsObject = keyExistsInDefaults && isObject(get(defaults, key));
74
+ const keyHasFormDataObject = keyExistsInFormData && isObject(keyValue);
75
+
76
+ if (keyDefaultIsObject && keyHasFormDataObject && !defaultValueIsNestedObject) {
77
+ acc[key as keyof T] = {
78
+ ...get(defaults, key),
79
+ ...keyValue,
80
+ };
81
+ return acc;
82
+ }
83
+
70
84
  acc[key as keyof T] = mergeDefaultsWithFormData<T>(
71
- defaults ? get(defaults, key) : {},
85
+ get(defaults, key) ?? {},
72
86
  keyValue,
73
87
  mergeExtraArrayDefaults,
74
88
  defaultSupercedesUndefined,
@@ -213,7 +213,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
213
213
  let experimental_dfsb_to_compute = experimental_defaultFormStateBehavior;
214
214
  let updatedRecurseList = _recurseList;
215
215
  if (
216
- schema[CONST_KEY] &&
216
+ schema[CONST_KEY] !== undefined &&
217
217
  experimental_defaultFormStateBehavior?.constAsDefaults !== 'never' &&
218
218
  !constIsAjvDataReference(schema)
219
219
  ) {
@@ -334,7 +334,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
334
334
  experimental_defaultFormStateBehavior: experimental_dfsb_to_compute,
335
335
  experimental_customMergeAllOf,
336
336
  parentDefaults: defaults as T | undefined,
337
- rawFormData: formData as T,
337
+ rawFormData: (rawFormData ?? formData) as T,
338
338
  required,
339
339
  shouldMergeDefaultsIntoFormData,
340
340
  });
@@ -728,6 +728,13 @@ export default function getDefaultFormState<
728
728
  shouldMergeDefaultsIntoFormData: true,
729
729
  });
730
730
 
731
+ if (schema.type !== 'object' && isObject(schema.default)) {
732
+ return {
733
+ ...defaults,
734
+ ...formData,
735
+ } as T;
736
+ }
737
+
731
738
  // If the formData is an object or an array, add additional properties from formData and override formData with
732
739
  // defaults since the defaults are already merged with formData.
733
740
  if (isObject(formData) || Array.isArray(formData)) {
package/src/types.ts CHANGED
@@ -341,6 +341,8 @@ export type TemplatesType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
341
341
  FieldTemplate: ComponentType<FieldTemplateProps<T, S, F>>;
342
342
  /** The template to use to render a Grid element */
343
343
  GridTemplate: ComponentType<GridTemplateProps>;
344
+ /** The template to use while rendering a multi-schema field (i.e. anyOf, oneOf) */
345
+ MultiSchemaFieldTemplate: ComponentType<MultiSchemaFieldTemplateProps<T, S, F>>;
344
346
  /** The template to use while rendering an object */
345
347
  ObjectFieldTemplate: ComponentType<ObjectFieldTemplateProps<T, S, F>>;
346
348
  /** The template to use for rendering the title of a field */
@@ -777,6 +779,18 @@ export type WrapIfAdditionalTemplateProps<
777
779
  | 'registry'
778
780
  >;
779
781
 
782
+ /** The properties that are passed to a MultiSchemaFieldTemplate implementation */
783
+ export interface MultiSchemaFieldTemplateProps<
784
+ T = any,
785
+ S extends StrictRJSFSchema = RJSFSchema,
786
+ F extends FormContextType = any,
787
+ > extends RJSFBaseProps<T, S, F> {
788
+ /** The rendered widget used to select a schema option */
789
+ selector: ReactNode;
790
+ /** The rendered SchemaField for the selected schema option */
791
+ optionSchemaField: ReactNode;
792
+ }
793
+
780
794
  /** The properties that are passed to a Widget implementation */
781
795
  export interface WidgetProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>
782
796
  extends GenericObjectType,
@@ -1100,6 +1114,11 @@ export interface FoundFieldType<S extends StrictRJSFSchema = RJSFSchema> {
1100
1114
  * set of APIs to the `@rjsf/core` components and the various themes as well.
1101
1115
  */
1102
1116
  export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> {
1117
+ /** Returns the `rootSchema` in the `SchemaUtilsType`
1118
+ *
1119
+ * @returns - The rootSchema
1120
+ */
1121
+ getRootSchema(): S;
1103
1122
  /** Returns the `ValidatorType` in the `SchemaUtilsType`
1104
1123
  *
1105
1124
  * @returns - The `ValidatorType`