Skip to content

Commit 9aabd4c

Browse files
author
jim
committed
Cleanup and bug fixes for merge.
1 parent 574328a commit 9aabd4c

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/renderers/dom/client/wrappers/__tests__/ReactDOMTextarea-test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('ReactDOMTextarea', function() {
5050

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

5555
node.value = 'cat';
5656

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

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

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

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

172-
expect(node.value).toBe('1');
172+
expect(node.value).toBe('0');
173173
});
174174

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

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

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

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

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

186-
expect(node.value).toBe('1');
186+
expect(node.value).toBe('0');
187187
});
188188

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

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

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

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

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

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

245-
expect(node.value).toEqual('gorilla');
245+
expect(node.value).toEqual('kitten');
246246
});
247247

248248
it('should keep value when switching to uncontrolled element if changed', function() {

src/renderers/dom/shared/DOMPropertyOperations.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,15 @@ var DOMPropertyOperations = {
153153
('' + node[propName]) !== ('' + value)) {
154154
// Contrary to `setAttribute`, object properties are properly
155155
// `toString`ed by IE8/9.
156-
node[propName] = value;
156+
157+
if (propName === 'defaultValue') {
158+
var tmp = node.value;
159+
node[propName] = value;
160+
node.value = tmp;
161+
}
162+
else {
163+
node[propName] = value;
164+
}
157165
}
158166
} else {
159167
var attributeName = propertyInfo.attributeName;

src/renderers/dom/shared/ReactDOMComponent.js

+10
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ function trapBubbledEventsLocal() {
345345
}
346346
}
347347

348+
function initializeInitialInputValue() {
349+
var inst = this;
350+
invariant(inst._rootNodeID, 'Must be mounted to initialize initial input value');
351+
var node = getNode(inst);
352+
node.value = node.defaultValue;
353+
}
354+
348355
function postUpdateSelectWrapper() {
349356
ReactDOMSelect.postUpdateWrapper(this);
350357
}
@@ -502,6 +509,9 @@ ReactDOMComponent.Mixin = {
502509
ReactDOMTextarea.mountWrapper(this, props, nativeParent);
503510
props = ReactDOMTextarea.getNativeProps(this, props);
504511
transaction.getReactMountReady().enqueue(trapBubbledEventsLocal, this);
512+
if (!props.value && props.defaultValue) {
513+
transaction.getReactMountReady().enqueue(initializeInitialInputValue, this);
514+
}
505515
break;
506516
}
507517

0 commit comments

Comments
 (0)