Skip to content

Commit d27bcaf

Browse files
nickgrosabdalla-rkoheath-freenome
authored
Backport #4665 to v5 (#4669)
* fixed oneOf radio button could not be modified when constAsDefaults is set to 'never' (#4665) Co-authored-by: Heath C <[email protected]> * Update CHANGELOG --------- Co-authored-by: Appie <[email protected]> Co-authored-by: Heath C <[email protected]>
1 parent 5f8d462 commit d27bcaf

File tree

3 files changed

+116
-23
lines changed

3 files changed

+116
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
1515
should change the heading of the (upcoming) version to include a major version bump.
1616
1717
-->
18+
# 5.24.12
19+
20+
## @rjsf/utils
21+
22+
- (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).
23+
1824
# 5.24.11
1925

2026
## @rjsf/utils

packages/core/test/Form.test.jsx

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,61 @@ describeRepeated('Form common', (createFormComponent) => {
14291429

14301430
expect(node.querySelector(secondInputID).value).to.equal('changed!');
14311431
});
1432-
it('Should modify oneOf radio button when the defaults are set.', () => {
1432+
it('Should modify oneOf object with references when the defaults are set.', () => {
1433+
const schema = {
1434+
type: 'object',
1435+
$defs: {
1436+
protocol: {
1437+
type: 'string',
1438+
enum: ['fast', 'balanced', 'stringent'],
1439+
default: 'fast',
1440+
},
1441+
},
1442+
oneOf: [
1443+
{
1444+
properties: {
1445+
protocol: {
1446+
$ref: '#/$defs/protocol',
1447+
},
1448+
},
1449+
},
1450+
{
1451+
properties: {
1452+
something: {
1453+
type: 'number',
1454+
},
1455+
},
1456+
},
1457+
],
1458+
};
1459+
1460+
const { node, onChange } = createFormComponent({
1461+
schema,
1462+
});
1463+
1464+
const protocolInputID = '#root_protocol';
1465+
expect(node.querySelector(protocolInputID).value).to.equal('0');
1466+
1467+
act(() => {
1468+
fireEvent.change(node.querySelector(protocolInputID), {
1469+
target: { value: '1' },
1470+
});
1471+
});
1472+
1473+
sinon.assert.calledWithMatch(
1474+
onChange.lastCall,
1475+
{
1476+
formData: {
1477+
protocol: 'balanced',
1478+
},
1479+
schema,
1480+
},
1481+
'root_protocol'
1482+
);
1483+
1484+
expect(node.querySelector(protocolInputID).value).to.equal('1');
1485+
});
1486+
describe('Should modify oneOf radio button when the defaults are set.', () => {
14331487
const schema = {
14341488
type: 'object',
14351489
properties: {
@@ -1476,35 +1530,68 @@ describeRepeated('Form common', (createFormComponent) => {
14761530
'ui:label': false,
14771531
},
14781532
};
1479-
1480-
const { node, onChange } = createFormComponent({
1481-
schema,
1482-
uiSchema,
1483-
});
1484-
14851533
const notApplicableInputID = '#root_a-1';
14861534
const NoInputID = '#root_a-0';
1487-
expect(node.querySelector(notApplicableInputID).checked).to.equal(true);
14881535

1489-
act(() => {
1490-
fireEvent.click(node.querySelector(NoInputID));
1491-
});
1536+
it('Test with default constAsDefaults', () => {
1537+
const { node, onChange } = createFormComponent({
1538+
schema,
1539+
uiSchema,
1540+
});
14921541

1493-
sinon.assert.calledWithMatch(
1494-
onChange.lastCall,
1495-
{
1496-
formData: {
1497-
a: false,
1542+
expect(node.querySelector(notApplicableInputID).checked).to.equal(true);
1543+
1544+
act(() => {
1545+
fireEvent.click(node.querySelector(NoInputID));
1546+
});
1547+
1548+
sinon.assert.calledWithMatch(
1549+
onChange.lastCall,
1550+
{
1551+
formData: {
1552+
a: false,
1553+
},
1554+
schema,
1555+
uiSchema,
14981556
},
1557+
'root_a'
1558+
);
1559+
1560+
expect(node.querySelector(NoInputID).checked).to.equal(true);
1561+
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
1562+
expect(node.querySelector('#root_b')).to.exist;
1563+
});
1564+
it('Test with constAsDefaults set to "never"', () => {
1565+
const { node, onChange } = createFormComponent({
14991566
schema,
15001567
uiSchema,
1501-
},
1502-
'root_a'
1503-
);
1568+
experimental_defaultFormStateBehavior: {
1569+
constAsDefaults: 'never',
1570+
},
1571+
});
1572+
1573+
expect(node.querySelector(notApplicableInputID).checked).to.equal(true);
15041574

1505-
expect(node.querySelector(NoInputID).checked).to.equal(true);
1506-
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
1507-
expect(node.querySelector('#root_b')).to.exist;
1575+
act(() => {
1576+
fireEvent.click(node.querySelector(NoInputID));
1577+
});
1578+
1579+
sinon.assert.calledWithMatch(
1580+
onChange.lastCall,
1581+
{
1582+
formData: {
1583+
a: false,
1584+
},
1585+
schema,
1586+
uiSchema,
1587+
},
1588+
'root_a'
1589+
);
1590+
1591+
expect(node.querySelector(NoInputID).checked).to.equal(true);
1592+
expect(node.querySelector(notApplicableInputID).checked).to.equal(false);
1593+
expect(node.querySelector('#root_b')).to.exist;
1594+
});
15081595
});
15091596
it('Should modify oneOf object with references when the defaults are set.', () => {
15101597
const schema = {

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)