Skip to content

Commit 474f249

Browse files
committed
Merge remote-tracking branch 'origin/main' into nested-object-conditional-defaul
2 parents 691af02 + bb64e56 commit 474f249

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ should change the heading of the (upcoming) version to include a major version b
2121
## @rjsf/utils
2222

2323
- Fixed issue with default value not being prefilled when object with if/then is nested inside another object, fixing [#4222](https://github.com/rjsf-team/react-jsonschema-form/issues/4222)
24+
- Fixed issue with schema array with nested dependent fixed-length, fixing [#3754](https://github.com/rjsf-team/react-jsonschema-form/issues/3754)
2425

2526

2627
# 6.1.2

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,14 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
619619
if (Array.isArray(defaults)) {
620620
defaults = defaults.map((item, idx) => {
621621
const schemaItem: S = getInnerSchemaForArrayItem<S>(schema, AdditionalItemsHandling.Fallback, idx);
622+
const itemFormData = Array.isArray(rawFormData) ? rawFormData[idx] : undefined;
622623
return computeDefaults<T, S, F>(validator, schemaItem, {
623624
rootSchema,
624625
_recurseList,
625626
experimental_defaultFormStateBehavior,
626627
experimental_customMergeAllOf,
627628
parentDefaults: item,
629+
rawFormData: itemFormData,
628630
required,
629631
shouldMergeDefaultsIntoFormData,
630632
initialDefaultsGenerated,

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,107 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
19471947
});
19481948
});
19491949

1950+
describe('array with nested dependent fixed-length array schema', () => {
1951+
const schema: RJSFSchema = {
1952+
items: [
1953+
{
1954+
dependencies: {
1955+
checkbox: {
1956+
if: {
1957+
properties: {
1958+
checkbox: {
1959+
const: true,
1960+
},
1961+
},
1962+
},
1963+
then: {
1964+
properties: {
1965+
inner_array: {
1966+
items: [
1967+
{
1968+
title: 'Fixed Item',
1969+
type: 'string',
1970+
},
1971+
],
1972+
title: 'Inner Fixed-Length Array',
1973+
type: 'array',
1974+
},
1975+
},
1976+
},
1977+
},
1978+
},
1979+
properties: {
1980+
checkbox: {
1981+
title: 'Dependency Checkbox',
1982+
type: 'boolean',
1983+
},
1984+
},
1985+
title: 'Outer Item',
1986+
type: 'object',
1987+
},
1988+
],
1989+
title: 'Outer Fixed Length Array',
1990+
type: 'array',
1991+
};
1992+
const formData = [{ checkbox: true }];
1993+
const includeUndefinedValues = 'excludeObjectChildren';
1994+
const expected = [
1995+
{
1996+
checkbox: true,
1997+
inner_array: [undefined],
1998+
},
1999+
];
2000+
2001+
test('getDefaultFormState', () => {
2002+
expect(getDefaultFormState(testValidator, schema, formData, undefined, includeUndefinedValues)).toEqual(
2003+
expected,
2004+
);
2005+
});
2006+
2007+
test('computeDefaults', () => {
2008+
expect(
2009+
computeDefaults(testValidator, schema, {
2010+
rawFormData: formData,
2011+
includeUndefinedValues,
2012+
shouldMergeDefaultsIntoFormData: true,
2013+
}),
2014+
).toEqual(expected);
2015+
});
2016+
2017+
test('getDefaultBasedOnSchemaType', () => {
2018+
expect(
2019+
getDefaultBasedOnSchemaType(
2020+
testValidator,
2021+
schema,
2022+
{
2023+
includeUndefinedValues,
2024+
},
2025+
[
2026+
{
2027+
checkbox: true,
2028+
},
2029+
],
2030+
),
2031+
).toEqual(expected);
2032+
});
2033+
2034+
test('getArrayDefaults', () => {
2035+
expect(
2036+
getArrayDefaults(
2037+
testValidator,
2038+
schema,
2039+
{
2040+
includeUndefinedValues,
2041+
},
2042+
[
2043+
{
2044+
checkbox: true,
2045+
},
2046+
],
2047+
),
2048+
).toEqual(expected);
2049+
});
2050+
});
19502051
describe('an invalid array schema', () => {
19512052
const schema: RJSFSchema = {
19522053
type: 'array',

0 commit comments

Comments
 (0)