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

Use default values when switching anyOf option #4375

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/core

- Updated `NumberField` to properly pass through the `errorSchema` and `id` in the onChange handler, fixing [#4382](https://github.com/rjsf-team/react-jsonschema-form/issues/4382)
- Fix default value population when switching between options in `MultiSchemaField` [#4375](https://github.com/rjsf-team/react-jsonschema-form/pull/4375). Fixes [#4367](https://github.com/rjsf-team/react-jsonschema-form/issues/4367)

## Dev / docs / playground

Expand Down Expand Up @@ -61,7 +62,7 @@ should change the heading of the (upcoming) version to include a major version b

- Fix issue 'Maximum call stack size exceeded' with playground share with large content.

# 5.22.3
# 5.22.3

## @rjsf/utils

Expand Down Expand Up @@ -137,7 +138,7 @@ should change the heading of the (upcoming) version to include a major version b

## @rjsf/core

- Updated `Form` to fix `focusOnError()` to support the ids that include dots, fixing [#4279](https://github.com/rjsf-team/react-jsonschema-form/issues/4279)
- Updated `Form` to fix `focusOnError()` to support the ids that include dots, fixing [#4279](https://github.com/rjsf-team/react-jsonschema-form/issues/4279)

## @rjsf/mui

Expand Down Expand Up @@ -165,7 +166,7 @@ should change the heading of the (upcoming) version to include a major version b

# 5.20.0

## @rjsf/core
## @rjsf/core

- Support allowing raising errors from within a custom Widget [#2718](https://github.com/rjsf-team/react-jsonschema-form/issues/2718)
- Updated `ArrayField`, `BooleanField` and `StringField` to call `optionsList()` with the additional `UiSchema` parameter, fixing [#4215](https://github.com/rjsf-team/react-jsonschema-form/issues/4215) and [#4260](https://github.com/rjsf-team/react-jsonschema-form/issues/4260)
Expand All @@ -183,7 +184,7 @@ should change the heading of the (upcoming) version to include a major version b

# 5.19.4

## @rjsf/core
## @rjsf/core

- Fix XSS when rendering schema validation errors [#4254](https://github.com/rjsf-team/react-jsonschema-form/issues/2718)
- NOTE: This will have potential consequences if you are using the [translateString](https://rjsf-team.github.io/react-jsonschema-form/docs/api-reference/form-props/#translatestring) feature and are trying to render HTML. Switching to [Markdown](https://www.markdownguide.org/) will solve your problems.
Expand All @@ -200,7 +201,7 @@ should change the heading of the (upcoming) version to include a major version b

## Dev / docs / playground

- Updated the `Validator` dropdown to add `AJV8 (discriminator)` which sets the AJV validator [discriminator](https://ajv.js.org/json-schema.html#discriminator) option to `true` to support testing schemas with that option in them
- Updated the `Validator` dropdown to add `AJV8 (discriminator)` which sets the AJV validator [discriminator](https://ajv.js.org/json-schema.html#discriminator) option to `true` to support testing schemas with that option in them

# 5.19.3

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/fields/MultiSchemaField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
const oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : undefined;

let newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
if (newFormData && newOption) {
if (newOption) {
// Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
// so that only the root objects themselves are created without adding undefined children properties
newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, 'excludeObjectChildren') as T;
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/anyOf.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,37 @@ describe('anyOf', () => {
);
});

it('should assign a default value and set defaults on option change for scalar types schemas', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a similar test for the oneOf.test.jsx file?

Copy link
Contributor Author

@quentin-sommer quentin-sommer Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, PR updated 👍

const { node, onChange } = createFormComponent({
schema: {
type: 'object',
properties: {
foo: {
anyOf: [
{ type: 'string', default: 'defaultfoo' },
{ type: 'boolean', default: true },
],
},
},
},
});
sinon.assert.calledWithMatch(onChange.lastCall, {
formData: { foo: 'defaultfoo' },
});

const $select = node.querySelector('select');

act(() => {
fireEvent.change($select, {
target: { value: $select.options[1].value },
});
});

sinon.assert.calledWithMatch(onChange.lastCall, {
formData: { foo: true },
});
});

it('should assign a default value and set defaults on option change when using references', () => {
const { node, onChange } = createFormComponent({
schema: {
Expand Down
34 changes: 34 additions & 0 deletions packages/core/test/oneOf.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,37 @@ describe('oneOf', () => {
);
});

it('should assign a default value and set defaults on option change for scalar types schemas', () => {
const { node, onChange } = createFormComponent({
schema: {
type: 'object',
properties: {
foo: {
oneOf: [
{ type: 'string', default: 'defaultfoo' },
{ type: 'boolean', default: true },
],
},
},
},
});
sinon.assert.calledWithMatch(onChange.lastCall, {
formData: { foo: 'defaultfoo' },
});

const $select = node.querySelector('select');

act(() => {
fireEvent.change($select, {
target: { value: $select.options[1].value },
});
});

sinon.assert.calledWithMatch(onChange.lastCall, {
formData: { foo: true },
});
});

it('should render a custom widget', () => {
const schema = {
type: 'object',
Expand Down Expand Up @@ -573,6 +604,7 @@ describe('oneOf', () => {
},
};
const formContext = { root: 'root-id', root_userId: 'userId-id' };

function CustomSchemaField(props) {
const { formContext, idSchema } = props;
return (
Expand All @@ -582,6 +614,7 @@ describe('oneOf', () => {
</>
);
}

const { node } = createFormComponent({
schema,
formData: { userId: 'foobarbaz' },
Expand Down Expand Up @@ -1598,6 +1631,7 @@ describe('oneOf', () => {
},
},
};

function customValidate(formData, errors) {
errors.userId.addError('test');
return errors;
Expand Down