UNPKG

13.8 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7
8var _react = _interopRequireDefault(require("react"));
9
10var _reactDom = _interopRequireDefault(require("react-dom"));
11
12var _propTypes = _interopRequireDefault(require("prop-types"));
13
14var _classnames = _interopRequireDefault(require("classnames"));
15
16var _util = require("../util");
17
18var _base = _interopRequireDefault(require("./base"));
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
21
22function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
23
24function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
25
26function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
27
28function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
29
30function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
31
32function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
33
34function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
35
36function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
37
38function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
39
40function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
41
42function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
43
44function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
45
46function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
47
48function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
49
50function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
51
52function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
53
54function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
55
56// safari in mac
57var isMacSafari = typeof navigator !== 'undefined' && navigator && navigator.userAgent ? navigator.userAgent.match(/^((?!chrome|android|windows).)*safari/i) : false;
58var hiddenStyle = {
59 visibility: 'hidden',
60 position: 'absolute',
61 zIndex: '-1000',
62 top: '-1000px',
63 overflowY: 'hidden',
64 left: 0,
65 right: 0
66};
67/**
68 * Input.TextArea
69 * @order 2
70 */
71
72var TextArea = /*#__PURE__*/function (_Base) {
73 _inherits(TextArea, _Base);
74
75 var _super = _createSuper(TextArea);
76
77 function TextArea(props) {
78 var _this;
79
80 _classCallCheck(this, TextArea);
81
82 _this = _super.call(this, props);
83
84 _defineProperty(_assertThisInitialized(_this), "_resizeTextArea", function (value) {
85 if (_this.nextFrameActionId) {
86 (0, _util.clearNextFrameAction)(_this.nextFrameActionId);
87 }
88
89 _this.nextFrameActionId = (0, _util.onNextFrame)(function () {
90 var height = _this._getHeight(value);
91
92 var maxHeight = _this.state.maxHeight ? _this.state.maxHeight : Infinity;
93
94 _this.setState({
95 height: _this._getHeight(value),
96 overflowY: height <= maxHeight ? 'hidden' : undefined
97 });
98 });
99 });
100
101 var _value;
102
103 if ('value' in props) {
104 _value = props.value;
105 } else {
106 _value = props.defaultValue;
107 }
108
109 _this.state = {
110 value: typeof _value === 'undefined' ? '' : _value
111 };
112 return _this;
113 }
114
115 _createClass(TextArea, [{
116 key: "componentDidMount",
117 value: function componentDidMount() {
118 var autoHeight = this.props.autoHeight;
119
120 if (autoHeight) {
121 if (_typeof(autoHeight) === 'object') {
122 /* eslint-disable react/no-did-mount-set-state */
123 this.setState(this._getMinMaxHeight(autoHeight, this.state.value));
124 } else {
125 this.setState({
126 height: this._getHeight(this.state.value),
127 overflowY: 'hidden'
128 });
129 }
130 }
131 }
132 }, {
133 key: "UNSAFE_componentWillReceiveProps",
134 value: function UNSAFE_componentWillReceiveProps(nextProps) {
135 if (_get(_getPrototypeOf(TextArea.prototype), "UNSAFE_componentWillReceiveProps", this)) {
136 _get(_getPrototypeOf(TextArea.prototype), "UNSAFE_componentWillReceiveProps", this).call(this, nextProps);
137 }
138
139 if (this.props.autoHeight && this.props.value !== nextProps.value) {
140 this._resizeTextArea(nextProps.value);
141 }
142 }
143 }, {
144 key: "_getMinMaxHeight",
145 value: function _getMinMaxHeight(_ref, value) {
146 var minRows = _ref.minRows,
147 maxRows = _ref.maxRows;
148
149 /* eslint-disable-next-line */
150 var node = _reactDom["default"].findDOMNode(this.helpRef);
151
152 node.setAttribute('rows', minRows);
153 var minHeight = node.clientHeight;
154 node.setAttribute('rows', maxRows);
155 var maxHeight = node.clientHeight;
156 node.setAttribute('rows', '1');
157
158 var height = this._getHeight(value);
159
160 return {
161 minHeight: minHeight,
162 maxHeight: maxHeight,
163 height: height,
164 overflowY: height <= maxHeight ? 'hidden' : undefined
165 };
166 }
167 }, {
168 key: "_getHeight",
169 value: function _getHeight(value) {
170 /* eslint-disable-next-line */
171 var node = _reactDom["default"].findDOMNode(this.helpRef);
172
173 node.value = value;
174 return node.scrollHeight;
175 }
176 }, {
177 key: "ieHack",
178 value: function ieHack(value) {
179 // Fix: textarea dit not support maxLength in ie9
180
181 /* istanbul ignore if */
182 if (_util.env.ieVersion === 9 && this.props.maxLength) {
183 var maxLength = parseInt(this.props.maxLength);
184 var len = this.getValueLength(value, true);
185
186 if (len > maxLength && this.props.cutString) {
187 value = value.replace(/\n/g, '\n\n');
188 value = value.substr(0, maxLength);
189 value = value.replace(/\n\n/g, '\n');
190 }
191 }
192
193 this.props.autoHeight && this._resizeTextArea(value);
194 return value;
195 }
196 /**
197 * value.length !== maxLength in ie/safari(mac) while value has `Enter`
198 * about maxLength compute: `Enter` was considered to be one char(\n) in chrome , but two chars(\r\n) in ie/safari(mac).
199 * so while value has `Enter`, we should let display length + 1
200 */
201
202 }, {
203 key: "getValueLength",
204 value: function getValueLength(value) {
205 var _this$props = this.props,
206 maxLength = _this$props.maxLength,
207 cutString = _this$props.cutString;
208 var nv = "".concat(value);
209 var strLen = this.props.getValueLength(nv);
210
211 if (typeof strLen !== 'number') {
212 strLen = nv.length;
213 }
214 /* istanbul ignore if */
215
216
217 if (_util.env.ieVersion || isMacSafari) {
218 strLen = strLen + nv.split('\n').length - 1;
219
220 if (strLen > maxLength && cutString) {
221 strLen = maxLength;
222 }
223 }
224
225 return strLen;
226 }
227 }, {
228 key: "saveTextAreaRef",
229 value: function saveTextAreaRef(textArea) {
230 this.inputRef = textArea;
231 }
232 }, {
233 key: "saveHelpRef",
234 value: function saveHelpRef(ref) {
235 this.helpRef = ref;
236 }
237 }, {
238 key: "render",
239 value: function render() {
240 var _classNames;
241
242 var _this$props2 = this.props,
243 rows = _this$props2.rows,
244 style = _this$props2.style,
245 className = _this$props2.className,
246 autoHeight = _this$props2.autoHeight,
247 prefix = _this$props2.prefix,
248 rtl = _this$props2.rtl,
249 hasBorder = _this$props2.hasBorder;
250 var cls = (0, _classnames["default"])(this.getClass(), (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "input-textarea"), true), _defineProperty(_classNames, "".concat(prefix, "noborder"), !hasBorder), _defineProperty(_classNames, className, !!className), _classNames));
251 var props = this.getProps(); // custom data attributes are assigned to the top parent node
252 // data-类自定义数据属性分配到顶层node节点
253
254 var dataProps = _util.obj.pickAttrsWith(this.props, 'data-'); // Custom props are transparently transmitted to the core input node by default
255 // 自定义属性默认透传到核心node节点:input
256
257
258 var others = _util.obj.pickOthers(_extends({}, dataProps, TextArea.propTypes), this.props);
259
260 var textareStyle = _objectSpread(_objectSpread({}, props.style), {}, {
261 height: this.state.height,
262 minHeight: this.state.minHeight,
263 maxHeight: this.state.maxHeight,
264 overflowY: this.state.overflowY
265 });
266
267 var wrapStyle = autoHeight ? _objectSpread(_objectSpread({}, style), {}, {
268 position: 'relative'
269 }) : style;
270 return /*#__PURE__*/_react["default"].createElement("span", _extends({
271 className: cls,
272 style: wrapStyle,
273 dir: rtl ? 'rtl' : undefined
274 }, dataProps), /*#__PURE__*/_react["default"].createElement("textarea", _extends({}, others, props, {
275 "data-real": true,
276 rows: rows,
277 style: textareStyle,
278 ref: this.saveRef.bind(this),
279 onKeyDown: this.onKeyDown.bind(this)
280 })), autoHeight ? /*#__PURE__*/_react["default"].createElement("textarea", {
281 "data-fake": true,
282 ref: this.saveHelpRef.bind(this),
283 style: _objectSpread(_objectSpread({}, props.style), hiddenStyle),
284 rows: "1"
285 }) : null, this.renderControl());
286 }
287 }]);
288
289 return TextArea;
290}(_base["default"]);
291
292exports["default"] = TextArea;
293
294_defineProperty(TextArea, "propTypes", _objectSpread(_objectSpread({}, _base["default"].propTypes), {}, {
295 /**
296 * 是否有边框
297 */
298 hasBorder: _propTypes["default"].bool,
299
300 /**
301 * 状态
302 * @enumdesc 错误
303 */
304 state: _propTypes["default"].oneOf(['error']),
305
306 /**
307 * 自动高度 true / {minRows: 2, maxRows: 4}
308 */
309 autoHeight: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].object]),
310
311 /**
312 * 多行文本框高度 <br />(不要直接用height设置多行文本框的高度, ie9 10会有兼容性问题)
313 */
314 rows: _propTypes["default"].number
315}));
316
317_defineProperty(TextArea, "defaultProps", _objectSpread(_objectSpread({}, _base["default"].defaultProps), {}, {
318 hasBorder: true,
319 rows: 4,
320 autoHeight: false
321}));
\No newline at end of file