Skip to content

Commit 4e9dbb2

Browse files
author
Ciro S. Costa
committed
Fixes checkedLink inconsistency -controlled inputs
Getting the checked value directly from `this.props.checked` was causing an inconsistency between the expected behavior when using `checkedLink`. This commit addresses that inconsistent behavior and adds test to check if it is producing controlled inputs as we expect for checkbox, text and radio types.
1 parent 57de1b0 commit 4e9dbb2

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/browser/ui/dom/components/ReactDOMInput.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ var ReactDOMInput = ReactCompositeComponent.createClass({
103103

104104
componentDidUpdate: function(prevProps, prevState, prevContext) {
105105
var rootNode = this.getDOMNode();
106-
if (this.props.checked != null) {
106+
var checked = LinkedValueUtils.getChecked(this);
107+
if (checked != null) {
107108
DOMPropertyOperations.setValueForProperty(
108109
rootNode,
109110
'checked',
110-
this.props.checked || false
111+
checked
111112
);
112113
}
113114

src/browser/ui/dom/components/__tests__/ReactDOMInput-test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,50 @@ describe('ReactDOMInput', function() {
281281

282282
});
283283

284+
it('should make radio input controlled with checkedLink', function() {
285+
var RadioInput = React.createClass({
286+
render: function () {
287+
return (
288+
<input
289+
type="radio"
290+
checkedLink={new ReactLink(false, emptyFunction)} />
291+
);
292+
}
293+
});
294+
295+
var stub = ReactTestUtils.renderIntoDocument(<RadioInput />);
296+
var radio = stub.getDOMNode();
297+
298+
expect(radio.checked).toBe(false);
299+
300+
radio.checked = true;
301+
ReactTestUtils.Simulate.change(radio);
302+
303+
expect(radio.checked).toEqual(false);
304+
});
305+
306+
it('should make text input controlled with valueLink', function() {
307+
var TextInput = React.createClass({
308+
render: function () {
309+
return (
310+
<input
311+
type="text"
312+
valueLink={new ReactLink('initial', emptyFunction)} />
313+
);
314+
}
315+
});
316+
317+
var stub = ReactTestUtils.renderIntoDocument(<TextInput />);
318+
var node = stub.getDOMNode();
319+
320+
expect(node.value).toEqual('initial');
321+
322+
node.value = 'something';
323+
ReactTestUtils.Simulate.change(node);
324+
325+
expect(node.value).toEqual('initial');
326+
});
327+
284328
it('should support checkedLink', function() {
285329
var container = document.createElement('div');
286330
var link = new ReactLink(true, mocks.getMockFunction());
@@ -299,6 +343,28 @@ describe('ReactDOMInput', function() {
299343
expect(link.requestChange.mock.calls[0][0]).toEqual(false);
300344
});
301345

346+
it('should make checkbox input controlled with checkedLink', function() {
347+
var CheckboxInput = React.createClass({
348+
render: function () {
349+
return (
350+
<input
351+
type="checkbox"
352+
checkedLink={new ReactLink(true, emptyFunction)} />
353+
);
354+
}
355+
});
356+
357+
var stub = ReactTestUtils.renderIntoDocument(<CheckboxInput />);
358+
var node = stub.getDOMNode();
359+
360+
expect(node.checked).toBe(true);
361+
362+
node.checked = false;
363+
ReactTestUtils.Simulate.change(node);
364+
365+
expect(node.checked).toEqual(true);
366+
});
367+
302368
it('should warn with checked and no onChange handler', function() {
303369
var oldWarn = console.warn;
304370
try {

0 commit comments

Comments
 (0)