rjsf-team / react-jsonschema-form

A React component for building Web forms from JSON Schema.
https://rjsf-team.github.io/react-jsonschema-form/
Apache License 2.0
14.34k stars 2.19k forks source link

OneOf not able to retain the default values when switching between the options #4322

Open Writingsinfo opened 1 month ago

Writingsinfo commented 1 month ago

Prerequisites

What theme are you using?

core

Version

5.x

Current Behavior

The issue we are seeing is that when we switch options, the default values are restored only for the fields that are common to both the options. Eg. Link to playground

In this example, when you switch from option-1 to option-2 and then back to option-1, default value for lorem is not restored.

Expected Behavior

Default values should be retained when switching between the options of oneOf

Steps To Reproduce

Switch between options and you'll notice that the default values are not retained. Link to playground

Environment

- OS:
- Node:
- npm:

Anything else?

This issue might be similar to #4208 but that one is for the same keys while the example I'm sharing is about the unique keys in the options of oneOf.

Same behavior is expected for anyOf.

Nickvajani commented 4 weeks ago

Thanks @heath-freenome for the quick fix. Looking forward to start using them.

reshmarevi-kv commented 3 weeks ago

will this gonna fix the issue of value not resetting back when switch between dependencies ?

https://rjsf-team.github.io/react-jsonschema-form/docs/json-schema/dependencies/

like this example.. current behaviour

when i select "yes" and enter some input in the "How old is your pet?" field and then switch to no and again comeback to yes the value i entered is still there. @Nickvajani @heath-freenome

heath-freenome commented 2 weeks ago

The fix will require you to set the experimental_defaultFormStateBehavior prop on the Form with the following value:

 { mergeDefaultsIntoFormData: 'useDefaultIfFormDataUndefined' }