Skip to content

Commit 744c4e7

Browse files
committed
Fixed issue with schema array with nested dependent fixed-length.
1 parent 619eead commit 744c4e7

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

CHANGELOG.md

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

7070
- Added support for rendering `ui:help` as markdown via new `ui:enableMarkdownInHelp` flag in `FieldHelpTemplate`
71+
- Fixed issue with schema array with nested dependent fixed-length, fixing [#3754](https://github.com/rjsf-team/react-jsonschema-form/issues/3754)
7172

7273

7374
# 6.0.2

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,14 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
614614
if (Array.isArray(defaults)) {
615615
defaults = defaults.map((item, idx) => {
616616
const schemaItem: S = getInnerSchemaForArrayItem<S>(schema, AdditionalItemsHandling.Fallback, idx);
617+
const itemFormData = Array.isArray(rawFormData) ? rawFormData[idx] : undefined;
617618
return computeDefaults<T, S, F>(validator, schemaItem, {
618619
rootSchema,
619620
_recurseList,
620621
experimental_defaultFormStateBehavior,
621622
experimental_customMergeAllOf,
622623
parentDefaults: item,
624+
rawFormData: itemFormData,
623625
required,
624626
shouldMergeDefaultsIntoFormData,
625627
initialDefaultsGenerated,

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,110 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
18701870
});
18711871
});
18721872

1873+
describe('array with nested dependent fixed-length array schema', () => {
1874+
const schema: RJSFSchema = {
1875+
items: [
1876+
{
1877+
dependencies: {
1878+
checkbox: {
1879+
if: {
1880+
properties: {
1881+
checkbox: {
1882+
const: true,
1883+
},
1884+
},
1885+
},
1886+
then: {
1887+
properties: {
1888+
inner_array: {
1889+
items: [
1890+
{
1891+
title: 'Fixed Item',
1892+
type: 'string',
1893+
},
1894+
],
1895+
title: 'Inner Fixed-Length Array',
1896+
type: 'array',
1897+
},
1898+
},
1899+
},
1900+
},
1901+
},
1902+
properties: {
1903+
checkbox: {
1904+
title: 'Dependency Checkbox',
1905+
type: 'boolean',
1906+
},
1907+
},
1908+
title: 'Outer Item',
1909+
type: 'object',
1910+
},
1911+
],
1912+
title: 'Outer Fixed Length Array',
1913+
type: 'array',
1914+
};
1915+
const formData = [{ checkbox: true }];
1916+
const includeUndefinedValues = 'excludeObjectChildren';
1917+
const expected = [
1918+
{
1919+
checkbox: true,
1920+
inner_array: [undefined],
1921+
},
1922+
];
1923+
1924+
test('getDefaultFormState', () => {
1925+
expect(getDefaultFormState(testValidator, schema, formData, schema, includeUndefinedValues)).toEqual(
1926+
expected,
1927+
);
1928+
});
1929+
1930+
test('computeDefaults', () => {
1931+
expect(
1932+
computeDefaults(testValidator, schema, {
1933+
rootSchema: schema,
1934+
rawFormData: formData,
1935+
includeUndefinedValues,
1936+
shouldMergeDefaultsIntoFormData: true,
1937+
}),
1938+
).toEqual(expected);
1939+
});
1940+
1941+
test('getDefaultBasedOnSchemaType', () => {
1942+
expect(
1943+
getDefaultBasedOnSchemaType(
1944+
testValidator,
1945+
schema,
1946+
{
1947+
rootSchema: schema,
1948+
includeUndefinedValues,
1949+
},
1950+
[
1951+
{
1952+
checkbox: true,
1953+
},
1954+
],
1955+
),
1956+
).toEqual(expected);
1957+
});
1958+
1959+
test('getArrayDefaults', () => {
1960+
expect(
1961+
getArrayDefaults(
1962+
testValidator,
1963+
schema,
1964+
{
1965+
rootSchema: schema,
1966+
includeUndefinedValues,
1967+
},
1968+
[
1969+
{
1970+
checkbox: true,
1971+
},
1972+
],
1973+
),
1974+
).toEqual(expected);
1975+
});
1976+
});
18731977
describe('an invalid array schema', () => {
18741978
const schema: RJSFSchema = {
18751979
type: 'array',

0 commit comments

Comments
 (0)