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 d049ebf
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
4 changes: 3 additions & 1 deletion 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 = {
}, props, {
defaultChecked: undefined,
defaultValue: undefined,
value: value != null ? value : props.defaultValue,
value: value != null ? value : inst._wrapperState.initialValue,
checked: checked != null ? checked : inst._wrapperState.initialChecked,
onChange: inst._wrapperState.onChange,
});
Expand Down Expand Up @@ -138,8 +138,10 @@ var ReactDOMInput = {
warnIfValueIsNull(props);
}

var defaultValue = props.defaultValue;
inst._wrapperState = {
initialChecked: props.defaultChecked || false,
initialValue: props.value ? props.value : defaultValue,
listeners: null,
onChange: _handleChange.bind(inst),
};
Expand Down
13 changes: 11 additions & 2 deletions src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('ReactDOMInput', function() {
var EventConstants;
var React;
var ReactDOM;
var ReactDOMServer;
var ReactDOMFeatureFlags;
var ReactLink;
var ReactTestUtils;
Expand All @@ -27,6 +28,7 @@ describe('ReactDOMInput', function() {
EventConstants = require('EventConstants');
React = require('React');
ReactDOM = require('ReactDOM');
ReactDOMServer = require('ReactDOMServer');
ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
ReactLink = require('ReactLink');
ReactTestUtils = require('ReactTestUtils');
Expand Down Expand Up @@ -67,7 +69,7 @@ describe('ReactDOMInput', function() {

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

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

it('should take `defaultValue` when changing to uncontrolled input', function() {
Expand All @@ -79,7 +81,14 @@ describe('ReactDOMInput', function() {

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

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

it('should take `defaultValue` when changing to uncontrolled input', function() {
var image = ReactDOMServer.renderToString(<input type="text" defaultValue="1" />);
var div = document.createElement('div');
div.innerHTML = image;
expect(div.firstChild.getAttribute('value')).toBe('1');
});

it('should display "foobar" for `defaultValue` of `objToString`', function() {
Expand Down
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 Down Expand Up @@ -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
11 changes: 10 additions & 1 deletion src/renderers/dom/shared/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,16 @@ var DOMPropertyOperations = {
('' + node[propName]) !== ('' + value)) {
// Contrary to `setAttribute`, object properties are properly
// `toString`ed by IE8/9.
node[propName] = value;

if (propName === 'defaultValue' && (node._tag === 'textarea' || node._tag === 'input')) {
// defaultValue is a special property because it can perturb the value property
var tmp = node.value;
node[propName] = value;
node.value = tmp;
}
else {
node[propName] = value;
}
}
} else {
var attributeName = propertyInfo.attributeName;
Expand Down
13 changes: 13 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 @@ -488,6 +495,9 @@ ReactDOMComponent.Mixin = {
ReactDOMInput.mountWrapper(this, props, nativeParent);
props = ReactDOMInput.getNativeProps(this, props);
transaction.getReactMountReady().enqueue(trapBubbledEventsLocal, this);
if (!props.value && props.defaultValue) {
transaction.getReactMountReady().enqueue(initializeInitialInputValue, this);
}
break;
case 'option':
ReactDOMOption.mountWrapper(this, props, nativeParent);
Expand All @@ -502,6 +512,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

0 comments on commit d049ebf

Please sign in to comment.