Skip to content

Commit 433f99b

Browse files
Use default values when switching anyOf option (#4375)
* Use default values when switching anyOf option * review * Update changelog * review: add oneOf test * Move changelog entry to 5.23.1 * Update CHANGELOG.md Fixed up release --------- Co-authored-by: Heath C <[email protected]>
1 parent 1c443a6 commit 433f99b

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

CHANGELOG.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ should change the heading of the (upcoming) version to include a major version b
1616
1717
-->
1818

19+
# 5.23.2
20+
21+
## @rjsf/core
22+
23+
- 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)
24+
1925
# 5.23.1
2026

2127
## @rjsf/chakra-ui
@@ -61,7 +67,7 @@ should change the heading of the (upcoming) version to include a major version b
6167

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

64-
# 5.22.3
70+
# 5.22.3
6571

6672
## @rjsf/utils
6773

@@ -137,7 +143,7 @@ should change the heading of the (upcoming) version to include a major version b
137143

138144
## @rjsf/core
139145

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

142148
## @rjsf/mui
143149

@@ -165,7 +171,7 @@ should change the heading of the (upcoming) version to include a major version b
165171

166172
# 5.20.0
167173

168-
## @rjsf/core
174+
## @rjsf/core
169175

170176
- Support allowing raising errors from within a custom Widget [#2718](https://github.com/rjsf-team/react-jsonschema-form/issues/2718)
171177
- 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
183189

184190
# 5.19.4
185191

186-
## @rjsf/core
192+
## @rjsf/core
187193

188194
- Fix XSS when rendering schema validation errors [#4254](https://github.com/rjsf-team/react-jsonschema-form/issues/2718)
189195
- 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
200206

201207
## Dev / docs / playground
202208

203-
- 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
209+
- 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
204210

205211
# 5.19.3
206212

packages/core/src/components/fields/MultiSchemaField.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
123123
const oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : undefined;
124124

125125
let newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
126-
if (newFormData && newOption) {
126+
if (newOption) {
127127
// Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
128128
// so that only the root objects themselves are created without adding undefined children properties
129129
newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, 'excludeObjectChildren') as T;

packages/core/test/anyOf.test.jsx

+31
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,37 @@ describe('anyOf', () => {
165165
);
166166
});
167167

168+
it('should assign a default value and set defaults on option change for scalar types schemas', () => {
169+
const { node, onChange } = createFormComponent({
170+
schema: {
171+
type: 'object',
172+
properties: {
173+
foo: {
174+
anyOf: [
175+
{ type: 'string', default: 'defaultfoo' },
176+
{ type: 'boolean', default: true },
177+
],
178+
},
179+
},
180+
},
181+
});
182+
sinon.assert.calledWithMatch(onChange.lastCall, {
183+
formData: { foo: 'defaultfoo' },
184+
});
185+
186+
const $select = node.querySelector('select');
187+
188+
act(() => {
189+
fireEvent.change($select, {
190+
target: { value: $select.options[1].value },
191+
});
192+
});
193+
194+
sinon.assert.calledWithMatch(onChange.lastCall, {
195+
formData: { foo: true },
196+
});
197+
});
198+
168199
it('should assign a default value and set defaults on option change when using references', () => {
169200
const { node, onChange } = createFormComponent({
170201
schema: {

packages/core/test/oneOf.test.jsx

+34
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,37 @@ describe('oneOf', () => {
222222
);
223223
});
224224

225+
it('should assign a default value and set defaults on option change for scalar types schemas', () => {
226+
const { node, onChange } = createFormComponent({
227+
schema: {
228+
type: 'object',
229+
properties: {
230+
foo: {
231+
oneOf: [
232+
{ type: 'string', default: 'defaultfoo' },
233+
{ type: 'boolean', default: true },
234+
],
235+
},
236+
},
237+
},
238+
});
239+
sinon.assert.calledWithMatch(onChange.lastCall, {
240+
formData: { foo: 'defaultfoo' },
241+
});
242+
243+
const $select = node.querySelector('select');
244+
245+
act(() => {
246+
fireEvent.change($select, {
247+
target: { value: $select.options[1].value },
248+
});
249+
});
250+
251+
sinon.assert.calledWithMatch(onChange.lastCall, {
252+
formData: { foo: true },
253+
});
254+
});
255+
225256
it('should render a custom widget', () => {
226257
const schema = {
227258
type: 'object',
@@ -573,6 +604,7 @@ describe('oneOf', () => {
573604
},
574605
};
575606
const formContext = { root: 'root-id', root_userId: 'userId-id' };
607+
576608
function CustomSchemaField(props) {
577609
const { formContext, idSchema } = props;
578610
return (
@@ -582,6 +614,7 @@ describe('oneOf', () => {
582614
</>
583615
);
584616
}
617+
585618
const { node } = createFormComponent({
586619
schema,
587620
formData: { userId: 'foobarbaz' },
@@ -1598,6 +1631,7 @@ describe('oneOf', () => {
15981631
},
15991632
},
16001633
};
1634+
16011635
function customValidate(formData, errors) {
16021636
errors.userId.addError('test');
16031637
return errors;

0 commit comments

Comments
 (0)