diff --git a/CHANGELOG.md b/CHANGELOG.md index e439b5367f..f129418864 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,13 @@ should change the heading of the (upcoming) version to include a major version b --> +# 6.1.3 + +## @rjsf/utils + +- Fixed issue with schema array with nested dependent fixed-length, fixing [#3754](https://github.com/rjsf-team/react-jsonschema-form/issues/3754) + + # 6.1.2 ## @rjsf/antd diff --git a/packages/utils/src/schema/getDefaultFormState.ts b/packages/utils/src/schema/getDefaultFormState.ts index e14877bcb9..6de034230e 100644 --- a/packages/utils/src/schema/getDefaultFormState.ts +++ b/packages/utils/src/schema/getDefaultFormState.ts @@ -614,12 +614,14 @@ export function getArrayDefaults { const schemaItem: S = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Fallback, idx); + const itemFormData = Array.isArray(rawFormData) ? rawFormData[idx] : undefined; return computeDefaults(validator, schemaItem, { rootSchema, _recurseList, experimental_defaultFormStateBehavior, experimental_customMergeAllOf, parentDefaults: item, + rawFormData: itemFormData, required, shouldMergeDefaultsIntoFormData, initialDefaultsGenerated, diff --git a/packages/utils/test/schema/getDefaultFormStateTest.ts b/packages/utils/test/schema/getDefaultFormStateTest.ts index ce7b18c2bf..fbdbafa595 100644 --- a/packages/utils/test/schema/getDefaultFormStateTest.ts +++ b/packages/utils/test/schema/getDefaultFormStateTest.ts @@ -1870,6 +1870,107 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType }); }); + describe('array with nested dependent fixed-length array schema', () => { + const schema: RJSFSchema = { + items: [ + { + dependencies: { + checkbox: { + if: { + properties: { + checkbox: { + const: true, + }, + }, + }, + then: { + properties: { + inner_array: { + items: [ + { + title: 'Fixed Item', + type: 'string', + }, + ], + title: 'Inner Fixed-Length Array', + type: 'array', + }, + }, + }, + }, + }, + properties: { + checkbox: { + title: 'Dependency Checkbox', + type: 'boolean', + }, + }, + title: 'Outer Item', + type: 'object', + }, + ], + title: 'Outer Fixed Length Array', + type: 'array', + }; + const formData = [{ checkbox: true }]; + const includeUndefinedValues = 'excludeObjectChildren'; + const expected = [ + { + checkbox: true, + inner_array: [undefined], + }, + ]; + + test('getDefaultFormState', () => { + expect(getDefaultFormState(testValidator, schema, formData, undefined, includeUndefinedValues)).toEqual( + expected, + ); + }); + + test('computeDefaults', () => { + expect( + computeDefaults(testValidator, schema, { + rawFormData: formData, + includeUndefinedValues, + shouldMergeDefaultsIntoFormData: true, + }), + ).toEqual(expected); + }); + + test('getDefaultBasedOnSchemaType', () => { + expect( + getDefaultBasedOnSchemaType( + testValidator, + schema, + { + includeUndefinedValues, + }, + [ + { + checkbox: true, + }, + ], + ), + ).toEqual(expected); + }); + + test('getArrayDefaults', () => { + expect( + getArrayDefaults( + testValidator, + schema, + { + includeUndefinedValues, + }, + [ + { + checkbox: true, + }, + ], + ), + ).toEqual(expected); + }); + }); describe('an invalid array schema', () => { const schema: RJSFSchema = { type: 'array',