Skip to content

Commit 753a8bd

Browse files
committed
[core] Port fix for getArrayDefaults
1 parent 25c6ec8 commit 753a8bd

File tree

4 files changed

+75
-10
lines changed

4 files changed

+75
-10
lines changed

.changeset/cuddly-shoes-lick.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@sjsf/form": patch
3+
---
4+
5+
Port fix for `getArrayDefaults`
6+
7+
- [Bug: Issue with array schema defaults not applying properly when formData is an empty array](https://github.com/rjsf-team/react-jsonschema-form/pull/4359)

packages/form/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
### Patch Changes
6464

65-
- [`23f37ab`](https://github.com/x0k/svelte-jsonschema-form/commit/23f37abf7b928bfef45a45ab4a902660a139bfcd) Thanks [@x0k](https://github.com/x0k)! - Porst a bunch of fixes for `getDefaultFormState`:
65+
- [`23f37ab`](https://github.com/x0k/svelte-jsonschema-form/commit/23f37abf7b928bfef45a45ab4a902660a139bfcd) Thanks [@x0k](https://github.com/x0k)! - Port a bunch of fixes for `getDefaultFormState`:
6666

6767
- [Bug: issue with dependencies computeDefaults](https://github.com/rjsf-team/react-jsonschema-form/pull/4282)
6868
- [Make fields with const pre-fiiled and readonly](https://github.com/rjsf-team/react-jsonschema-form/pull/4326)

packages/form/src/core/default-state.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,51 @@ describe("getDefaultFormState2()", () => {
637637
},
638638
});
639639
});
640+
it("test an array with defaults with no formData", () => {
641+
const schema: Schema = {
642+
type: "array",
643+
minItems: 4,
644+
default: ["Raphael", "Michaelangelo"],
645+
items: {
646+
type: "string",
647+
default: "Unknown",
648+
},
649+
};
650+
651+
expect(
652+
computeDefaults3(testValidator, defaultMerger, schema, {
653+
...defaults,
654+
rootSchema: schema,
655+
includeUndefinedValues: "excludeObjectChildren",
656+
})
657+
).toEqual(["Raphael", "Michaelangelo", "Unknown", "Unknown"]);
658+
});
659+
it("test an array with defaults with empty array as formData", () => {
660+
const schema: Schema = {
661+
type: "array",
662+
minItems: 4,
663+
default: ["Raphael", "Michaelangelo"],
664+
items: {
665+
type: "string",
666+
default: "Unknown",
667+
},
668+
};
669+
670+
expect(
671+
computeDefaults3(testValidator, defaultMerger, schema, {
672+
...defaults,
673+
rootSchema: schema,
674+
rawFormData: [],
675+
includeUndefinedValues: "excludeObjectChildren",
676+
experimental_defaultFormStateBehavior: {
677+
arrayMinItems: {
678+
mergeExtraDefaults: true,
679+
populate: "all",
680+
},
681+
},
682+
})
683+
).toEqual(["Raphael", "Michaelangelo", "Unknown", "Unknown"]);
684+
});
640685
it("test computeDefaults handles an invalid property schema", () => {
641686
const schema: Schema = {
642687
type: "object",

packages/form/src/core/default-state.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -670,17 +670,20 @@ export function getArrayDefaults(
670670
}: ComputeDefaultsProps2,
671671
defaults: SchemaArrayValue | undefined
672672
): SchemaArrayValue | undefined {
673-
const neverPopulate =
674-
experimental_defaultFormStateBehavior?.arrayMinItems?.populate === "never";
675-
const ignoreMinItemsFlagSet =
676-
experimental_defaultFormStateBehavior?.arrayMinItems?.populate ===
677-
"requiredOnly";
673+
const {
674+
populate: arrayMinItemsPopulate,
675+
mergeExtraDefaults: arrayMergeExtraDefaults,
676+
computeSkipPopulate = () => false,
677+
} = experimental_defaultFormStateBehavior?.arrayMinItems ?? {};
678+
679+
const neverPopulate = arrayMinItemsPopulate === "never";
680+
const ignoreMinItemsFlagSet = arrayMinItemsPopulate === "requiredOnly";
681+
const isPopulateAll =
682+
arrayMinItemsPopulate === "all" ||
683+
(!neverPopulate && !ignoreMinItemsFlagSet);
678684
const isSkipEmptyDefaults =
679685
experimental_defaultFormStateBehavior?.emptyObjectFields ===
680686
"skipEmptyDefaults";
681-
const computeSkipPopulate =
682-
experimental_defaultFormStateBehavior?.arrayMinItems?.computeSkipPopulate ??
683-
(() => false);
684687

685688
const emptyDefault: SchemaArrayValue | undefined = isSkipEmptyDefaults
686689
? undefined
@@ -713,7 +716,7 @@ export function getArrayDefaults(
713716
if (neverPopulate) {
714717
defaults = rawFormData;
715718
} else {
716-
defaults = rawFormData.map((item, idx) => {
719+
const itemDefaults = rawFormData.map((item, idx) => {
717720
return computeDefaults3(validator, merger, schemaItem, {
718721
rootSchema,
719722
stack,
@@ -725,6 +728,16 @@ export function getArrayDefaults(
725728
isSchemaRoot: false,
726729
});
727730
});
731+
// If the populate 'requiredOnly' flag is set then we only merge and include extra defaults if they are required.
732+
// Or if populate 'all' is set we merge and include extra defaults.
733+
const mergeExtraDefaults =
734+
((ignoreMinItemsFlagSet && required) || isPopulateAll) &&
735+
arrayMergeExtraDefaults === true;
736+
defaults = mergeDefaultsWithFormData(
737+
defaults,
738+
itemDefaults,
739+
mergeExtraDefaults
740+
);
728741
}
729742
}
730743

0 commit comments

Comments
 (0)