Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
should change the heading of the (upcoming) version to include a major version bump.

-->
# 5.24.12

## @rjsf/utils

- (Backported change from 6.0.0-beta.11) Fixed issue where oneOf radio button could not be modified when constAsDefaults is set to 'never', fixing [#4634](https://github.com/rjsf-team/react-jsonschema-form/issues/4634).

# 5.24.11

## @rjsf/utils
Expand Down
131 changes: 109 additions & 22 deletions packages/core/test/Form.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,61 @@ describeRepeated('Form common', (createFormComponent) => {

expect(node.querySelector(secondInputID).value).to.equal('changed!');
});
it('Should modify oneOf radio button when the defaults are set.', () => {
it('Should modify oneOf object with references when the defaults are set.', () => {
const schema = {
type: 'object',
$defs: {
protocol: {
type: 'string',
enum: ['fast', 'balanced', 'stringent'],
default: 'fast',
},
},
oneOf: [
{
properties: {
protocol: {
$ref: '#/$defs/protocol',
},
},
},
{
properties: {
something: {
type: 'number',
},
},
},
],
};

const { node, onChange } = createFormComponent({
schema,
});

const protocolInputID = '#root_protocol';
expect(node.querySelector(protocolInputID).value).to.equal('0');

act(() => {
fireEvent.change(node.querySelector(protocolInputID), {
target: { value: '1' },
});
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
protocol: 'balanced',
},
schema,
},
'root_protocol'
);

expect(node.querySelector(protocolInputID).value).to.equal('1');
});
describe('Should modify oneOf radio button when the defaults are set.', () => {
const schema = {
type: 'object',
properties: {
Expand Down Expand Up @@ -1476,35 +1530,68 @@ describeRepeated('Form common', (createFormComponent) => {
'ui:label': false,
},
};

const { node, onChange } = createFormComponent({
schema,
uiSchema,
});

const notApplicableInputID = '#root_a-1';
const NoInputID = '#root_a-0';
expect(node.querySelector(notApplicableInputID).checked).to.equal(true);

act(() => {
fireEvent.click(node.querySelector(NoInputID));
});
it('Test with default constAsDefaults', () => {
const { node, onChange } = createFormComponent({
schema,
uiSchema,
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
a: false,
expect(node.querySelector(notApplicableInputID).checked).to.equal(true);

act(() => {
fireEvent.click(node.querySelector(NoInputID));
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
a: false,
},
schema,
uiSchema,
},
'root_a'
);

expect(node.querySelector(NoInputID).checked).to.equal(true);
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
expect(node.querySelector('#root_b')).to.exist;
});
it('Test with constAsDefaults set to "never"', () => {
const { node, onChange } = createFormComponent({
schema,
uiSchema,
},
'root_a'
);
experimental_defaultFormStateBehavior: {
constAsDefaults: 'never',
},
});

expect(node.querySelector(notApplicableInputID).checked).to.equal(true);

expect(node.querySelector(NoInputID).checked).to.equal(true);
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
expect(node.querySelector('#root_b')).to.exist;
act(() => {
fireEvent.click(node.querySelector(NoInputID));
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
a: false,
},
schema,
uiSchema,
},
'root_a'
);

expect(node.querySelector(NoInputID).checked).to.equal(true);
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
expect(node.querySelector('#root_b')).to.exist;
});
});
it('Should modify oneOf object with references when the defaults are set.', () => {
const schema = {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
experimental_defaultFormStateBehavior: experimental_dfsb_to_compute,
experimental_customMergeAllOf,
parentDefaults: defaults as T | undefined,
rawFormData: formData as T,
rawFormData: (rawFormData ?? formData) as T,
required,
shouldMergeDefaultsIntoFormData,
});
Expand Down