Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request to allow controlled checkboxes to remove checked when assigned null #7530

Closed
wants to merge 2 commits into from
Closed
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: 3 additions & 3 deletions src/renderers/dom/client/wrappers/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var ReactDOMInput = {
defaultChecked: undefined,
defaultValue: undefined,
value: value != null ? value : inst._wrapperState.initialValue,
checked: checked != null ? checked : inst._wrapperState.initialChecked,
checked: checked !== undefined ? checked : inst._wrapperState.initialChecked,
onChange: inst._wrapperState.onChange,
});

Expand Down Expand Up @@ -146,7 +146,7 @@ var ReactDOMInput = {

var defaultValue = props.defaultValue;
inst._wrapperState = {
initialChecked: props.checked != null ? props.checked : props.defaultChecked,
initialChecked: props.checked !== undefined ? props.checked : props.defaultChecked,
initialValue: props.value != null ? props.value : defaultValue,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If accepted, wouldn't this introduce a new inconsistency between controlled text inputs and checkboxes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Would it be unreasonable to extend this to text inputs?

Copy link
Contributor

@syranide syranide Aug 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has been discussed before and rejected. I don't think anyone feels super strongly about the current implementation, but changing it means breaking a lot of code.

listeners: null,
onChange: _handleChange.bind(inst),
Expand Down Expand Up @@ -192,7 +192,7 @@ var ReactDOMInput = {

// TODO: Shouldn't this be getChecked(props)?
var checked = props.checked;
if (checked != null) {
if (checked !== undefined) {
DOMPropertyOperations.setValueForProperty(
ReactDOMComponentTree.getNodeFromInstance(inst),
'checked',
Expand Down
22 changes: 22 additions & 0 deletions src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,4 +843,26 @@ describe('ReactDOMInput', function() {
'node.setAttribute("checked", "")',
]);
});

it('removes the checked state when assigned null after initially rendering true', function() {
var Input = React.createClass({
getInitialState() {
return { checked: true };
},

render() {
return (
<form>
<input ref="input" type="checkbox" checked={this.state.checked} />
</form>
);
},
});

var el = ReactTestUtils.renderIntoDocument(<Input />);

el.setState({ checked: null });

expect(el.refs.input.checked).toBe(false);
});
});