choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
200 lines (169 loc) • 6.43 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _get from "@babel/runtime/helpers/get";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import { __decorate } from "tslib";
import React from 'react';
import { observer } from 'mobx-react';
import { action, observable } from 'mobx';
import isString from 'lodash/isString';
import ReactResizeObserver from '../../../es/_util/resizeObserver';
import { TextField } from '../text-field/TextField';
import { ResizeType } from './enum';
import calculateNodeHeight from './calculateNodeHeight';
import autobind from '../_util/autobind';
function getResizeProp(resize) {
switch (resize) {
case ResizeType.both:
return 'both';
case ResizeType.vertical:
return 'height';
case ResizeType.horizontal:
return 'width';
default:
return undefined;
}
}
var TextArea = /*#__PURE__*/function (_TextField) {
_inherits(TextArea, _TextField);
var _super = _createSuper(TextArea);
function TextArea() {
_classCallCheck(this, TextArea);
return _super.apply(this, arguments);
}
_createClass(TextArea, [{
key: "componentDidMount",
value: function componentDidMount() {
_get(_getPrototypeOf(TextArea.prototype), "componentDidMount", this).call(this);
if (this.element && this.props.autoSize) {
// 自适应高度,挂载时渲染样式
this.forceUpdate();
}
}
}, {
key: "getOmitPropsKeys",
value: function getOmitPropsKeys() {
return _get(_getPrototypeOf(TextArea.prototype), "getOmitPropsKeys", this).call(this).concat(['resize', 'autoSize', 'onResize']);
}
}, {
key: "getOtherProps",
value: function getOtherProps() {
var _this$props = this.props,
_this$props$resize = _this$props.resize,
resize = _this$props$resize === void 0 ? ResizeType.none : _this$props$resize,
autoSize = _this$props.autoSize;
var otherProps = _get(_getPrototypeOf(TextArea.prototype), "getOtherProps", this).call(this);
var _otherProps$style = otherProps.style,
style = _otherProps$style === void 0 ? {} : _otherProps$style;
style.resize = resize;
if (resize !== ResizeType.none) {
style.transition = 'none';
}
if (autoSize) {
var minRows = autoSize.minRows,
maxRows = autoSize.maxRows;
otherProps.rows = minRows;
var element = this.element;
if (element) {
_extends(style, calculateNodeHeight(element, true, minRows, maxRows));
}
}
otherProps.style = style;
return otherProps;
}
}, {
key: "handleResize",
value: function handleResize(width, height, target) {
var onResize = this.props.onResize;
if (!this.resized) {
var element = this.element;
if (element && element.style.width) {
this.resized = true;
}
}
if (onResize) {
onResize(width, height, target);
}
}
}, {
key: "getSuffix",
value: function getSuffix() {
return null;
}
}, {
key: "renderLengthInfoWrapper",
value: function renderLengthInfoWrapper() {
if (!this.showLengthInfo) {
return null;
}
var editorTextInfo = this.getEditorTextInfo();
var inputLength = editorTextInfo.text.length;
var maxLength = this.getProp('maxLength');
var lengthElement = this.renderLengthInfo(maxLength, inputLength);
return lengthElement;
}
}, {
key: "renderWrapper",
value: function renderWrapper() {
var _this$props$resize2 = this.props.resize,
resize = _this$props$resize2 === void 0 ? ResizeType.none : _this$props$resize2;
var resizable = resize !== ResizeType.none;
var wrapperProps = this.getWrapperProps() || {};
var lengthElement = this.renderLengthInfoWrapper(); // 先计算suffix,然后再计算clearButton,设置right,避免重叠
var suffix = this.getSuffix();
var button = this.getInnerSpanButton();
if (this.resized) {
var wrapperStyle = wrapperProps.style;
wrapperProps.style = _objectSpread(_objectSpread({}, wrapperStyle), {}, {
width: 'auto'
});
}
if (lengthElement) {
var _wrapperStyle = wrapperProps.style;
wrapperProps.style = _objectSpread(_objectSpread({}, _wrapperStyle), {}, {
marginBottom: '0.2rem'
});
}
var element = this.wrapperInputNode();
var children = /*#__PURE__*/React.createElement(React.Fragment, null, element, lengthElement, suffix, button);
return /*#__PURE__*/React.createElement("div", _extends({
key: "wrapper"
}, wrapperProps), this.renderPlaceHolder(), /*#__PURE__*/React.createElement("label", null, resizable ? /*#__PURE__*/React.createElement(ReactResizeObserver, {
onResize: this.handleResize,
resizeProp: getResizeProp(resize)
}, children) : children, this.renderFloatLabel()));
}
}, {
key: "wrapperInputNode",
value: function wrapperInputNode() {
var text = this.getTextNode();
var elementProps = this.getOtherProps() || {};
return /*#__PURE__*/React.createElement("textarea", _extends({}, elementProps, {
placeholder: this.hasFloatLabel ? undefined : this.getPlaceholders()[0],
readOnly: !this.editable,
value: isString(text) ? text : this.processValue(this.getValue())
}));
}
}, {
key: "handleEnterDown",
value: function handleEnterDown(_) {// noop
}
}]);
return TextArea;
}(TextField);
TextArea.displayName = 'TextArea';
TextArea.defaultProps = _objectSpread(_objectSpread({}, TextField.defaultProps), {}, {
suffixCls: 'textarea',
rows: 4,
autoSize: false
}); // eslint-disable-next-line camelcase
TextArea.__PRO_TEXTAREA = true;
__decorate([observable], TextArea.prototype, "resized", void 0);
__decorate([autobind, action], TextArea.prototype, "handleResize", null);
TextArea = __decorate([observer], TextArea);
export default TextArea;
//# sourceMappingURL=TextArea.js.map