Skip to content

Commit 6b40974

Browse files
fixed oneOf radio button could not be modified when constAsDefaults is set to 'never' (#4665)
Co-authored-by: Heath C <51679588+heath-freenome@users.noreply.github.com>
1 parent 50b6d74 commit 6b40974

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

packages/core/test/Form.test.jsx

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ describeRepeated('Form common', (createFormComponent) => {
15471547

15481548
expect(node.querySelector(protocolInputID).value).to.equal('1');
15491549
});
1550-
it('Should modify oneOf radio button when the defaults are set.', () => {
1550+
describe('Should modify oneOf radio button when the defaults are set.', () => {
15511551
const schema = {
15521552
type: 'object',
15531553
properties: {
@@ -1594,35 +1594,68 @@ describeRepeated('Form common', (createFormComponent) => {
15941594
'ui:label': false,
15951595
},
15961596
};
1597-
1598-
const { node, onChange } = createFormComponent({
1599-
schema,
1600-
uiSchema,
1601-
});
1602-
16031597
const notApplicableInputID = '#root_a-1';
16041598
const NoInputID = '#root_a-0';
1605-
expect(node.querySelector(notApplicableInputID).checked).to.equal(true);
16061599

1607-
act(() => {
1608-
fireEvent.click(node.querySelector(NoInputID));
1609-
});
1600+
it('Test with default constAsDefaults', () => {
1601+
const { node, onChange } = createFormComponent({
1602+
schema,
1603+
uiSchema,
1604+
});
16101605

1611-
sinon.assert.calledWithMatch(
1612-
onChange.lastCall,
1613-
{
1614-
formData: {
1615-
a: false,
1606+
expect(node.querySelector(notApplicableInputID).checked).to.equal(true);
1607+
1608+
act(() => {
1609+
fireEvent.click(node.querySelector(NoInputID));
1610+
});
1611+
1612+
sinon.assert.calledWithMatch(
1613+
onChange.lastCall,
1614+
{
1615+
formData: {
1616+
a: false,
1617+
},
1618+
schema,
1619+
uiSchema,
16161620
},
1621+
'root_a',
1622+
);
1623+
1624+
expect(node.querySelector(NoInputID).checked).to.equal(true);
1625+
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
1626+
expect(node.querySelector('#root_b')).to.exist;
1627+
});
1628+
it('Test with constAsDefaults set to "never"', () => {
1629+
const { node, onChange } = createFormComponent({
16171630
schema,
16181631
uiSchema,
1619-
},
1620-
'root_a',
1621-
);
1632+
experimental_defaultFormStateBehavior: {
1633+
constAsDefaults: 'never',
1634+
},
1635+
});
1636+
1637+
expect(node.querySelector(notApplicableInputID).checked).to.equal(true);
1638+
1639+
act(() => {
1640+
fireEvent.click(node.querySelector(NoInputID));
1641+
});
1642+
1643+
sinon.assert.calledWithMatch(
1644+
onChange.lastCall,
1645+
{
1646+
formData: {
1647+
a: false,
1648+
},
1649+
schema,
1650+
uiSchema,
1651+
},
1652+
'root_a',
1653+
);
16221654

1623-
expect(node.querySelector(NoInputID).checked).to.equal(true);
1624-
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
1625-
expect(node.querySelector('#root_b')).to.exist;
1655+
expect(node.querySelector(NoInputID).checked).to.equal(true);
1656+
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
1657+
expect(node.querySelector('#root_b')).to.exist;
1658+
});
16261659
});
16271660
});
16281661

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
334334
experimental_defaultFormStateBehavior: experimental_dfsb_to_compute,
335335
experimental_customMergeAllOf,
336336
parentDefaults: defaults as T | undefined,
337-
rawFormData: formData as T,
337+
rawFormData: (rawFormData ?? formData) as T,
338338
required,
339339
shouldMergeDefaultsIntoFormData,
340340
});

0 commit comments

Comments
 (0)