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

Default value of field dependent on drop down not being updated #2978

Closed
4 tasks done
IanTurtle opened this issue Aug 1, 2022 · 4 comments
Closed
4 tasks done

Default value of field dependent on drop down not being updated #2978

IanTurtle opened this issue Aug 1, 2022 · 4 comments
Assignees
Labels
any-one-all-of Related to fixing anyOf, oneOf or allOf bug dependencies

Comments

@IanTurtle
Copy link

Prerequisites

What theme are you using?

core

Version

Playground

Current Behavior

In a form with two fields, where one is a dropdown of different options and a second is dependent on the other. The second field changes values depending on the option second. If you then select a different option from the dropdown menu, the value of the second field does not change to the different value.

Expected Behavior

In a form with two fields, where one is a dropdown of different options and a second is dependent on the other. The second field changes values depending on the option second. If you then select a different option from the dropdown menu, the value of the second field would change to the different value.

Steps To Reproduce

In the React JSON Schema playground, create a form using the following payload

{
    "title": "Optional Components",
    "required": [
      "component"
    ],
    "properties": {
      "component": {
        "title": "Component",
        "type": "object",
        "properties": {
          "component": {
            "title": "Component",
            "type": "string",
            "enum": [
              "value1",
              "value2"
            ]
          }
        },
        "required": [
          "component"
        ],
        "dependencies": {
          "component": {
            "oneOf": [
              {
                "properties": {
                  "component": {
                    "enum": [
                      "value1"
                    ]
                  },
                  "template": {
                    "type": "string",
                    "default": "abc"
                  }
                }
              },
              {
                "properties": {
                  "component": {
                    "enum": [
                      "value2"
                    ]
                  },
                  "template": {
                    "type": "string",
                    "default": "def"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }

Select value1 from the Component menu and notice the value of the template field is abc
image

Then select value2 from the Component menu and notice the value of the template field is abc (and not the expected def)
image

Environment

Playground

Anything else?

Playground link here

@IanTurtle IanTurtle added bug needs triage Initial label given, to be assigned correct labels and assigned labels Aug 1, 2022
@IanTurtle IanTurtle changed the title Default value of field dependent of drop down not updating as expected Default value of field dependent on drop down not being updated Aug 5, 2022
@lucaswiman
Copy link

I'm running into similar issues. I think the cause is here:

if (isObject(defaults) && isObject(schema.default)) {
// For object defaults, only override parent defaults that are defined in
// schema.default.
defaults = mergeObjects(
defaults!,
schema.default as GenericObjectType
) as T;
} else if (DEFAULT_KEY in schema) {
defaults = schema.default as unknown as T;
} else if (REF_KEY in schema) {
// Use referenced schema defaults for this node.
const refSchema = findSchemaDefinition<S>(schema[REF_KEY]!, rootSchema);
return computeDefaults<T, S>(
validator,
refSchema,
defaults,
rootSchema,
formData as T,
includeUndefinedValues
);
} else if (DEPENDENCIES_KEY in schema) {
const resolvedSchema = resolveDependencies(
validator,
schema,
rootSchema,
formData
);
return computeDefaults<T, S>(
validator,
resolvedSchema,
defaults,
rootSchema,
formData as T,
includeUndefinedValues
);
} else if (isFixedItems(schema)) {

When computing the defaults, once one set of defaults is determined, it only recurses down to non-dependent properties.

@lucaswiman
Copy link

Looking into this a bit more, it's less clear to me that this is a bug. FormData would need to record the source of every field (set from a default or entered data). Slightly altering the example playground with "enum": ["abc"] or "const": "abc" yields the desired behavior (see playground link).

@lucaswiman
Copy link

The example I've been looking at is here, with a recursive definition with dependencies.

To reproduce on that playground:

  • Select attr -> foo
  • Then update node_type -> Not
  • Child node_type does not default to "AtttributeNode"

A related schema (which should be semantically equivalent) puts the default on the node_type property (link). Here following the same procedure refuses to update the invalid child.node_type value in formData (even though the default is displayed in the UI element), and doesn't display the attr element.

@heath-freenome heath-freenome added dependencies any-one-all-of Related to fixing anyOf, oneOf or allOf and removed needs triage Initial label given, to be assigned correct labels and assigned labels Dec 16, 2022
@heath-freenome
Copy link
Member

I'm wondering if this is a very complicated example of #2944 in that our oneOf code isn't properly selecting the correct one-of

heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Jan 20, 2023
Fixes rjsf-team#2944, rjsf-team#3236, rjsf-team#2978 and possibly others
- In `@rjsf/utils`, added new `getClosestMatchingOption()`, `getFirstMatchingOption()` and `sanitizeDataForNewSchema()` schema-based utility functions
  - Deprecated `getMatchingOption()` and updated all calls to it in other utility functions to use `getFirstMatchingOption()`
  - Added 100% unit tests for all new functions, renaming the old `getMatchingOptionsTest.ts` file to `getFirstMatchingOptionsTest.ts`
  - Updated `createSchemaUtils()` and it's associated type to add the three new functions
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated the `schema.tests.ts` to add the new tests for the new schema-based utility functions
- In `@rjsf/core`, updated the `MultiSchemaField` to use the new `getClosestMatchingOption()` and `sanitizeDataForNewSchema()` utility functions
  - Also updated the render to properly pass props to the widget and the schema field
- In `@rjsf/playground`, updated `onFormDataEdited()` to only change the formData in the state if the `JSON.stringify()` of the old and new values are different
  - Also updated the `npm start` command to add the `--force` option to avoid issues where changes made to other packages weren't getting picked up due to `vite` caching
- Updated the `utility-functions.md` file to document the new schema-based functions and to fix up incorrect strike-through caused by the unescaped `<S>` generic
- Updated the `5.x upgrade guide.md` file to document the new utility functions and the deprecation of `getMatchingOption()`
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Jan 20, 2023
Fixes rjsf-team#2944, rjsf-team#3236, rjsf-team#2978 and possibly others
- In `@rjsf/utils`, added new `getClosestMatchingOption()`, `getFirstMatchingOption()` and `sanitizeDataForNewSchema()` schema-based utility functions
  - Deprecated `getMatchingOption()` and updated all calls to it in other utility functions to use `getFirstMatchingOption()`
  - Added 100% unit tests for all new functions, renaming the old `getMatchingOptionsTest.ts` file to `getFirstMatchingOptionsTest.ts`
  - Updated `createSchemaUtils()` and it's associated type to add the three new functions
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated the `schema.tests.ts` to add the new tests for the new schema-based utility functions
- In `@rjsf/core`, updated the `MultiSchemaField` to use the new `getClosestMatchingOption()` and `sanitizeDataForNewSchema()` utility functions
  - Also updated the render to properly pass props to the widget and the schema field
- In `@rjsf/playground`, updated `onFormDataEdited()` to only change the formData in the state if the `JSON.stringify()` of the old and new values are different
  - Also updated the `npm start` command to add the `--force` option to avoid issues where changes made to other packages weren't getting picked up due to `vite` caching
- Updated the `utility-functions.md` file to document the new schema-based functions and to fix up incorrect strike-through caused by the unescaped `<S>` generic
- Updated the `5.x upgrade guide.md` file to document the new utility functions and the deprecation of `getMatchingOption()`
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Jan 23, 2023
Fixes rjsf-team#2944, rjsf-team#3236, rjsf-team#2978 and possibly others
- In `@rjsf/utils`, added new `getClosestMatchingOption()`, `getFirstMatchingOption()` and `sanitizeDataForNewSchema()` schema-based utility functions
  - Deprecated `getMatchingOption()` and updated all calls to it in other utility functions to use `getFirstMatchingOption()`
  - Added 100% unit tests for all new functions, renaming the old `getMatchingOptionsTest.ts` file to `getFirstMatchingOptionsTest.ts`
  - Updated `createSchemaUtils()` and it's associated type to add the three new functions
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated the `schema.tests.ts` to add the new tests for the new schema-based utility functions
- In `@rjsf/core`, updated the `MultiSchemaField` to use the new `getClosestMatchingOption()` and `sanitizeDataForNewSchema()` utility functions
  - Also updated the render to properly pass props to the widget and the schema field
- In `@rjsf/playground`, updated `onFormDataEdited()` to only change the formData in the state if the `JSON.stringify()` of the old and new values are different
  - Also updated the `npm start` command to add the `--force` option to avoid issues where changes made to other packages weren't getting picked up due to `vite` caching
- Updated the `utility-functions.md` file to document the new schema-based functions and to fix up incorrect strike-through caused by the unescaped `<S>` generic
- Updated the `5.x upgrade guide.md` file to document the new utility functions and the deprecation of `getMatchingOption()`
heath-freenome added a commit that referenced this issue Jan 24, 2023
* fix: fixed several issue in oneOf/anyOf functions
Fixes #2944, #3236, #2978 and possibly others
- In `@rjsf/utils`, added new `getClosestMatchingOption()`, `getFirstMatchingOption()` and `sanitizeDataForNewSchema()` schema-based utility functions
  - Deprecated `getMatchingOption()` and updated all calls to it in other utility functions to use `getFirstMatchingOption()`
  - Added 100% unit tests for all new functions, renaming the old `getMatchingOptionsTest.ts` file to `getFirstMatchingOptionsTest.ts`
  - Updated `createSchemaUtils()` and it's associated type to add the three new functions
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated the `schema.tests.ts` to add the new tests for the new schema-based utility functions
- In `@rjsf/core`, updated the `MultiSchemaField` to use the new `getClosestMatchingOption()` and `sanitizeDataForNewSchema()` utility functions
  - Also updated the render to properly pass props to the widget and the schema field
- In `@rjsf/playground`, updated `onFormDataEdited()` to only change the formData in the state if the `JSON.stringify()` of the old and new values are different
  - Also updated the `npm start` command to add the `--force` option to avoid issues where changes made to other packages weren't getting picked up due to `vite` caching
- Updated the `utility-functions.md` file to document the new schema-based functions and to fix up incorrect strike-through caused by the unescaped `<S>` generic
- Updated the `5.x upgrade guide.md` file to document the new utility functions and the deprecation of `getMatchingOption()`

* - Fixed a few small issues exposed by trying to use the playground in #2375
- Also updated the `CHANGELOG.md` to include all of the fixed issues

* - Fix #2538 by fixing additionalProperties to deal with allOf/anyOf/oneOf

* - Updated `getSchemaType()` to grab the type of the first element of a `oneOf`/`anyOf`

* - Allow `formData` in `getClosestMatchingOption()` to accept `undefined`

* - Responded to reviewer feedback

* - Deal with sanitizing data when both `array.items` elements are booleans and have the same value

* - Fixed issue with const being assigned default value incorrectly and handle readOnly default values like const
- Updated some documentation in the types and createSchemaUtils
shijistar pushed a commit to shijistar/react-jsonschema-form that referenced this issue Jun 8, 2023
* fix: fixed several issue in oneOf/anyOf functions
Fixes rjsf-team#2944, rjsf-team#3236, rjsf-team#2978 and possibly others
- In `@rjsf/utils`, added new `getClosestMatchingOption()`, `getFirstMatchingOption()` and `sanitizeDataForNewSchema()` schema-based utility functions
  - Deprecated `getMatchingOption()` and updated all calls to it in other utility functions to use `getFirstMatchingOption()`
  - Added 100% unit tests for all new functions, renaming the old `getMatchingOptionsTest.ts` file to `getFirstMatchingOptionsTest.ts`
  - Updated `createSchemaUtils()` and it's associated type to add the three new functions
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated the `schema.tests.ts` to add the new tests for the new schema-based utility functions
- In `@rjsf/core`, updated the `MultiSchemaField` to use the new `getClosestMatchingOption()` and `sanitizeDataForNewSchema()` utility functions
  - Also updated the render to properly pass props to the widget and the schema field
- In `@rjsf/playground`, updated `onFormDataEdited()` to only change the formData in the state if the `JSON.stringify()` of the old and new values are different
  - Also updated the `npm start` command to add the `--force` option to avoid issues where changes made to other packages weren't getting picked up due to `vite` caching
- Updated the `utility-functions.md` file to document the new schema-based functions and to fix up incorrect strike-through caused by the unescaped `<S>` generic
- Updated the `5.x upgrade guide.md` file to document the new utility functions and the deprecation of `getMatchingOption()`

* - Fixed a few small issues exposed by trying to use the playground in rjsf-team#2375
- Also updated the `CHANGELOG.md` to include all of the fixed issues

* - Fix rjsf-team#2538 by fixing additionalProperties to deal with allOf/anyOf/oneOf

* - Updated `getSchemaType()` to grab the type of the first element of a `oneOf`/`anyOf`

* - Allow `formData` in `getClosestMatchingOption()` to accept `undefined`

* - Responded to reviewer feedback

* - Deal with sanitizing data when both `array.items` elements are booleans and have the same value

* - Fixed issue with const being assigned default value incorrectly and handle readOnly default values like const
- Updated some documentation in the types and createSchemaUtils
shijistar pushed a commit to shijistar/react-jsonschema-form that referenced this issue Jun 8, 2023
* fix: fixed several issue in oneOf/anyOf functions
Fixes rjsf-team#2944, rjsf-team#3236, rjsf-team#2978 and possibly others
- In `@rjsf/utils`, added new `getClosestMatchingOption()`, `getFirstMatchingOption()` and `sanitizeDataForNewSchema()` schema-based utility functions
  - Deprecated `getMatchingOption()` and updated all calls to it in other utility functions to use `getFirstMatchingOption()`
  - Added 100% unit tests for all new functions, renaming the old `getMatchingOptionsTest.ts` file to `getFirstMatchingOptionsTest.ts`
  - Updated `createSchemaUtils()` and it's associated type to add the three new functions
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated the `schema.tests.ts` to add the new tests for the new schema-based utility functions
- In `@rjsf/core`, updated the `MultiSchemaField` to use the new `getClosestMatchingOption()` and `sanitizeDataForNewSchema()` utility functions
  - Also updated the render to properly pass props to the widget and the schema field
- In `@rjsf/playground`, updated `onFormDataEdited()` to only change the formData in the state if the `JSON.stringify()` of the old and new values are different
  - Also updated the `npm start` command to add the `--force` option to avoid issues where changes made to other packages weren't getting picked up due to `vite` caching
- Updated the `utility-functions.md` file to document the new schema-based functions and to fix up incorrect strike-through caused by the unescaped `<S>` generic
- Updated the `5.x upgrade guide.md` file to document the new utility functions and the deprecation of `getMatchingOption()`

* - Fixed a few small issues exposed by trying to use the playground in rjsf-team#2375
- Also updated the `CHANGELOG.md` to include all of the fixed issues

* - Fix rjsf-team#2538 by fixing additionalProperties to deal with allOf/anyOf/oneOf

* - Updated `getSchemaType()` to grab the type of the first element of a `oneOf`/`anyOf`

* - Allow `formData` in `getClosestMatchingOption()` to accept `undefined`

* - Responded to reviewer feedback

* - Deal with sanitizing data when both `array.items` elements are booleans and have the same value

* - Fixed issue with const being assigned default value incorrectly and handle readOnly default values like const
- Updated some documentation in the types and createSchemaUtils
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 dependencies
Projects
None yet
Development

No branches or pull requests

5 participants