1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _prodInvariant = require('./reactProdInvariant'),
|
14 | _assign = require('object-assign');
|
15 |
|
16 | var LinkedValueUtils = require('./LinkedValueUtils');
|
17 | var ReactDOMComponentTree = require('./ReactDOMComponentTree');
|
18 | var ReactUpdates = require('./ReactUpdates');
|
19 |
|
20 | var invariant = require('fbjs/lib/invariant');
|
21 | var warning = require('fbjs/lib/warning');
|
22 |
|
23 | var didWarnValueLink = false;
|
24 | var didWarnValDefaultVal = false;
|
25 |
|
26 | function forceUpdateIfMounted() {
|
27 | if (this._rootNodeID) {
|
28 |
|
29 | ReactDOMTextarea.updateWrapper(this);
|
30 | }
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var ReactDOMTextarea = {
|
49 | getHostProps: function (inst, props) {
|
50 | !(props.dangerouslySetInnerHTML == null) ? process.env.NODE_ENV !== 'production' ? invariant(false, '`dangerouslySetInnerHTML` does not make sense on <textarea>.') : _prodInvariant('91') : void 0;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | var hostProps = _assign({}, props, {
|
58 | value: undefined,
|
59 | defaultValue: undefined,
|
60 | children: '' + inst._wrapperState.initialValue,
|
61 | onChange: inst._wrapperState.onChange
|
62 | });
|
63 |
|
64 | return hostProps;
|
65 | },
|
66 |
|
67 | mountWrapper: function (inst, props) {
|
68 | if (process.env.NODE_ENV !== 'production') {
|
69 | LinkedValueUtils.checkPropTypes('textarea', props, inst._currentElement._owner);
|
70 | if (props.valueLink !== undefined && !didWarnValueLink) {
|
71 | process.env.NODE_ENV !== 'production' ? warning(false, '`valueLink` prop on `textarea` is deprecated; set `value` and `onChange` instead.') : void 0;
|
72 | didWarnValueLink = true;
|
73 | }
|
74 | if (props.value !== undefined && props.defaultValue !== undefined && !didWarnValDefaultVal) {
|
75 | process.env.NODE_ENV !== 'production' ? warning(false, 'Textarea elements must be either controlled or uncontrolled ' + '(specify either the value prop, or the defaultValue prop, but not ' + 'both). Decide between using a controlled or uncontrolled textarea ' + 'and remove one of these props. More info: ' + 'https://fb.me/react-controlled-components') : void 0;
|
76 | didWarnValDefaultVal = true;
|
77 | }
|
78 | }
|
79 |
|
80 | var value = LinkedValueUtils.getValue(props);
|
81 | var initialValue = value;
|
82 |
|
83 |
|
84 | if (value == null) {
|
85 | var defaultValue = props.defaultValue;
|
86 |
|
87 | var children = props.children;
|
88 | if (children != null) {
|
89 | if (process.env.NODE_ENV !== 'production') {
|
90 | process.env.NODE_ENV !== 'production' ? warning(false, 'Use the `defaultValue` or `value` props instead of setting ' + 'children on <textarea>.') : void 0;
|
91 | }
|
92 | !(defaultValue == null) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'If you supply `defaultValue` on a <textarea>, do not pass children.') : _prodInvariant('92') : void 0;
|
93 | if (Array.isArray(children)) {
|
94 | !(children.length <= 1) ? process.env.NODE_ENV !== 'production' ? invariant(false, '<textarea> can only have at most one child.') : _prodInvariant('93') : void 0;
|
95 | children = children[0];
|
96 | }
|
97 |
|
98 | defaultValue = '' + children;
|
99 | }
|
100 | if (defaultValue == null) {
|
101 | defaultValue = '';
|
102 | }
|
103 | initialValue = defaultValue;
|
104 | }
|
105 |
|
106 | inst._wrapperState = {
|
107 | initialValue: '' + initialValue,
|
108 | listeners: null,
|
109 | onChange: _handleChange.bind(inst)
|
110 | };
|
111 | },
|
112 |
|
113 | updateWrapper: function (inst) {
|
114 | var props = inst._currentElement.props;
|
115 |
|
116 | var node = ReactDOMComponentTree.getNodeFromInstance(inst);
|
117 | var value = LinkedValueUtils.getValue(props);
|
118 | if (value != null) {
|
119 |
|
120 |
|
121 | var newValue = '' + value;
|
122 |
|
123 |
|
124 | if (newValue !== node.value) {
|
125 | node.value = newValue;
|
126 | }
|
127 | if (props.defaultValue == null) {
|
128 | node.defaultValue = newValue;
|
129 | }
|
130 | }
|
131 | if (props.defaultValue != null) {
|
132 | node.defaultValue = props.defaultValue;
|
133 | }
|
134 | },
|
135 |
|
136 | postMountWrapper: function (inst) {
|
137 |
|
138 |
|
139 | var node = ReactDOMComponentTree.getNodeFromInstance(inst);
|
140 | var textContent = node.textContent;
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | if (textContent === inst._wrapperState.initialValue) {
|
147 | node.value = textContent;
|
148 | }
|
149 | }
|
150 | };
|
151 |
|
152 | function _handleChange(event) {
|
153 | var props = this._currentElement.props;
|
154 | var returnValue = LinkedValueUtils.executeOnChange(props, event);
|
155 | ReactUpdates.asap(forceUpdateIfMounted, this);
|
156 | return returnValue;
|
157 | }
|
158 |
|
159 | module.exports = ReactDOMTextarea; |
\ | No newline at end of file |