Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 tasks done
Writingsinfo opened this issue Oct 2, 2024 · 3 comments
Open
4 tasks done
Assignees
Labels

Comments

@Writingsinfo
Copy link

Writingsinfo commented Oct 2, 2024

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.

@Writingsinfo Writingsinfo added bug needs triage Initial label given, to be assigned correct labels and assigned labels Oct 2, 2024
@heath-freenome heath-freenome self-assigned this Oct 4, 2024
@heath-freenome heath-freenome removed the needs triage Initial label given, to be assigned correct labels and assigned label Oct 4, 2024
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Oct 5, 2024
…rjsf-team#4322

Fixes rjsf-team#3997 and rjsf-team#4322
- In `@rjsf/utils`, made the following changes:
  - Updated `mergeDefaultsWithFormData()` to not overwrite a default when the formData has an undefined value
  - Updated `getClosestMatchingOption()` to improve the scoring function so that an object container that matches a key gets an extra point
- In `@rjsf/core`, updated `MultiSchemaField` to call `onChange` after setting the new option in state rather than before
- Updated the `CHANGELOG.md` accordingly
@Nickvajani
Copy link

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

@reshmarevi-kv
Copy link

reshmarevi-kv commented Oct 17, 2024

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
Copy link
Member

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

 { mergeDefaultsIntoFormData: 'useDefaultIfFormDataUndefined' }

heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Oct 18, 2024
…rjsf-team#4322

Fixes rjsf-team#3997 and rjsf-team#4322
- In `@rjsf/utils`, made the following changes:
  - Updated `mergeDefaultsWithFormData()` to not overwrite a default when the formData has an undefined value
  - Updated `getClosestMatchingOption()` to improve the scoring function so that an object container that matches a key gets an extra point
- In `@rjsf/core`, updated `MultiSchemaField` to call `onChange` after setting the new option in state rather than before
- Updated the `CHANGELOG.md` accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Oct 21, 2024
…rjsf-team#4322

Fixes rjsf-team#3997 and rjsf-team#4322
- In `@rjsf/utils`, made the following changes:
  - Updated `mergeDefaultsWithFormData()` to not overwrite a default when the formData has an undefined value
  - Updated `getClosestMatchingOption()` to improve the scoring function so that an object container that matches a key gets an extra point
- In `@rjsf/core`, updated `MultiSchemaField` to call `onChange` after setting the new option in state rather than before
- Updated the `CHANGELOG.md` accordingly
heath-freenome added a commit that referenced this issue Oct 21, 2024
…4322 (#4329)

* Fix: made small changes to utility functions to fix #3997 and #4322
Fixes #3997 and #4322
- In `@rjsf/utils`, made the following changes:
  - Updated `mergeDefaultsWithFormData()` to not overwrite a default when the formData has an undefined value
  - Updated `getClosestMatchingOption()` to improve the scoring function so that an object container that matches a key gets an extra point
- In `@rjsf/core`, updated `MultiSchemaField` to call `onChange` after setting the new option in state rather than before
- Updated the `CHANGELOG.md` accordingly

* - In order to avoid regressions, added a new `mergeDefaultsWithFormData` prop to the `Experimental_DefaultFormStateBehavior`
- Updated `mergeDefaultsWithFormData()` to add new optional `defaultSupercedesUndefined` that when true uses the defaults rather than `undefined` formData
- Updated `getDefaultFormState()` to pass true to `mergeDefaultsWithFormData` for `defaultSupercedesUndefined` when `mergeDefaultsIntoFormData` has the value `useDefaultIfFormDataUndefined`
- Updated the documentation for the new capabilities
- Updated the playground to add controls for the new `mergeDefaultsIntoFormData` option
  - moved the `Show Error List` component over one column, making it inline radio buttons rather than a select

* - Improved documentation a teeny bit

* - More doc improvements

* - Responded to reviewer feedback

* - Fixed broken links

* - Added section header for wrapping BaseInputTemplate

* - Updated CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants