From 3186f4ceabbf059ac141c791ad0be782bc668654 Mon Sep 17 00:00:00 2001 From: jim Date: Mon, 4 Apr 2016 10:39:07 -0700 Subject: [PATCH] Cleanup and bug fixes for merge. --- .../dom/client/wrappers/ReactDOMInput.js | 16 +++- .../dom/client/wrappers/ReactDOMTextarea.js | 84 +++++++++++-------- .../wrappers/__tests__/ReactDOMInput-test.js | 13 ++- .../__tests__/ReactDOMTextarea-test.js | 29 ++++--- src/renderers/dom/shared/ReactDOMComponent.js | 2 + 5 files changed, 97 insertions(+), 47 deletions(-) diff --git a/src/renderers/dom/client/wrappers/ReactDOMInput.js b/src/renderers/dom/client/wrappers/ReactDOMInput.js index 56814142e39dc..150b4035d0e1e 100644 --- a/src/renderers/dom/client/wrappers/ReactDOMInput.js +++ b/src/renderers/dom/client/wrappers/ReactDOMInput.js @@ -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, }); @@ -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), }; @@ -217,6 +219,18 @@ var ReactDOMInput = { } } }, + + finishMounting: function() { + var inst = this; + var props = inst._currentElement.props; + if (!props.value && props.defaultValue) { + invariant(inst._rootNodeID, 'Must be mounted to initialize initial input value'); + var node = ReactDOMComponentTree.getNodeFromInstance(inst); + var tmp = node.value; + node.defaultValue = ''+props.defaultValue; + node.value = tmp; + } + }, }; function _handleChange(event) { diff --git a/src/renderers/dom/client/wrappers/ReactDOMTextarea.js b/src/renderers/dom/client/wrappers/ReactDOMTextarea.js index 37213bf319c0d..498519ceedc6c 100644 --- a/src/renderers/dom/client/wrappers/ReactDOMTextarea.js +++ b/src/renderers/dom/client/wrappers/ReactDOMTextarea.js @@ -66,44 +66,12 @@ var ReactDOMTextarea = { var value = LinkedValueUtils.getValue(props); - // only bother fetching default value if we're going to use it - if (value == null) { - var defaultValue = props.defaultValue; - // TODO (yungsters): Remove support for children content in , container); + ReactDOM.render(, container); - expect(node.value).toBe('1'); + expect(node.value).toBe('0'); }); it('should not incur unnecessary DOM mutations', function() { @@ -228,9 +237,9 @@ describe('ReactDOMTextarea', function() { expect(console.error.argsForCall.length).toBe(1); expect(node.value).toBe('giraffe'); - // Changing children should cause value to change (new behavior of `defaultValue`) + // Changing children should do nothing, it functions like `defaultValue`. stub = ReactDOM.render(, 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() { @@ -242,7 +251,7 @@ describe('ReactDOMTextarea', function() { ReactDOM.render(, container); - expect(node.value).toEqual('gorilla'); + expect(node.value).toEqual('kitten'); }); it('should keep value when switching to uncontrolled element if changed', function() { diff --git a/src/renderers/dom/shared/ReactDOMComponent.js b/src/renderers/dom/shared/ReactDOMComponent.js index 808706d62b8dc..5283330866fa6 100644 --- a/src/renderers/dom/shared/ReactDOMComponent.js +++ b/src/renderers/dom/shared/ReactDOMComponent.js @@ -488,6 +488,7 @@ ReactDOMComponent.Mixin = { ReactDOMInput.mountWrapper(this, props, nativeParent); props = ReactDOMInput.getNativeProps(this, props); transaction.getReactMountReady().enqueue(trapBubbledEventsLocal, this); + transaction.getReactMountReady().enqueue(ReactDOMInput.finishMounting, this); break; case 'option': ReactDOMOption.mountWrapper(this, props, nativeParent); @@ -502,6 +503,7 @@ ReactDOMComponent.Mixin = { ReactDOMTextarea.mountWrapper(this, props, nativeParent); props = ReactDOMTextarea.getNativeProps(this, props); transaction.getReactMountReady().enqueue(trapBubbledEventsLocal, this); + transaction.getReactMountReady().enqueue(ReactDOMTextarea.finishMounting, this); break; }