diff --git a/CHANGELOG.md b/CHANGELOG.md index a84feb1b99..ffdce1c64c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,12 @@ should change the heading of the (upcoming) version to include a major version b --> +# 5.23.2 + +## @rjsf/core + +- 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) + # 5.23.1 ## @rjsf/chakra-ui @@ -61,7 +67,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 @@ -137,7 +143,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 @@ -165,7 +171,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) @@ -183,7 +189,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. @@ -200,7 +206,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 diff --git a/packages/core/src/components/fields/MultiSchemaField.tsx b/packages/core/src/components/fields/MultiSchemaField.tsx index 7808f3289c..d4dd390a90 100644 --- a/packages/core/src/components/fields/MultiSchemaField.tsx +++ b/packages/core/src/components/fields/MultiSchemaField.tsx @@ -123,7 +123,7 @@ class AnyOfField= 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; diff --git a/packages/core/test/anyOf.test.jsx b/packages/core/test/anyOf.test.jsx index b043ad7923..0c30e92113 100644 --- a/packages/core/test/anyOf.test.jsx +++ b/packages/core/test/anyOf.test.jsx @@ -165,6 +165,37 @@ describe('anyOf', () => { ); }); + 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: { + 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: { diff --git a/packages/core/test/oneOf.test.jsx b/packages/core/test/oneOf.test.jsx index 3c76c6cfd5..4c021b2464 100644 --- a/packages/core/test/oneOf.test.jsx +++ b/packages/core/test/oneOf.test.jsx @@ -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', @@ -573,6 +604,7 @@ describe('oneOf', () => { }, }; const formContext = { root: 'root-id', root_userId: 'userId-id' }; + function CustomSchemaField(props) { const { formContext, idSchema } = props; return ( @@ -582,6 +614,7 @@ describe('oneOf', () => { ); } + const { node } = createFormComponent({ schema, formData: { userId: 'foobarbaz' }, @@ -1598,6 +1631,7 @@ describe('oneOf', () => { }, }, }; + function customValidate(formData, errors) { errors.userId.addError('test'); return errors;