choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
235 lines (197 loc) • 7.32 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 _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 { __decorate } from "tslib";
import React from 'react';
import { observer } from 'mobx-react';
import { action, observable } from 'mobx';
import isString from 'lodash/isString';
import PropTypes from 'prop-types';
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 text = this.getTextNode();
var resizable = resize !== ResizeType.none;
var wrapperProps = this.getWrapperProps() || {};
var elementProps = this.getOtherProps() || {};
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({}, wrapperStyle, {
width: 'auto'
});
}
if (lengthElement) {
var _wrapperStyle = wrapperProps.style;
wrapperProps.style = _objectSpread({}, _wrapperStyle, {
marginBottom: '0.2rem'
});
}
var element = React.createElement("textarea", _extends({}, elementProps, {
placeholder: this.hasFloatLabel ? undefined : this.getPlaceholders()[0],
readOnly: !this.editable,
value: isString(text) ? text : this.processValue(this.getValue())
}));
var children = React.createElement(React.Fragment, null, element, lengthElement, suffix, button);
return React.createElement("div", _extends({
key: "wrapper"
}, wrapperProps), this.renderPlaceHolder(), React.createElement("label", null, resizable ? React.createElement(ReactResizeObserver, {
onResize: this.handleResize,
resizeProp: getResizeProp(resize)
}, children) : children, this.renderFloatLabel()));
}
}, {
key: "handleEnterDown",
value: function handleEnterDown(_) {// noop
}
}]);
return TextArea;
}(TextField);
TextArea.displayName = 'TextArea';
TextArea.propTypes = _objectSpread({
cols: PropTypes.number,
rows: PropTypes.number,
resize: PropTypes.oneOf([ResizeType.vertical, ResizeType.horizontal, ResizeType.none, ResizeType.both]),
autoSize: PropTypes.oneOfType([PropTypes.bool, PropTypes.object])
}, TextField.propTypes);
TextArea.defaultProps = _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