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