rjsf-team/react-jsonschema-form

Default values in conditional subschemas with oneOf select are not updating correctly

Open

#4603 opened on May 6, 2025

View on GitHub
 (4 comments) (3 reactions) (0 assignees)TypeScript (13,175 stars) (2,136 forks)batch import
featurehelp wanted

Description

Prerequisites

What theme are you using?

mui

Version

5.x

Current Behavior

I am experiencing an issue where the default values defined within conditional subschemas are not being updated in the form data when the value of a select field (using oneOf) changes. When a different option is selected in the dropdown, the default value for the conditionally displayed field (hi in this case) does not update to the default value specified in the corresponding then schema. It seems to retain the initial default or remains undefined if no initial default was set for that condition.

const schema = {
  type: "object",
  properties: {
    select: {
      type: "string",
      title: "select me",
      oneOf: [
        { const: 1, title: "1" },
        { const: 2, title: "2" },
        { const: 3, title: "3" },
      ],
    },
  },
  allOf: [
    {
      if: {
        properties: {
          select: {
            const: 1,
          },
        },
      },
      then: {
        properties: {
          hi: {
            title: "hi 1",
            default: 1,
            type: "number",
          },
        },
      },
    },
    {
      if: {
        properties: {
          select: {
            const: 2,
          },
        },
      },
      then: {
        properties: {
          hi: {
            title: "hi 2",
            default: 2,
            type: "number",
          },
        },
      },
    },
    {
      if: {
        properties: {
          select: {
            const: 3,
          },
        },
      },
      then: {
        properties: {
          hi: {},
        },
      },
    },
  ],
};

Playground link

When the value of the "select me" dropdown is changed, the form correctly shows or hides the "hi" field based on the selection. However, the default value of the "hi" field does not update according to the default value specified in the then block corresponding to the selected option. For instance, if "1" is selected initially, "hi 1" might have a default of 1. If the selection is changed to "2", "hi 2" should have a default of 2, but it either retains the initial default (if any) or appears empty.

Expected Behavior

I expect that when the value of the "select me" dropdown changes, the formData for the "hi" field should be updated to the default value specified in the then schema that matches the selected value.

Steps To Reproduce

  1. Render a form using the provided schema.
  2. Observe the initial state of the "hi" field based on the initial selection in "select me" (or the absence of selection).
  3. Change the value of the "select me" dropdown to a different option (e.g., from "1" to "2").
  4. Observe that the "hi" field does not update to the default value defined in the then block for the newly selected option.

Environment

rjsf Version: 5.24.10
React Version: 19
Browser: chrome
Operating System: windows

Anything else?

No response

Contributor guide