1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _typeof from 'babel-runtime/helpers/typeof';
|
3 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
4 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
5 | import _inherits from 'babel-runtime/helpers/inherits';
|
6 |
|
7 | var _class, _temp, _initialiseProps;
|
8 |
|
9 | import React from 'react';
|
10 | import ReactDOM from 'react-dom';
|
11 | import PropTypes from 'prop-types';
|
12 | import classNames from 'classnames';
|
13 | import { obj, env } from '../util';
|
14 | import Base from './base';
|
15 |
|
16 | function onNextFrame(cb) {
|
17 | if (window.requestAnimationFrame) {
|
18 | return window.requestAnimationFrame(cb);
|
19 | }
|
20 | return window.setTimeout(cb, 1);
|
21 | }
|
22 |
|
23 | function clearNextFrameAction(nextFrameId) {
|
24 | if (window.cancelAnimationFrame) {
|
25 | window.cancelAnimationFrame(nextFrameId);
|
26 | } else {
|
27 | window.clearTimeout(nextFrameId);
|
28 | }
|
29 | }
|
30 |
|
31 |
|
32 | var isMacSafari = typeof navigator !== 'undefined' && navigator && navigator.userAgent ? navigator.userAgent.match(/^((?!chrome|android|windows).)*safari/i) : false;
|
33 |
|
34 | var hiddenStyle = {
|
35 | visibility: 'hidden',
|
36 | position: 'absolute',
|
37 | zIndex: '-1000',
|
38 | top: '-1000px',
|
39 | overflowY: 'hidden',
|
40 | left: 0,
|
41 | right: 0
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var TextArea = (_temp = _class = function (_Base) {
|
49 | _inherits(TextArea, _Base);
|
50 |
|
51 | function TextArea(props) {
|
52 | _classCallCheck(this, TextArea);
|
53 |
|
54 | var _this = _possibleConstructorReturn(this, _Base.call(this, props));
|
55 |
|
56 | _initialiseProps.call(_this);
|
57 |
|
58 | var value = void 0;
|
59 | if ('value' in props) {
|
60 | value = props.value;
|
61 | } else {
|
62 | value = props.defaultValue;
|
63 | }
|
64 |
|
65 | _this.state = {
|
66 | value: typeof value === 'undefined' ? '' : value
|
67 | };
|
68 | return _this;
|
69 | }
|
70 |
|
71 | TextArea.prototype.componentDidMount = function componentDidMount() {
|
72 | var autoHeight = this.props.autoHeight;
|
73 | if (autoHeight) {
|
74 | if ((typeof autoHeight === 'undefined' ? 'undefined' : _typeof(autoHeight)) === 'object') {
|
75 |
|
76 | this.setState(this._getMinMaxHeight(autoHeight, this.state.value));
|
77 | } else {
|
78 | this.setState({
|
79 | height: this._getHeight(this.state.value),
|
80 | overflowY: 'hidden'
|
81 | });
|
82 | }
|
83 | }
|
84 | };
|
85 |
|
86 | TextArea.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
|
87 | if (this.props.autoHeight && this.props.value !== prevProps.value) {
|
88 | this._resizeTextArea(this.props.value);
|
89 | }
|
90 | };
|
91 |
|
92 | TextArea.prototype._getMinMaxHeight = function _getMinMaxHeight(_ref, value) {
|
93 | var minRows = _ref.minRows,
|
94 | maxRows = _ref.maxRows;
|
95 |
|
96 | var node = ReactDOM.findDOMNode(this.helpRef);
|
97 | if (!node) {
|
98 | return {};
|
99 | }
|
100 | node.setAttribute('rows', minRows);
|
101 | var minHeight = node.clientHeight;
|
102 |
|
103 | node.setAttribute('rows', maxRows);
|
104 | var maxHeight = node.clientHeight;
|
105 |
|
106 | node.setAttribute('rows', '1');
|
107 | var height = this._getHeight(value);
|
108 |
|
109 | return {
|
110 | minHeight: minHeight,
|
111 | maxHeight: maxHeight,
|
112 | height: height,
|
113 | overflowY: height <= maxHeight ? 'hidden' : undefined
|
114 | };
|
115 | };
|
116 |
|
117 | TextArea.prototype._getHeight = function _getHeight(value) {
|
118 | var node = ReactDOM.findDOMNode(this.helpRef);
|
119 | if (!node) {
|
120 | return 0;
|
121 | }
|
122 | node.value = value;
|
123 |
|
124 | return node.scrollHeight;
|
125 | };
|
126 |
|
127 | TextArea.prototype.ieHack = function ieHack(value) {
|
128 |
|
129 |
|
130 | if (env.ieVersion === 9 && this.props.maxLength) {
|
131 | var maxLength = parseInt(this.props.maxLength);
|
132 | var len = this.getValueLength(value, true);
|
133 | if (len > maxLength && this.props.cutString) {
|
134 | value = value.replace(/\n/g, '\n\n');
|
135 | value = value.substr(0, maxLength);
|
136 | value = value.replace(/\n\n/g, '\n');
|
137 | }
|
138 | }
|
139 |
|
140 | this.props.autoHeight && this._resizeTextArea(value);
|
141 |
|
142 | return value;
|
143 | };
|
144 |
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | TextArea.prototype.getValueLength = function getValueLength(value) {
|
153 | var _props = this.props,
|
154 | maxLength = _props.maxLength,
|
155 | cutString = _props.cutString;
|
156 |
|
157 |
|
158 | var nv = '' + value;
|
159 | var strLen = this.props.getValueLength(nv);
|
160 | if (typeof strLen !== 'number') {
|
161 | strLen = nv.length;
|
162 | }
|
163 |
|
164 |
|
165 | if (env.ieVersion || isMacSafari) {
|
166 | strLen = strLen + nv.split('\n').length - 1;
|
167 | if (strLen > maxLength && cutString) {
|
168 | strLen = maxLength;
|
169 | }
|
170 | }
|
171 |
|
172 | return strLen;
|
173 | };
|
174 |
|
175 | TextArea.prototype.saveTextAreaRef = function saveTextAreaRef(textArea) {
|
176 | this.inputRef = textArea;
|
177 | };
|
178 |
|
179 | TextArea.prototype.saveHelpRef = function saveHelpRef(ref) {
|
180 | this.helpRef = ref;
|
181 | };
|
182 |
|
183 | TextArea.prototype.render = function render() {
|
184 | var _classNames, _classNames2;
|
185 |
|
186 | var _props2 = this.props,
|
187 | rows = _props2.rows,
|
188 | style = _props2.style,
|
189 | className = _props2.className,
|
190 | autoHeight = _props2.autoHeight,
|
191 | isPreview = _props2.isPreview,
|
192 | renderPreview = _props2.renderPreview,
|
193 | prefix = _props2.prefix,
|
194 | rtl = _props2.rtl,
|
195 | hasBorder = _props2.hasBorder,
|
196 | size = _props2.size,
|
197 | composition = _props2.composition;
|
198 |
|
199 |
|
200 | var cls = classNames(this.getClass(), (_classNames = {}, _classNames['' + prefix + size] = size === 'large' || 'size' === 'small', _classNames[prefix + 'input-textarea'] = true, _classNames[prefix + 'noborder'] = !hasBorder, _classNames[className] = !!className, _classNames));
|
201 |
|
202 | var props = this.getProps();
|
203 |
|
204 |
|
205 | var dataProps = obj.pickAttrsWith(this.props, 'data-');
|
206 |
|
207 |
|
208 | var others = obj.pickOthers(_extends({}, dataProps, TextArea.propTypes), this.props);
|
209 |
|
210 | var textareStyle = _extends({}, props.style, {
|
211 | height: this.state.height,
|
212 | minHeight: this.state.minHeight,
|
213 | maxHeight: this.state.maxHeight,
|
214 | overflowY: this.state.overflowY
|
215 | });
|
216 |
|
217 | var previewCls = classNames((_classNames2 = {}, _classNames2[prefix + 'input-textarea'] = true, _classNames2[prefix + 'form-preview'] = true, _classNames2[className] = !!className, _classNames2));
|
218 |
|
219 | var wrapStyle = autoHeight ? _extends({}, style, { position: 'relative' }) : style;
|
220 |
|
221 | if (isPreview) {
|
222 | var value = props.value;
|
223 |
|
224 | if ('renderPreview' in this.props) {
|
225 | return React.createElement(
|
226 | 'div',
|
227 | _extends({}, others, { className: previewCls }),
|
228 | renderPreview(value, this.props)
|
229 | );
|
230 | }
|
231 | return React.createElement(
|
232 | 'div',
|
233 | _extends({}, others, { className: previewCls }),
|
234 | value.split('\n').map(function (data, i) {
|
235 | return React.createElement(
|
236 | 'p',
|
237 | { key: 'p-' + i },
|
238 | data
|
239 | );
|
240 | })
|
241 | );
|
242 | }
|
243 |
|
244 | var compositionProps = {};
|
245 | if (composition) {
|
246 | compositionProps.onCompositionStart = this.handleCompositionStart;
|
247 | compositionProps.onCompositionEnd = this.handleCompositionEnd;
|
248 | }
|
249 |
|
250 | return React.createElement(
|
251 | 'span',
|
252 | _extends({ className: cls, style: wrapStyle, dir: rtl ? 'rtl' : undefined }, dataProps),
|
253 | React.createElement('textarea', _extends({}, others, props, compositionProps, {
|
254 | 'data-real': true,
|
255 | rows: rows,
|
256 | style: textareStyle,
|
257 | ref: this.saveRef.bind(this),
|
258 | onKeyDown: this.onKeyDown.bind(this)
|
259 | })),
|
260 | autoHeight ? React.createElement('textarea', {
|
261 | 'data-fake': true,
|
262 | ref: this.saveHelpRef.bind(this),
|
263 | style: _extends({}, props.style, hiddenStyle),
|
264 | rows: '1'
|
265 | }) : null,
|
266 | this.renderControl()
|
267 | );
|
268 | };
|
269 |
|
270 | return TextArea;
|
271 | }(Base), _class.getDerivedStateFromProps = Base.getDerivedStateFromProps, _class.propTypes = _extends({}, Base.propTypes, {
|
272 | |
273 |
|
274 |
|
275 | hasBorder: PropTypes.bool,
|
276 | |
277 |
|
278 |
|
279 |
|
280 | state: PropTypes.oneOf(['error', 'warning']),
|
281 | |
282 |
|
283 |
|
284 | autoHeight: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
|
285 | |
286 |
|
287 |
|
288 | rows: PropTypes.number,
|
289 | |
290 |
|
291 |
|
292 | isPreview: PropTypes.bool,
|
293 | |
294 |
|
295 |
|
296 |
|
297 | renderPreview: PropTypes.func
|
298 | }), _class.defaultProps = _extends({}, Base.defaultProps, {
|
299 | hasBorder: true,
|
300 | isPreview: false,
|
301 | rows: 4,
|
302 | autoHeight: false
|
303 | }), _initialiseProps = function _initialiseProps() {
|
304 | var _this2 = this;
|
305 |
|
306 | this._resizeTextArea = function (value) {
|
307 | if (_this2.nextFrameActionId) {
|
308 | clearNextFrameAction(_this2.nextFrameActionId);
|
309 | }
|
310 | _this2.nextFrameActionId = onNextFrame(function () {
|
311 | var height = _this2._getHeight(value);
|
312 | var maxHeight = _this2.state.maxHeight ? _this2.state.maxHeight : Infinity;
|
313 |
|
314 | _this2.setState({
|
315 | height: _this2._getHeight(value),
|
316 | overflowY: height <= maxHeight ? 'hidden' : undefined
|
317 | });
|
318 | });
|
319 | };
|
320 | }, _temp);
|
321 | export { TextArea as default }; |
\ | No newline at end of file |