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

Switching between two oneOf option returns undefined form data #4314

Closed
4 tasks done
altpd13 opened this issue Sep 26, 2024 · 4 comments
Closed
4 tasks done

Switching between two oneOf option returns undefined form data #4314

altpd13 opened this issue Sep 26, 2024 · 4 comments

Comments

@altpd13
Copy link

altpd13 commented Sep 26, 2024

Prerequisites

What theme are you using?

core

Version

5.2.0

Current Behavior

Using this json Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "ExecuteMsg",
  "oneOf": [
    {
      "type": "object",
      "required": [
        "increment"
      ],
      "properties": {
        "increment": {
          "type": "object"
        }
      },
      "additionalProperties": false
    },
    {
      "type": "object",
      "required": [
        "reset"
      ],
      "properties": {
        "reset": {
          "type": "object",
          "required": [
            "count"
          ],
          "properties": {
            "count": {
              "type": "integer",
              "format": "int32"
            }
          }
        }
      },
      "additionalProperties": false
    }
  ]
}

when form is first initialized formData is {increment: {}} and when I change to reset formData is set to {increment: undefined, reset: {}} and again when change to increment formData is {increment: undefined, reset: undefined}

Expected Behavior

When changing back to option increment It should be {increment: {}} not {increment: undefined, reset: undefined}

Steps To Reproduce

https://rjsf-team.github.io/react-jsonschema-form/#eyJmb3JtRGF0YSI6eyJyZXNldCI6e319LCJzY2hlbWEiOnsiJHNjaGVtYSI6Imh0dHA6Ly9qc29uLXNjaGVtYS5vcmcvZHJhZnQtMDcvc2NoZW1hIyIsInRpdGxlIjoiRXhlY3V0ZU1zZyIsIm9uZU9mIjpbeyJ0eXBlIjoib2JqZWN0IiwicmVxdWlyZWQiOlsiaW5jcmVtZW50Il0sInByb3BlcnRpZXMiOnsiaW5jcmVtZW50Ijp7InR5cGUiOiJvYmplY3QifX0sImFkZGl0aW9uYWxQcm9wZXJ0aWVzIjpmYWxzZX0seyJ0eXBlIjoib2JqZWN0IiwicmVxdWlyZWQiOlsicmVzZXQiXSwicHJvcGVydGllcyI6eyJyZXNldCI6eyJ0eXBlIjoib2JqZWN0IiwicmVxdWlyZWQiOlsiY291bnQiXSwicHJvcGVydGllcyI6eyJjb3VudCI6eyJ0eXBlIjoiaW50ZWdlciIsImZvcm1hdCI6ImludDMyIn19fX0sImFkZGl0aW9uYWxQcm9wZXJ0aWVzIjpmYWxzZX1dfSwidWlTY2hlbWEiOnt9LCJ0aGVtZSI6ImRlZmF1bHQiLCJsaXZlU2V0dGluZ3MiOnsic2hvd0Vycm9yTGlzdCI6InRvcCIsImV4cGVyaW1lbnRhbF9kZWZhdWx0Rm9ybVN0YXRlQmVoYXZpb3IiOnsiYXJyYXlNaW5JdGVtcyI6eyJwb3B1bGF0ZSI6InBvcHVsYXRlIiwibWVyZ2VFeHRyYURlZmF1bHRzIjpmYWxzZX0sImFsbE9mIjoic2tpcERlZmF1bHRzIiwiZW1wdHlPYmplY3RGaWVsZHMiOiJwb3B1bGF0ZUFsbERlZmF1bHRzIn0sImxpdmVWYWxpZGF0ZSI6ZmFsc2V9fQ==

Environment

- OS: Mac Sonoma 14.5
- Node: 20.0.0
- npm: 9.6.4
- yarn: 1.22.22

Anything else?

No response

@altpd13 altpd13 added bug needs triage Initial label given, to be assigned correct labels and assigned labels Sep 26, 2024
@altpd13 altpd13 changed the title <title> Switching between two oneOf option returns undefined form data Switching between two oneOf option returns undefined form data Sep 26, 2024
@nickgros
Copy link
Contributor

Looks like a duplicate of #3711 ?

@altpd13
Copy link
Author

altpd13 commented Sep 30, 2024

Thank you @nickgros for reading my issue
But I'm afraid the behavior #3711 is different from my behavior.

The current behavior I've mentioned is when changing back to oneOf element which requires empty object property results undefined value.
So for example I want a formData that have empty value(e.g {increment: {}} on submit. The initial formData when Form first renders is set as exactly what I wanted it to be({increment: {}}). But changing to other oneOf element and changing back to increment element will set the formData to {increment: undefined, reset: undefined} not {increment: {}, reset: undefined}.

I wish this information would helps you understand

@heath-freenome heath-freenome removed the needs triage Initial label given, to be assigned correct labels and assigned label Oct 4, 2024
@nickgros
Copy link
Contributor

nickgros commented Oct 4, 2024

We're also seeing the issue in #3977 in this playground, but not sure if fixing that will fix this

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
@altpd13
Copy link
Author

altpd13 commented Nov 27, 2024

Seems like it is fixed!
People who are having this issue use this experimental feature
https://rjsf-team.github.io/react-jsonschema-form/docs/api-reference/form-props/#mergedefaultsintoformdata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants