"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _tslib = require("tslib");

var _react = _interopRequireDefault(require("react"));

var _mobxReact = require("mobx-react");

var _mobx = require("mobx");

var _isString = _interopRequireDefault(require("lodash/isString"));

var _resizeObserver = _interopRequireDefault(require("../../../lib/_util/resizeObserver"));

var _TextField2 = require("../text-field/TextField");

var _enum = require("./enum");

var _calculateNodeHeight = _interopRequireDefault(require("./calculateNodeHeight"));

var _autobind = _interopRequireDefault(require("../_util/autobind"));

function getResizeProp(resize) {
  switch (resize) {
    case _enum.ResizeType.both:
      return 'both';

    case _enum.ResizeType.vertical:
      return 'height';

    case _enum.ResizeType.horizontal:
      return 'width';

    default:
      return undefined;
  }
}

var TextArea = /*#__PURE__*/function (_TextField) {
  (0, _inherits2["default"])(TextArea, _TextField);

  var _super = (0, _createSuper2["default"])(TextArea);

  function TextArea() {
    (0, _classCallCheck2["default"])(this, TextArea);
    return _super.apply(this, arguments);
  }

  (0, _createClass2["default"])(TextArea, [{
    key: "multiple",
    get: function get() {
      return false;
    }
  }, {
    key: "resize",
    get: function get() {
      var _this$props = this.props,
          autoSize = _this$props.autoSize,
          resize = _this$props.resize;

      if (autoSize) {
        var minRows = autoSize.minRows,
            maxRows = autoSize.maxRows;

        if (minRows && minRows === maxRows) {
          return undefined;
        }
      }

      return resize;
    }
  }, {
    key: "autoSize",
    get: function get() {
      var autoSize = this.props.autoSize;

      if (autoSize) {
        var minRows = autoSize.minRows,
            maxRows = autoSize.maxRows;

        if (minRows && minRows === maxRows) {
          return false;
        }
      }

      return autoSize;
    }
  }, {
    key: "rows",
    get: function get() {
      var _this$props2 = this.props,
          _this$props2$rows = _this$props2.rows,
          rows = _this$props2$rows === void 0 ? 4 : _this$props2$rows,
          autoSize = _this$props2.autoSize;

      if (autoSize) {
        var minRows = autoSize.minRows;
        return minRows || rows;
      }

      return rows;
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      (0, _get2["default"])((0, _getPrototypeOf2["default"])(TextArea.prototype), "componentDidMount", this).call(this);

      if (this.element && this.autoSize) {
        // 自适应高度，挂载时渲染样式
        this.forceUpdate();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      var _this = this;

      var element = this.element,
          autoSize = this.autoSize;

      if (autoSize && element) {
        var minRows = autoSize.minRows,
            maxRows = autoSize.maxRows;
        var calculateStyle = (0, _calculateNodeHeight["default"])(element, true, minRows, maxRows);
        var autoSizeCalcHeight = this.autoSizeCalcHeight;

        if (!autoSizeCalcHeight || calculateStyle.height !== autoSizeCalcHeight.height) {
          (0, _mobx.runInAction)(function () {
            _this.autoSizeCalcHeight = calculateStyle;
          });
        }
      }
    }
  }, {
    key: "getOmitPropsKeys",
    value: function getOmitPropsKeys() {
      return (0, _get2["default"])((0, _getPrototypeOf2["default"])(TextArea.prototype), "getOmitPropsKeys", this).call(this).concat(['resize', 'autoSize', 'onResize']);
    }
  }, {
    key: "getOtherProps",
    value: function getOtherProps() {
      var _this$resize = this.resize,
          resize = _this$resize === void 0 ? _enum.ResizeType.none : _this$resize,
          autoSize = this.autoSize,
          rows = this.rows;
      var otherProps = (0, _get2["default"])((0, _getPrototypeOf2["default"])(TextArea.prototype), "getOtherProps", this).call(this);
      var _otherProps$style = otherProps.style,
          style = _otherProps$style === void 0 ? {} : _otherProps$style;
      style.resize = resize;

      if (resize !== _enum.ResizeType.none) {
        style.transition = 'none';
      }

      if (autoSize) {
        var autoSizeCalcHeight = this.autoSizeCalcHeight;

        if (autoSizeCalcHeight) {
          (0, _extends2["default"])(style, autoSizeCalcHeight);
        }
      }

      otherProps.rows = rows;
      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: "handleClearButtonClick",
    value: function handleClearButtonClick(e) {
      e.preventDefault();
      this.clear();
    }
  }, {
    key: "handleInnerButtonMouseDown",
    value: function handleInnerButtonMouseDown(e) {
      e.preventDefault();
    }
  }, {
    key: "renderWrapper",
    value: function renderWrapper() {
      var _this$resize2 = this.resize,
          resize = _this$resize2 === void 0 ? _enum.ResizeType.none : _this$resize2;
      var resizable = resize !== _enum.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 = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, wrapperStyle), {}, {
          width: 'auto'
        });
      }

      if (lengthElement) {
        var _wrapperStyle = wrapperProps.style;
        wrapperProps.style = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _wrapperStyle), {}, {
          marginBottom: '0.2rem'
        });
      }

      var renderedValue = this.renderRenderedValue(undefined, {
        className: "".concat(this.prefixCls, "-renderer-wrapper")
      });
      var element = this.wrapperInputNode(renderedValue);

      var children = /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, element, lengthElement, suffix, button);

      return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
        key: "wrapper"
      }, wrapperProps), this.renderPlaceHolder(), renderedValue, /*#__PURE__*/_react["default"].createElement("label", null, resizable ? /*#__PURE__*/_react["default"].createElement(_resizeObserver["default"], {
        onResize: this.handleResize,
        resizeProp: getResizeProp(resize)
      }, children) : children, this.renderFloatLabel()));
    }
  }, {
    key: "wrapperInputNode",
    value: function wrapperInputNode(renderedValue) {
      var text = this.getTextNode();
      var elementProps = this.getOtherProps() || {};
      var otherProps = {};

      if (renderedValue) {
        otherProps.style = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, elementProps.style), {}, {
          textIndent: -1000,
          color: 'transparent'
        });
      }

      return /*#__PURE__*/_react["default"].createElement("textarea", (0, _extends2["default"])({}, elementProps, otherProps, {
        placeholder: this.hasFloatLabel && !this.isFocused ? undefined : this.getPlaceholders()[0],
        readOnly: !this.editable,
        value: (0, _isString["default"])(text) ? text : this.processValue(this.getValue())
      }));
    }
  }, {
    key: "handleEnterDown",
    value: function handleEnterDown(_) {// noop
    }
  }]);
  return TextArea;
}(_TextField2.TextField);

TextArea.displayName = 'TextArea';
TextArea.defaultProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _TextField2.TextField.defaultProps), {}, {
  suffixCls: 'textarea',
  autoSize: false
}); // eslint-disable-next-line camelcase

TextArea.__PRO_TEXTAREA = true;
(0, _tslib.__decorate)([_mobx.observable], TextArea.prototype, "resized", void 0);
(0, _tslib.__decorate)([_mobx.observable], TextArea.prototype, "autoSizeCalcHeight", void 0);
(0, _tslib.__decorate)([_autobind["default"], _mobx.action], TextArea.prototype, "handleResize", null);
(0, _tslib.__decorate)([_autobind["default"]], TextArea.prototype, "handleClearButtonClick", null);
TextArea = (0, _tslib.__decorate)([_mobxReact.observer], TextArea);
var _default = TextArea;
exports["default"] = _default;
//# sourceMappingURL=TextArea.js.map
