Skip to content

Commit 5ae35dc

Browse files
authored
1 parent a80cf89 commit 5ae35dc

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

.changeset/itchy-donkeys-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/form": patch
3+
---
4+
5+
Port https://github.com/rjsf-team/react-jsonschema-form/pull/4860

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

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,166 @@ describe("getDefaultFormState()", () => {
28232823
});
28242824
});
28252825

2826+
describe("array with nested dependent fixed-length array schema", () => {
2827+
const schema: Schema = {
2828+
items: [
2829+
{
2830+
dependencies: {
2831+
checkbox: {
2832+
if: {
2833+
properties: {
2834+
checkbox: {
2835+
const: true,
2836+
},
2837+
},
2838+
},
2839+
then: {
2840+
properties: {
2841+
inner_array: {
2842+
items: [
2843+
{
2844+
title: "Fixed Item",
2845+
type: "string",
2846+
},
2847+
],
2848+
title: "Inner Fixed-Length Array",
2849+
type: "array",
2850+
},
2851+
},
2852+
},
2853+
},
2854+
},
2855+
properties: {
2856+
checkbox: {
2857+
title: "Dependency Checkbox",
2858+
type: "boolean",
2859+
},
2860+
},
2861+
title: "Outer Item",
2862+
type: "object",
2863+
},
2864+
],
2865+
title: "Outer Fixed Length Array",
2866+
type: "array",
2867+
};
2868+
const formData = [{ checkbox: true }];
2869+
const includeUndefinedValues = "excludeObjectChildren";
2870+
const expected = [
2871+
{
2872+
checkbox: true,
2873+
inner_array: [undefined],
2874+
},
2875+
];
2876+
2877+
beforeEach(() => {
2878+
testValidator = createValidator({
2879+
cases: [
2880+
{
2881+
schema: { properties: { checkbox: { const: true } } },
2882+
value: { checkbox: true },
2883+
result: true,
2884+
},
2885+
],
2886+
});
2887+
defaultMerger = createMerger({
2888+
merges: [
2889+
{
2890+
left: {
2891+
properties: {
2892+
checkbox: { title: "Dependency Checkbox", type: "boolean" },
2893+
},
2894+
title: "Outer Item",
2895+
type: "object",
2896+
},
2897+
right: {
2898+
properties: {
2899+
inner_array: {
2900+
items: [{ title: "Fixed Item", type: "string" }],
2901+
title: "Inner Fixed-Length Array",
2902+
type: "array",
2903+
},
2904+
},
2905+
},
2906+
result: {
2907+
properties: {
2908+
checkbox: { title: "Dependency Checkbox", type: "boolean" },
2909+
inner_array: {
2910+
items: [{ title: "Fixed Item", type: "string" }],
2911+
title: "Inner Fixed-Length Array",
2912+
type: "array",
2913+
},
2914+
},
2915+
title: "Outer Item",
2916+
type: "object",
2917+
},
2918+
},
2919+
],
2920+
});
2921+
});
2922+
2923+
test("getDefaultFormState", () => {
2924+
expect(
2925+
getDefaultFormState(
2926+
testValidator,
2927+
defaultMerger,
2928+
schema,
2929+
formData,
2930+
undefined,
2931+
includeUndefinedValues
2932+
)
2933+
).toEqual(expected);
2934+
});
2935+
2936+
test("computeDefaults", () => {
2937+
expect(
2938+
computeDefaults(testValidator, defaultMerger, schema, {
2939+
...defaults,
2940+
rawFormData: formData,
2941+
includeUndefinedValues,
2942+
shouldMergeDefaultsIntoFormData: true,
2943+
})
2944+
).toEqual(expected);
2945+
});
2946+
2947+
test("getDefaultBasedOnSchemaType", () => {
2948+
expect(
2949+
getDefaultBasedOnSchemaType(
2950+
testValidator,
2951+
defaultMerger,
2952+
schema,
2953+
{
2954+
...defaults,
2955+
includeUndefinedValues,
2956+
},
2957+
[
2958+
{
2959+
checkbox: true,
2960+
},
2961+
]
2962+
)
2963+
).toEqual(expected);
2964+
});
2965+
2966+
test("getArrayDefaults", () => {
2967+
expect(
2968+
getArrayDefaults(
2969+
testValidator,
2970+
defaultMerger,
2971+
schema,
2972+
{
2973+
...defaults,
2974+
includeUndefinedValues,
2975+
},
2976+
[
2977+
{
2978+
checkbox: true,
2979+
},
2980+
]
2981+
)
2982+
).toEqual(expected);
2983+
});
2984+
});
2985+
28262986
describe("an invalid array schema", () => {
28272987
const schema: Schema = {
28282988
type: "array",

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,14 +832,17 @@ export function getArrayDefaults(
832832
AdditionalItemsHandling.Fallback,
833833
idx
834834
);
835+
const itemFormData = Array.isArray(rawFormData)
836+
? rawFormData[idx]
837+
: undefined;
835838
return computeDefaults(validator, merger, schemaItem, {
836839
rootSchema,
837840
stack,
838841
experimental_defaultFormStateBehavior,
839842
parentDefaults: item,
840843
required,
841844
includeUndefinedValues: false,
842-
rawFormData: undefined,
845+
rawFormData: itemFormData,
843846
isSchemaRoot: false,
844847
shouldMergeDefaultsIntoFormData,
845848
initialDefaultsGenerated,

0 commit comments

Comments
 (0)