@rjsf/utils 5.22.2 → 5.22.4

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.
@@ -219,8 +219,8 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
219
219
  } else if (DEPENDENCIES_KEY in schema) {
220
220
  // Get the default if set from properties to ensure the dependencies conditions are resolved based on it
221
221
  const defaultFormData: T = {
222
- ...formData,
223
222
  ...getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults),
223
+ ...formData,
224
224
  };
225
225
  const resolvedSchema = resolveDependencies<T, S, F>(
226
226
  validator,
@@ -432,11 +432,14 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
432
432
  ): T | T[] | undefined {
433
433
  const schema: S = rawSchema;
434
434
 
435
- const neverPopulate = experimental_defaultFormStateBehavior?.arrayMinItems?.populate === 'never';
436
- const ignoreMinItemsFlagSet = experimental_defaultFormStateBehavior?.arrayMinItems?.populate === 'requiredOnly';
435
+ const arrayMinItemsStateBehavior = experimental_defaultFormStateBehavior?.arrayMinItems ?? {};
436
+ const { populate: arrayMinItemsPopulate, mergeExtraDefaults: arrayMergeExtraDefaults } = arrayMinItemsStateBehavior;
437
+
438
+ const neverPopulate = arrayMinItemsPopulate === 'never';
439
+ const ignoreMinItemsFlagSet = arrayMinItemsPopulate === 'requiredOnly';
440
+ const isPopulateAll = arrayMinItemsPopulate === 'all' || (!neverPopulate && !ignoreMinItemsFlagSet);
441
+ const computeSkipPopulate = arrayMinItemsStateBehavior?.computeSkipPopulate ?? (() => false);
437
442
  const isSkipEmptyDefaults = experimental_defaultFormStateBehavior?.emptyObjectFields === 'skipEmptyDefaults';
438
- const computeSkipPopulate =
439
- experimental_defaultFormStateBehavior?.arrayMinItems?.computeSkipPopulate ?? (() => false);
440
443
 
441
444
  const emptyDefault = isSkipEmptyDefaults ? undefined : [];
442
445
 
@@ -460,7 +463,7 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
460
463
  if (neverPopulate) {
461
464
  defaults = rawFormData;
462
465
  } else {
463
- defaults = rawFormData.map((item: T, idx: number) => {
466
+ const itemDefaults = rawFormData.map((item: T, idx: number) => {
464
467
  return computeDefaults<T, S, F>(validator, schemaItem, {
465
468
  rootSchema,
466
469
  _recurseList,
@@ -470,6 +473,11 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
470
473
  required,
471
474
  });
472
475
  }) as T[];
476
+
477
+ // If the populate 'requiredOnly' flag is set then we only merge and include extra defaults if they are required.
478
+ // Or if populate 'all' is set we merge and include extra defaults.
479
+ const mergeExtraDefaults = ((ignoreMinItemsFlagSet && required) || isPopulateAll) && arrayMergeExtraDefaults;
480
+ defaults = mergeDefaultsWithFormData(defaults, itemDefaults, mergeExtraDefaults);
473
481
  }
474
482
  }
475
483