Skip to content

Commit 265fb7d

Browse files
authored
1 parent 5ae35dc commit 265fb7d

File tree

3 files changed

+167
-7
lines changed

3 files changed

+167
-7
lines changed

.changeset/humble-spoons-smell.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/4889

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

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,6 +2535,154 @@ describe("getDefaultFormState()", () => {
25352535
).toEqual(expected);
25362536
});
25372537
});
2538+
2539+
describe("an nested object with if/then condition", () => {
2540+
const schema: Schema = {
2541+
type: "object",
2542+
properties: {
2543+
wrappingObject: {
2544+
type: "object",
2545+
properties: {
2546+
checkbox: {
2547+
type: "boolean",
2548+
},
2549+
},
2550+
if: {
2551+
properties: {
2552+
checkbox: {
2553+
const: true,
2554+
},
2555+
},
2556+
required: ["checkbox"],
2557+
},
2558+
then: {
2559+
properties: {
2560+
foo: {
2561+
type: "string",
2562+
default: "foo value",
2563+
},
2564+
},
2565+
required: ["foo"],
2566+
},
2567+
},
2568+
},
2569+
};
2570+
const rawFormData = {
2571+
wrappingObject: {
2572+
checkbox: true,
2573+
},
2574+
};
2575+
const expected = {
2576+
wrappingObject: {
2577+
checkbox: true,
2578+
foo: "foo value",
2579+
},
2580+
};
2581+
2582+
beforeEach(() => {
2583+
testValidator = createValidator({
2584+
cases: [
2585+
{
2586+
schema: {
2587+
properties: { checkbox: { const: true } },
2588+
required: ["checkbox"],
2589+
},
2590+
value: { checkbox: true },
2591+
result: true,
2592+
},
2593+
{
2594+
schema: {
2595+
properties: { checkbox: { const: true } },
2596+
required: ["checkbox"],
2597+
},
2598+
value: {},
2599+
result: false
2600+
},
2601+
],
2602+
});
2603+
defaultMerger = createMerger({
2604+
merges: [
2605+
{
2606+
left: {
2607+
type: "object",
2608+
properties: { checkbox: { type: "boolean" } },
2609+
},
2610+
right: {
2611+
properties: {
2612+
foo: { type: "string", default: "foo value" },
2613+
},
2614+
required: ["foo"],
2615+
},
2616+
result: {
2617+
type: "object",
2618+
properties: {
2619+
checkbox: { type: "boolean" },
2620+
foo: { type: "string", default: "foo value" },
2621+
},
2622+
required: ["foo"],
2623+
},
2624+
},
2625+
],
2626+
});
2627+
});
2628+
2629+
test("getDefaultFormState", () => {
2630+
expect(
2631+
getDefaultFormState(
2632+
testValidator,
2633+
defaultMerger,
2634+
schema,
2635+
rawFormData,
2636+
schema
2637+
)
2638+
).toEqual(expected);
2639+
});
2640+
2641+
test("computeDefaults", () => {
2642+
expect(
2643+
computeDefaults(testValidator, defaultMerger, schema, {
2644+
...defaults,
2645+
rootSchema: schema,
2646+
rawFormData,
2647+
shouldMergeDefaultsIntoFormData: true,
2648+
})
2649+
).toEqual(expected);
2650+
});
2651+
2652+
test("getDefaultBasedOnSchemaType", () => {
2653+
expect(
2654+
getDefaultBasedOnSchemaType(
2655+
testValidator,
2656+
defaultMerger,
2657+
schema,
2658+
{
2659+
...defaults,
2660+
rootSchema: schema,
2661+
rawFormData,
2662+
shouldMergeDefaultsIntoFormData: true,
2663+
},
2664+
undefined
2665+
)
2666+
).toEqual(expected);
2667+
});
2668+
2669+
test("getObjectDefaults", () => {
2670+
expect(
2671+
getObjectDefaults(
2672+
testValidator,
2673+
defaultMerger,
2674+
schema,
2675+
{
2676+
...defaults,
2677+
rootSchema: schema,
2678+
rawFormData,
2679+
shouldMergeDefaultsIntoFormData: true,
2680+
},
2681+
undefined
2682+
)
2683+
).toEqual(expected);
2684+
});
2685+
});
25382686
});
25392687

25402688
describe("array schemas", () => {

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { resolveDependencies, retrieveSchema } from "./resolve.js";
99
import {
1010
ALL_OF_KEY,
1111
DEPENDENCIES_KEY,
12+
IF_KEY,
1213
type Schema,
1314
type SchemaArrayValue,
1415
type SchemaObjectValue,
@@ -682,13 +683,19 @@ export function getObjectDefaults(
682683
}: ComputeDefaultsProps<SchemaObjectValue>,
683684
defaults: SchemaValue | undefined
684685
): SchemaObjectValue | null {
685-
// This is a custom addition that fixes this issue:
686-
// https://github.com/rjsf-team/react-jsonschema-form/issues/3832
687-
const retrievedSchema =
688-
experimental_defaultFormStateBehavior?.allOf === "populateDefaults" &&
689-
ALL_OF_KEY in schema
690-
? retrieveSchema(validator, merger, schema, rootSchema, formData)
691-
: schema;
686+
// Retrieve the schema:
687+
// - If schema contains `allOf` AND `experimental_defaultFormStateBehavior.allOf` is set to `populateDefaults`
688+
// - OR if schema contains an 'if' AND `emptyObjectFields` is not set to `skipEmptyDefaults`
689+
// This ensures we compute defaults correctly for schemas with these keywords.
690+
const shouldRetrieveSchema =
691+
(experimental_defaultFormStateBehavior?.allOf === "populateDefaults" &&
692+
ALL_OF_KEY in schema) ||
693+
(experimental_defaultFormStateBehavior?.emptyObjectFields !==
694+
"skipEmptyDefaults" &&
695+
IF_KEY in schema);
696+
const retrievedSchema = shouldRetrieveSchema
697+
? retrieveSchema(validator, merger, schema, rootSchema, formData)
698+
: schema;
692699
const retrievedSchemaRequired = new Set(retrievedSchema.required);
693700
const parentConstObject = isSchemaObjectValue(retrievedSchema.const)
694701
? retrievedSchema.const

0 commit comments

Comments
 (0)