Skip to content

Commit 26daa54

Browse files
committed
Mutate all then query all, and rename test helper
1 parent 5969037 commit 26daa54

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

packages/react-dom-bindings/src/client/ReactDOMInput.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,6 @@ export function restoreControlledInputState(element: Element, props: Object) {
388388
);
389389
}
390390

391-
// We need update the tracked value on the named cousin since the value
392-
// was changed but the input saw no event or value set
393-
updateValueIfChanged(otherNode);
394-
395391
// If this is a controlled radio button group, forcing the input that
396392
// was previously checked to update will cause it to be come re-checked
397393
// as appropriate.
@@ -406,10 +402,17 @@ export function restoreControlledInputState(element: Element, props: Object) {
406402
otherProps.name,
407403
);
408404
}
405+
406+
// If any updateInput() call set .checked to true, an input in this group
407+
// (often, `rootNode` itself) may have become unchecked
408+
for (let i = 0; i < group.length; i++) {
409+
const otherNode = ((group[i]: any): HTMLInputElement);
410+
if (otherNode.form !== rootNode.form) {
411+
continue;
412+
}
413+
updateValueIfChanged(otherNode);
414+
}
409415
}
410-
// Calling updateInput on the other radio buttons may have caused our event
411-
// target's checkedness to change back
412-
updateValueIfChanged(rootNode);
413416
}
414417

415418
// In Chrome, assigning defaultValue to certain input types triggers input validation.

packages/react-dom/src/__tests__/ReactDOMInput-test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('ReactDOMInput', () => {
6161
];
6262
}
6363

64-
function assertInputTrackingIsClean(parent) {
64+
function assertInputTrackingIsCurrent(parent) {
6565
parent.querySelectorAll('input, textarea, select').forEach(input => {
6666
const [trackedValue, currentValue] =
6767
getTrackedAndCurrentInputValue(input);
@@ -1205,7 +1205,7 @@ describe('ReactDOMInput', () => {
12051205
expect(isCheckedDirty(aNode)).toBe(true);
12061206
expect(isCheckedDirty(bNode)).toBe(true);
12071207
expect(isCheckedDirty(cNode)).toBe(true);
1208-
assertInputTrackingIsClean(container);
1208+
assertInputTrackingIsCurrent(container);
12091209

12101210
setUntrackedChecked.call(bNode, true);
12111211
expect(aNode.checked).toBe(false);
@@ -1232,7 +1232,7 @@ describe('ReactDOMInput', () => {
12321232
expect(isCheckedDirty(aNode)).toBe(true);
12331233
expect(isCheckedDirty(bNode)).toBe(true);
12341234
expect(isCheckedDirty(cNode)).toBe(true);
1235-
assertInputTrackingIsClean(container);
1235+
assertInputTrackingIsCurrent(container);
12361236
});
12371237

12381238
it('should check the correct radio when the selected name moves', () => {
@@ -1271,13 +1271,13 @@ describe('ReactDOMInput', () => {
12711271
const firstRadioNode = ReactDOM.findDOMNode(stub).childNodes[1];
12721272
expect(isCheckedDirty(firstRadioNode)).toBe(true);
12731273
expect(firstRadioNode.checked).toBe(false);
1274-
assertInputTrackingIsClean(container);
1274+
assertInputTrackingIsCurrent(container);
12751275
dispatchEventOnNode(buttonNode, 'click');
12761276
expect(firstRadioNode.checked).toBe(true);
1277-
assertInputTrackingIsClean(container);
1277+
assertInputTrackingIsCurrent(container);
12781278
dispatchEventOnNode(buttonNode, 'click');
12791279
expect(firstRadioNode.checked).toBe(false);
1280-
assertInputTrackingIsClean(container);
1280+
assertInputTrackingIsCurrent(container);
12811281
});
12821282

12831283
it("shouldn't get tricked by changing radio names, part 2", () => {
@@ -1306,7 +1306,7 @@ describe('ReactDOMInput', () => {
13061306
expect(two.checked).toBe(false);
13071307
expect(isCheckedDirty(one)).toBe(true);
13081308
expect(isCheckedDirty(two)).toBe(true);
1309-
assertInputTrackingIsClean(container);
1309+
assertInputTrackingIsCurrent(container);
13101310

13111311
ReactDOM.render(
13121312
<div>
@@ -1331,7 +1331,7 @@ describe('ReactDOMInput', () => {
13311331
expect(two.checked).toBe(true);
13321332
expect(isCheckedDirty(one)).toBe(true);
13331333
expect(isCheckedDirty(two)).toBe(true);
1334-
assertInputTrackingIsClean(container);
1334+
assertInputTrackingIsCurrent(container);
13351335
});
13361336

13371337
it('should control radio buttons if the tree updates during render', () => {
@@ -1395,7 +1395,7 @@ describe('ReactDOMInput', () => {
13951395
expect(bNode.checked).toBe(true);
13961396
expect(isCheckedDirty(aNode)).toBe(true);
13971397
expect(isCheckedDirty(bNode)).toBe(true);
1398-
assertInputTrackingIsClean(container);
1398+
assertInputTrackingIsCurrent(container);
13991399

14001400
setUntrackedChecked.call(aNode, true);
14011401
// This next line isn't necessary in a proper browser environment, but
@@ -1411,7 +1411,7 @@ describe('ReactDOMInput', () => {
14111411
expect(bNode.checked).toBe(true);
14121412
expect(isCheckedDirty(aNode)).toBe(true);
14131413
expect(isCheckedDirty(bNode)).toBe(true);
1414-
assertInputTrackingIsClean(container);
1414+
assertInputTrackingIsCurrent(container);
14151415
});
14161416

14171417
it('should warn with value and no onChange handler and readOnly specified', () => {
@@ -1807,7 +1807,7 @@ describe('ReactDOMInput', () => {
18071807
container,
18081808
);
18091809
expect(isCheckedDirty(input)).toBe(true);
1810-
assertInputTrackingIsClean(container);
1810+
assertInputTrackingIsCurrent(container);
18111811
});
18121812

18131813
it('should warn if radio checked false changes to become uncontrolled', () => {

0 commit comments

Comments
 (0)