1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports["default"] = void 0;
|
11 |
|
12 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
13 |
|
14 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
15 |
|
16 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
17 |
|
18 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
19 |
|
20 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
21 |
|
22 | var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
23 |
|
24 | var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
25 |
|
26 | var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
27 |
|
28 | var _react = _interopRequireWildcard(require("react"));
|
29 |
|
30 | var _omit = _interopRequireDefault(require("lodash/omit"));
|
31 |
|
32 | var _classnames = _interopRequireDefault(require("classnames"));
|
33 |
|
34 | var _calculateNodeHeight = _interopRequireDefault(require("./calculateNodeHeight"));
|
35 |
|
36 | var _configure = require("../configure");
|
37 |
|
38 | function _createSuper(Derived) {
|
39 | function isNativeReflectConstruct() {
|
40 | if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
41 | if (Reflect.construct.sham) return false;
|
42 | if (typeof Proxy === "function") return true;
|
43 |
|
44 | try {
|
45 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
|
46 | return true;
|
47 | } catch (e) {
|
48 | return false;
|
49 | }
|
50 | }
|
51 |
|
52 | return function () {
|
53 | var Super = (0, _getPrototypeOf2["default"])(Derived),
|
54 | result;
|
55 |
|
56 | if (isNativeReflectConstruct()) {
|
57 | var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor;
|
58 | result = Reflect.construct(Super, arguments, NewTarget);
|
59 | } else {
|
60 | result = Super.apply(this, arguments);
|
61 | }
|
62 |
|
63 | return (0, _possibleConstructorReturn2["default"])(this, result);
|
64 | };
|
65 | }
|
66 |
|
67 | function onNextFrame(cb) {
|
68 | if (window.requestAnimationFrame) {
|
69 | return window.requestAnimationFrame(cb);
|
70 | }
|
71 |
|
72 | return window.setTimeout(cb, 1);
|
73 | }
|
74 |
|
75 | function clearNextFrameAction(nextFrameId) {
|
76 | if (window.cancelAnimationFrame) {
|
77 | window.cancelAnimationFrame(nextFrameId);
|
78 | } else {
|
79 | window.clearTimeout(nextFrameId);
|
80 | }
|
81 | }
|
82 |
|
83 | var TextArea =
|
84 |
|
85 | function (_Component) {
|
86 | (0, _inherits2["default"])(TextArea, _Component);
|
87 |
|
88 | var _super = _createSuper(TextArea);
|
89 |
|
90 | function TextArea() {
|
91 | var _this;
|
92 |
|
93 | (0, _classCallCheck2["default"])(this, TextArea);
|
94 | _this = _super.apply(this, arguments);
|
95 | _this.state = {
|
96 | textareaStyles: {},
|
97 | inputLength: 0,
|
98 | focused: false
|
99 | };
|
100 |
|
101 | _this.resizeTextarea = function () {
|
102 | var autosize = _this.props.autosize;
|
103 |
|
104 | if (!autosize || !_this.textAreaRef) {
|
105 | return;
|
106 | }
|
107 |
|
108 | var minRows = autosize ? autosize.minRows : null;
|
109 | var maxRows = autosize ? autosize.maxRows : null;
|
110 | var textareaStyles = (0, _calculateNodeHeight["default"])(_this.textAreaRef, false, minRows, maxRows);
|
111 |
|
112 | _this.setState({
|
113 | textareaStyles: textareaStyles
|
114 | });
|
115 | };
|
116 |
|
117 | _this.handleTextareaChange = function (e) {
|
118 | if (!('value' in _this.props)) {
|
119 | _this.resizeTextarea();
|
120 | }
|
121 |
|
122 | var onChange = _this.props.onChange;
|
123 |
|
124 | if (onChange) {
|
125 | onChange(e);
|
126 | }
|
127 | };
|
128 |
|
129 | _this.handleKeyDown = function (e) {
|
130 | var _this$props = _this.props,
|
131 | onPressEnter = _this$props.onPressEnter,
|
132 | onKeyDown = _this$props.onKeyDown;
|
133 |
|
134 | if (e.keyCode === 13 && onPressEnter) {
|
135 | onPressEnter(e);
|
136 | }
|
137 |
|
138 | if (onKeyDown) {
|
139 | onKeyDown(e);
|
140 | }
|
141 | };
|
142 |
|
143 | _this.handleInput = function () {
|
144 | _this.setState({
|
145 | inputLength: _this.textAreaRef.value.length
|
146 | });
|
147 | };
|
148 |
|
149 | _this.saveTextAreaRef = function (textArea) {
|
150 | _this.textAreaRef = textArea;
|
151 | };
|
152 |
|
153 | _this.handleFocus = function (e) {
|
154 | var onFocus = _this.props.onFocus;
|
155 |
|
156 | _this.setState({
|
157 | focused: true
|
158 | });
|
159 |
|
160 | if (onFocus) {
|
161 | onFocus(e);
|
162 | }
|
163 | };
|
164 |
|
165 | _this.handleBlur = function (e) {
|
166 | var onBlur = _this.props.onBlur;
|
167 |
|
168 | _this.setState({
|
169 | focused: false
|
170 | });
|
171 |
|
172 | if (onBlur) {
|
173 | onBlur(e);
|
174 | }
|
175 | };
|
176 |
|
177 | return _this;
|
178 | }
|
179 |
|
180 | (0, _createClass2["default"])(TextArea, [{
|
181 | key: "componentDidMount",
|
182 | value: function componentDidMount() {
|
183 | this.resizeTextarea();
|
184 |
|
185 | if (this.textAreaRef.value) {
|
186 | this.setState({
|
187 | inputLength: this.textAreaRef.value.length
|
188 | });
|
189 | }
|
190 |
|
191 | var autoFocus = this.props.autoFocus;
|
192 |
|
193 | if (autoFocus) {
|
194 | this.setState({
|
195 | focused: true
|
196 | });
|
197 | }
|
198 | }
|
199 | }, {
|
200 | key: "componentWillReceiveProps",
|
201 | value: function componentWillReceiveProps(nextProps) {
|
202 |
|
203 | if (this.textAreaRef.value !== nextProps.value) {
|
204 | var inputLength = nextProps.value && nextProps.value.length;
|
205 | this.setState({
|
206 | inputLength: inputLength || 0
|
207 | });
|
208 | }
|
209 |
|
210 | if (nextProps.autoFocus) {
|
211 | this.setState({
|
212 | focused: true
|
213 | });
|
214 | }
|
215 |
|
216 | var value = this.props.value;
|
217 |
|
218 | if (value !== nextProps.value) {
|
219 | if (this.nextFrameActionId) {
|
220 | clearNextFrameAction(this.nextFrameActionId);
|
221 | }
|
222 |
|
223 | this.nextFrameActionId = onNextFrame(this.resizeTextarea);
|
224 | }
|
225 | }
|
226 | }, {
|
227 | key: "focus",
|
228 | value: function focus() {
|
229 | this.textAreaRef.focus();
|
230 | }
|
231 | }, {
|
232 | key: "blur",
|
233 | value: function blur() {
|
234 | this.textAreaRef.blur();
|
235 | }
|
236 | }, {
|
237 | key: "getPrefixCls",
|
238 | value: function getPrefixCls() {
|
239 | var prefixCls = this.props.prefixCls;
|
240 | return (0, _configure.getPrefixCls)('input', prefixCls);
|
241 | }
|
242 | }, {
|
243 | key: "getTextAreaClassName",
|
244 | value: function getTextAreaClassName() {
|
245 | var className = this.props.className;
|
246 | var prefixCls = this.getPrefixCls();
|
247 | return (0, _classnames["default"])(prefixCls, "".concat(prefixCls, "-textarea-element"), className);
|
248 | }
|
249 | }, {
|
250 | key: "getWrapperClassName",
|
251 | value: function getWrapperClassName() {
|
252 | var _classNames;
|
253 |
|
254 | var _this$props2 = this.props,
|
255 | disabled = _this$props2.disabled,
|
256 | label = _this$props2.label,
|
257 | border = _this$props2.border;
|
258 | var _this$state = this.state,
|
259 | inputLength = _this$state.inputLength,
|
260 | focused = _this$state.focused;
|
261 | var prefixCls = this.getPrefixCls();
|
262 | return (0, _classnames["default"])("".concat(prefixCls, "-wrapper"), "".concat(prefixCls, "-textarea"), (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-has-value"), inputLength !== 0), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-focused"), focused), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-has-label"), !!label), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-has-border"), border), _classNames));
|
263 | }
|
264 | }, {
|
265 | key: "getLengthInfo",
|
266 | value: function getLengthInfo() {
|
267 | var _this$props3 = this.props,
|
268 | maxLength = _this$props3.maxLength,
|
269 | showLengthInfo = _this$props3.showLengthInfo;
|
270 | var prefixCls = this.getPrefixCls();
|
271 | var inputLength = this.state.inputLength;
|
272 | return maxLength && showLengthInfo || maxLength && maxLength > 0 && inputLength === maxLength ? _react["default"].createElement("div", {
|
273 | className: "".concat(prefixCls, "-length-info")
|
274 | }, "".concat(inputLength, "/").concat(maxLength)) : null;
|
275 | }
|
276 | }, {
|
277 | key: "getLabel",
|
278 | value: function getLabel() {
|
279 | var _this$props4 = this.props,
|
280 | placeholder = _this$props4.placeholder,
|
281 | label = _this$props4.label;
|
282 | var _this$state2 = this.state,
|
283 | inputLength = _this$state2.inputLength,
|
284 | focused = _this$state2.focused;
|
285 |
|
286 | if (inputLength === 0 && !focused && placeholder) {
|
287 | return placeholder;
|
288 | }
|
289 |
|
290 | return label;
|
291 | }
|
292 | }, {
|
293 | key: "renderFloatLabel",
|
294 | value: function renderFloatLabel() {
|
295 | var label = this.getLabel();
|
296 |
|
297 | if (label) {
|
298 | var prefixCls = this.getPrefixCls();
|
299 | return _react["default"].createElement("div", {
|
300 | className: "".concat(prefixCls, "-label-wrapper")
|
301 | }, _react["default"].createElement("div", {
|
302 | className: "".concat(prefixCls, "-label")
|
303 | }, label));
|
304 | }
|
305 | }
|
306 | }, {
|
307 | key: "render",
|
308 | value: function render() {
|
309 | var props = this.props;
|
310 | var textareaStyles = this.state.textareaStyles;
|
311 | var prefixCls = this.getPrefixCls();
|
312 | var otherProps = (0, _omit["default"])(props, ['prefixCls', 'onPressEnter', 'autosize', 'placeholder', 'focused', 'showLengthInfo']);
|
313 | var style = (0, _objectSpread2["default"])({}, props.style, {}, textareaStyles);
|
314 |
|
315 | if ('value' in otherProps) {
|
316 | otherProps.value = otherProps.value || '';
|
317 | }
|
318 |
|
319 | otherProps.onInput = this.handleInput;
|
320 | return _react["default"].createElement("span", {
|
321 | className: this.getWrapperClassName()
|
322 | }, _react["default"].createElement("div", {
|
323 | className: "".concat(prefixCls, "-rendered-wrapper")
|
324 | }, _react["default"].createElement("textarea", (0, _extends2["default"])({}, otherProps, {
|
325 | className: this.getTextAreaClassName(),
|
326 | style: style,
|
327 | onKeyDown: this.handleKeyDown,
|
328 | onChange: this.handleTextareaChange,
|
329 | ref: this.saveTextAreaRef,
|
330 | onInput: this.handleInput,
|
331 | onBlur: this.handleBlur,
|
332 | onFocus: this.handleFocus
|
333 | })), this.renderFloatLabel()), this.getLengthInfo());
|
334 | }
|
335 | }]);
|
336 | return TextArea;
|
337 | }(_react.Component);
|
338 |
|
339 | exports["default"] = TextArea;
|
340 | TextArea.displayName = 'TextArea';
|
341 | TextArea.defaultProps = {
|
342 | showLengthInfo: true,
|
343 | border: true
|
344 | };
|
345 |
|