UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

329 lines (276 loc) 9.39 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { function 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; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import React, { Component } from 'react'; import omit from 'lodash/omit'; import classNames from 'classnames'; import calculateNodeHeight from './calculateNodeHeight'; import { getPrefixCls as _getPrefixCls } from '../configure'; function onNextFrame(cb) { if (window.requestAnimationFrame) { return window.requestAnimationFrame(cb); } return window.setTimeout(cb, 1); } function clearNextFrameAction(nextFrameId) { if (window.cancelAnimationFrame) { window.cancelAnimationFrame(nextFrameId); } else { window.clearTimeout(nextFrameId); } } var TextArea = /*#__PURE__*/ function (_Component) { _inherits(TextArea, _Component); var _super = _createSuper(TextArea); function TextArea() { var _this; _classCallCheck(this, TextArea); _this = _super.apply(this, arguments); _this.state = { textareaStyles: {}, inputLength: 0, focused: false }; _this.resizeTextarea = function () { var autosize = _this.props.autosize; if (!autosize || !_this.textAreaRef) { return; } var minRows = autosize ? autosize.minRows : null; var maxRows = autosize ? autosize.maxRows : null; var textareaStyles = calculateNodeHeight(_this.textAreaRef, false, minRows, maxRows); _this.setState({ textareaStyles: textareaStyles }); }; _this.handleTextareaChange = function (e) { if (!('value' in _this.props)) { _this.resizeTextarea(); } var onChange = _this.props.onChange; if (onChange) { onChange(e); } }; _this.handleKeyDown = function (e) { var _this$props = _this.props, onPressEnter = _this$props.onPressEnter, onKeyDown = _this$props.onKeyDown; if (e.keyCode === 13 && onPressEnter) { onPressEnter(e); } if (onKeyDown) { onKeyDown(e); } }; _this.handleInput = function () { _this.setState({ inputLength: _this.textAreaRef.value.length }); }; _this.saveTextAreaRef = function (textArea) { _this.textAreaRef = textArea; }; _this.handleFocus = function (e) { var onFocus = _this.props.onFocus; _this.setState({ focused: true }); if (onFocus) { onFocus(e); } }; _this.handleBlur = function (e) { var onBlur = _this.props.onBlur; _this.setState({ focused: false }); if (onBlur) { onBlur(e); } }; return _this; } _createClass(TextArea, [{ key: "componentDidMount", value: function componentDidMount() { this.resizeTextarea(); if (this.textAreaRef.value) { this.setState({ inputLength: this.textAreaRef.value.length }); } var autoFocus = this.props.autoFocus; if (autoFocus) { this.setState({ focused: true }); } } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { // Re-render with the new content then recalculate the height as required. if (this.textAreaRef.value !== nextProps.value) { var inputLength = nextProps.value && nextProps.value.length; this.setState({ inputLength: inputLength || 0 }); } if (nextProps.autoFocus) { this.setState({ focused: true }); } var value = this.props.value; if (value !== nextProps.value) { if (this.nextFrameActionId) { clearNextFrameAction(this.nextFrameActionId); } this.nextFrameActionId = onNextFrame(this.resizeTextarea); } } }, { key: "focus", value: function focus() { this.textAreaRef.focus(); } }, { key: "blur", value: function blur() { this.textAreaRef.blur(); } }, { key: "getPrefixCls", value: function getPrefixCls() { var prefixCls = this.props.prefixCls; return _getPrefixCls('input', prefixCls); } }, { key: "getTextAreaClassName", value: function getTextAreaClassName() { var className = this.props.className; var prefixCls = this.getPrefixCls(); return classNames(prefixCls, "".concat(prefixCls, "-textarea-element"), className); } }, { key: "getWrapperClassName", value: function getWrapperClassName() { var _classNames; var _this$props2 = this.props, disabled = _this$props2.disabled, label = _this$props2.label, border = _this$props2.border; var _this$state = this.state, inputLength = _this$state.inputLength, focused = _this$state.focused; var prefixCls = this.getPrefixCls(); return classNames("".concat(prefixCls, "-wrapper"), "".concat(prefixCls, "-textarea"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-has-value"), inputLength !== 0), _defineProperty(_classNames, "".concat(prefixCls, "-focused"), focused), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-has-label"), !!label), _defineProperty(_classNames, "".concat(prefixCls, "-has-border"), border), _classNames)); } }, { key: "getLengthInfo", value: function getLengthInfo() { var _this$props3 = this.props, maxLength = _this$props3.maxLength, showLengthInfo = _this$props3.showLengthInfo; var prefixCls = this.getPrefixCls(); var inputLength = this.state.inputLength; return maxLength && showLengthInfo || maxLength && maxLength > 0 && inputLength === maxLength ? React.createElement("div", { className: "".concat(prefixCls, "-length-info") }, "".concat(inputLength, "/").concat(maxLength)) : null; } }, { key: "getLabel", value: function getLabel() { var _this$props4 = this.props, placeholder = _this$props4.placeholder, label = _this$props4.label; var _this$state2 = this.state, inputLength = _this$state2.inputLength, focused = _this$state2.focused; if (inputLength === 0 && !focused && placeholder) { return placeholder; } return label; } }, { key: "renderFloatLabel", value: function renderFloatLabel() { var label = this.getLabel(); if (label) { var prefixCls = this.getPrefixCls(); return React.createElement("div", { className: "".concat(prefixCls, "-label-wrapper") }, React.createElement("div", { className: "".concat(prefixCls, "-label") }, label)); } } }, { key: "render", value: function render() { var props = this.props; var textareaStyles = this.state.textareaStyles; var prefixCls = this.getPrefixCls(); var otherProps = omit(props, ['prefixCls', 'onPressEnter', 'autosize', 'placeholder', 'focused', 'showLengthInfo']); var style = _objectSpread({}, props.style, {}, textareaStyles); // Make sure it could be reset when using form.getFieldDecorator if ('value' in otherProps) { otherProps.value = otherProps.value || ''; } otherProps.onInput = this.handleInput; return React.createElement("span", { className: this.getWrapperClassName() }, React.createElement("div", { className: "".concat(prefixCls, "-rendered-wrapper") }, React.createElement("textarea", _extends({}, otherProps, { className: this.getTextAreaClassName(), style: style, onKeyDown: this.handleKeyDown, onChange: this.handleTextareaChange, ref: this.saveTextAreaRef, onInput: this.handleInput, onBlur: this.handleBlur, onFocus: this.handleFocus })), this.renderFloatLabel()), this.getLengthInfo()); } }]); return TextArea; }(Component); export { TextArea as default }; TextArea.displayName = 'TextArea'; TextArea.defaultProps = { showLengthInfo: true, border: true }; //# sourceMappingURL=TextArea.js.map