Skip to content

Commit

Permalink
Cleanup and bug fixes for merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
jim committed Apr 4, 2016
1 parent 574328a commit bec734c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('ReactDOMTextarea', function() {

// Changing `defaultValue` should change if no value set.
renderTextarea(<textarea defaultValue="gorilla" />, container, true);
expect(node.value).toEqual('gorilla');
expect(node.value).toEqual('giraffe');

node.value = 'cat';

Expand Down Expand Up @@ -163,27 +163,27 @@ describe('ReactDOMTextarea', function() {
it('should take updates to `defaultValue` for uncontrolled textarea', function() {
var container = document.createElement('div');

var node = ReactDOM.render(<textarea type="text" defaultValue="0" />, container);
var node = ReactDOM.render(<textarea defaultValue="0" />, container);

expect(node.value).toBe('0');

ReactDOM.render(<textarea type="text" defaultValue="1" />, container);
ReactDOM.render(<textarea defaultValue="1" />, container);

expect(node.value).toBe('1');
expect(node.value).toBe('0');
});

it('should take updates to children in lieu of `defaultValue` for uncontrolled textarea', function() {
var container = document.createElement('div');

var node = ReactDOM.render(<textarea type="text" defaultValue="0" />, container);
var node = ReactDOM.render(<textarea defaultValue="0" />, container);

expect(node.value).toBe('0');

spyOn(console, 'error'); // deprecation warning for `children` content

ReactDOM.render(<textarea type="text">1</textarea>, container);
ReactDOM.render(<textarea>1</textarea>, container);

expect(node.value).toBe('1');
expect(node.value).toBe('0');
});

it('should not incur unnecessary DOM mutations', function() {
Expand All @@ -206,7 +206,7 @@ describe('ReactDOMTextarea', function() {
expect(nodeValueSetter.mock.calls.length).toBe(0);

ReactDOM.render(<textarea value="b" onChange={emptyFunction} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
expect(nodeValueSetter.mock.calls.length).toBe(2); // must update both value and defaultValue
});

it('should properly control a value of number `0`', function() {
Expand All @@ -230,7 +230,7 @@ describe('ReactDOMTextarea', function() {

// Changing children should cause value to change (new behavior of `defaultValue`)
stub = ReactDOM.render(<textarea>gorilla</textarea>, container);
expect(node.value).toEqual('gorilla');
expect(node.value).toEqual('giraffe');
});

it('should not keep value when switching to uncontrolled element if not changed', function() {
Expand All @@ -242,7 +242,7 @@ describe('ReactDOMTextarea', function() {

ReactDOM.render(<textarea defaultValue="gorilla"></textarea>, container);

expect(node.value).toEqual('gorilla');
expect(node.value).toEqual('kitten');
});

it('should keep value when switching to uncontrolled element if changed', function() {
Expand Down
10 changes: 9 additions & 1 deletion src/renderers/dom/shared/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ var DOMPropertyOperations = {
('' + node[propName]) !== ('' + value)) {
// Contrary to `setAttribute`, object properties are properly
// `toString`ed by IE8/9.
node[propName] = value;

if (propName === 'defaultValue') {
var tmp = node.value;
node[propName] = value;
node.value = tmp;
}
else {
node[propName] = value;
}
}
} else {
var attributeName = propertyInfo.attributeName;
Expand Down
12 changes: 12 additions & 0 deletions src/renderers/dom/shared/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ function trapBubbledEventsLocal() {
}
}

function initializeInitialInputValue() {
var inst = this;
invariant(inst._rootNodeID, 'Must be mounted to initialize initial input value');
var node = getNode(inst);
node.value = node.defaultValue;
}

function postUpdateSelectWrapper() {
ReactDOMSelect.postUpdateWrapper(this);
}
Expand Down Expand Up @@ -502,6 +509,9 @@ ReactDOMComponent.Mixin = {
ReactDOMTextarea.mountWrapper(this, props, nativeParent);
props = ReactDOMTextarea.getNativeProps(this, props);
transaction.getReactMountReady().enqueue(trapBubbledEventsLocal, this);
if (!props.value && props.defaultValue) {
transaction.getReactMountReady().enqueue(initializeInitialInputValue, this);
}
break;
}

Expand Down Expand Up @@ -799,6 +809,8 @@ ReactDOMComponent.Mixin = {
ReactDOMTextarea.updateWrapper(this);
lastProps = ReactDOMTextarea.getNativeProps(this, lastProps);
nextProps = ReactDOMTextarea.getNativeProps(this, nextProps);
// if(lastProps.value !== nextProps.value) throw Error("WFT: "+lastProps.value+" "+nextProps.value);
// if(lastProps.defaultValue !== nextProps.defaultValue) throw Error("WFT: "+lastProps.defaultValue+" "+nextProps.defaultValue);
break;
}

Expand Down

0 comments on commit bec734c

Please sign in to comment.