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

State corruption of selectedOption in MultiSchemaField when editing data within an oneOf #2086

Closed
3 tasks done
erunion opened this issue Oct 10, 2020 · 2 comments
Closed
3 tasks done
Labels
any-one-all-of Related to fixing anyOf, oneOf or allOf bug core

Comments

@erunion
Copy link
Collaborator

erunion commented Oct 10, 2020

Prerequisites

Description

On a oneOf schema that contains a handful of objects that might slightly differ, the selectedOption state is immediately corrupted and the selected option changes once editing data within the form.

Debugging, the problem is the componentDidUpdate React event handler on the MultiSchemaField component. This event is invoked anytime formData is changed within the multi schema, and is changing the selectedOption without the users permission, resulting in an incredibly degraded experience on these types of schemas. Check out this playground example for an example of it in action.

Completely eliminating the componentDidUpdate event from this component resolves the issue, but I'm having a hard time understanding why this component has a componentDidUpdate at all to reset the selectedOption when formData changes. selectedOption should only change with the onOptionChange event handler, no?

Steps to Reproduce

  1. Load the playground example.
  2. Type 1 into the listing_id input.

Expected behavior

The selected MultiSchemaField should not change unless the user invokes a change to the dropdown.

Actual behavior

MultiSchemaField selectedOption state is changed when formData is updated.

Version

This is happening on the latest master.

@skyboyer
Copy link
Contributor

skyboyer commented Mar 15, 2021

to emphasize: it happens only if formData matches multiple types from same oneOf simultaneously

why it can re-calculate type after formData has changed? Since formData is coming from outside, it's technically possible new data does not match shape of previously selected "oneOf". Maybe a desired result would be not picking first matching type but checking, if data still fits type currently selected. And then picking first matching only if data shape does not satisfy type currently selected.

@skyboyer
Copy link
Contributor

By now, in general case, I see 2 different approaches with their own pitfalls:

  1. to convert flat list of "oneOf" into a tree of nested "oneOf" where at each level there would have be unique set of fields. Easier to say than to do, sure.
  2. to materialize type with introducing extra field with "enum" of only one possible value("enum": ["Cuba"]), enforce that value with "default": ["Cuba"] and hide it with "ui:widget": "hidden" in UI schema(example). Unfortunately, in this case we need also trim that extra field before sending to server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
any-one-all-of Related to fixing anyOf, oneOf or allOf bug core
Projects
None yet
Development

No branches or pull requests

4 participants