Skip to content

Commit

Permalink
Fix the "checked" attribute is not initially set on the input
Browse files Browse the repository at this point in the history
  • Loading branch information
Wensheng Xu committed Jun 27, 2018
1 parent 6d6de60 commit 3c670a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,12 @@ describe('ReactDOMInput', () => {
const cNode = stub.refs.c;

expect(aNode.checked).toBe(true);
expect(bNode.getAttribute('checked')).toBe(null);
expect(bNode.checked).toBe(false);
expect(bNode.getAttribute('checked')).toBe(null);
// c is in a separate form and shouldn't be affected at all here
expect(cNode.checked).toBe(true);
expect(cNode.getAttribute('checked')).toBe('checked');

bNode.checked = true;
// This next line isn't necessary in a proper browser environment, but
Expand All @@ -750,6 +753,11 @@ describe('ReactDOMInput', () => {
aNode.checked = false;
expect(cNode.checked).toBe(true);

// The original 'checked' attribute should be unchanged
expect(aNode.getAttribute('checked')).toBe(null);
expect(bNode.getAttribute('checked')).toBe(null);
expect(cNode.getAttribute('checked')).toBe('checked');

// Now let's run the actual ReactDOMInput change event handler
ReactTestUtils.Simulate.change(bNode);

Expand Down
4 changes: 4 additions & 0 deletions packages/react-dom/src/client/ReactDOMFiberInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ export function postMountWrapper(
}
node.defaultChecked = !node.defaultChecked;
node.defaultChecked = !node.defaultChecked;
// Set the "checked" attribute initially.
if (props.hasOwnProperty('defaultChecked') && !!props.defaultChecked) {
node.setAttribute('checked', 'checked');
}
if (name !== '') {
node.name = name;
}
Expand Down

0 comments on commit 3c670a4

Please sign in to comment.