Merge pull request #274 from chenglou/textarea-patch

fix textarea `value` of number 0
This commit is contained in:
Paul O’Shannessy
2013-09-10 18:01:33 -07:00
4 changed files with 55 additions and 6 deletions
+8 -5
View File
@@ -50,9 +50,10 @@ var instancesByReactID = {};
var ReactDOMInput = ReactCompositeComponent.createClass({
getInitialState: function() {
var defaultValue = this.props.defaultValue;
return {
checked: this.props.defaultChecked || false,
value: this.props.defaultValue != null ? this.props.defaultValue : ''
value: defaultValue != null && defaultValue !== false ? defaultValue : ''
};
},
@@ -69,9 +70,11 @@ var ReactDOMInput = ReactCompositeComponent.createClass({
props.defaultValue = null;
props.checked =
this.props.checked != null ? this.props.checked : this.state.checked;
// Cast `this.props.value` to a string so equality checks pass.
props.value =
this.props.value != null ? '' + this.props.value : this.state.value;
props.value = this.props.value != null && this.props.value !== false
? '' + this.props.value
: this.state.value;
props.onChange = this._handleChange;
return input(props, this.props.children);
@@ -102,7 +105,7 @@ var ReactDOMInput = ReactCompositeComponent.createClass({
DOMPropertyOperations.setValueForProperty(
rootNode,
'value',
'' + this.props.value || ''
this.props.value !== false ? '' + this.props.value : ''
);
}
},
+1 -1
View File
@@ -117,7 +117,7 @@ var ReactDOMTextarea = ReactCompositeComponent.createClass({
DOMPropertyOperations.setValueForProperty(
rootNode,
'value',
this.props.value || ''
this.props.value !== false ? '' + this.props.value : ''
);
}
},
@@ -45,6 +45,13 @@ describe('ReactDOMInput', function() {
expect(node.value).toBe('0');
});
it('should display "" for `defaultValue` of `false`', function() {
var stub = <input type="text" defaultValue={false} />;
var node = renderTextInput(stub);
expect(node.value).toBe('');
});
it('should display `value` of number 0', function() {
var stub = <input type="text" value={0} />;
var node = renderTextInput(stub);
@@ -52,6 +59,22 @@ describe('ReactDOMInput', function() {
expect(node.value).toBe('0');
});
it('should display "" for `value` of `false`', function() {
var stub = <input type="text" value={false} />;
var node = renderTextInput(stub);
expect(node.value).toBe('');
});
it('should properly control a value of number `0`', function() {
var stub = <input type="text" value={0} />;
var node = renderTextInput(stub);
node.value = 'giraffe';
ReactTestUtils.Simulate.input(node);
expect(node.value).toBe('0');
});
it('should control radio buttons', function() {
var RadioGroup = React.createClass({
render: function() {
@@ -58,6 +58,13 @@ describe('ReactDOMTextarea', function() {
expect(node.value).toBe('0');
});
it('should display "" for `defaultValue` of `false`', function() {
var stub = <textarea type="text" defaultValue={false} />;
var node = renderTextarea(stub);
expect(node.value).toBe('');
});
it('should allow setting `value`', function() {
var stub = <textarea value="giraffe" />;
var node = renderTextarea(stub);
@@ -75,6 +82,22 @@ describe('ReactDOMTextarea', function() {
expect(node.value).toBe('0');
});
it('should display "" for `value` of `false`', function() {
var stub = <textarea type="text" value={false} />;
var node = renderTextarea(stub);
expect(node.value).toBe('');
});
it('should properly control a value of number `0`', function() {
var stub = <textarea value={0} />;
var node = renderTextarea(stub);
node.value = 'giraffe';
ReactTestUtils.Simulate.input(node);
expect(node.value).toBe('0');
});
it('should treat children like `defaultValue`', function() {
spyOn(console, 'warn');