"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _i18nFrontTranslate = require("../../utils/i18n-front-translate");

var _style = require("./style");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _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); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var TextareaView = /*#__PURE__*/function (_PureComponent) {
  _inherits(TextareaView, _PureComponent);

  var _super = _createSuper(TextareaView);

  function TextareaView(props) {
    var _this;

    _classCallCheck(this, TextareaView);

    _this = _super.call(this, props);

    _this.isHasPlaceholder = function () {
      var _this$props = _this.props,
          placeholder = _this$props.placeholder,
          value = _this$props.value;

      _this.setState({
        isHasPlaceholder: placeholder.length > 0 && value.length <= 0
      });
    };

    _this._handlerPlaceholderClick = function () {
      // @ts-ignore
      _this.refs.textarea.focus();

      _this.setState({
        isFocus: true
      });
    };

    _this._handlerFocus = function (event) {
      var _this$props2 = _this.props,
          autoResizing = _this$props2.autoResizing,
          initialHeight = _this$props2.initialHeight,
          maxHeight = _this$props2.maxHeight;
      var textarea = _this.refs.textarea; // @ts-ignore

      textarea.style.height = autoResizing && maxHeight && maxHeight > initialHeight ? "".concat(maxHeight, "px") : "".concat(initialHeight, "px");

      _this.setState({
        isFocus: true
      });

      _this.toEnd(event);
    };

    _this._handlerBlur = function (event) {
      _this.setState({
        isFocus: false
      });
    };

    _this._handlerChange = function (event) {
      _this.props.onChange(event.target.value);
    };

    _this.toEnd = function (event) {
      var obj = event.srcElement ? event.srcElement : event.target;

      if (obj.createTextRange) {
        // IE浏览器
        var range = obj.createTextRange();
        range.moveStart("character", event.target.value.length);
        range.collapse(true);
      } else {
        // 非IE浏览器
        obj.setSelectionRange(event.target.value.length, event.target.value.length);
        obj.focus();
      }
    };

    _this.state = {
      isFocus: false,
      isHasPlaceholder: false
    };
    return _this;
  }

  _createClass(TextareaView, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props3 = this.props,
          initialHeight = _this$props3.initialHeight,
          maxHeight = _this$props3.maxHeight;
      var textarea = this.refs.textarea; // @ts-ignore

      textarea.style.height = textarea.value && textarea.scrollHeight > initialHeight ? "".concat(maxHeight || textarea.scrollHeight, "px") : "".concat(initialHeight, "px");
      this.isHasPlaceholder();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      this.isHasPlaceholder();
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
          className = _this$props4.className,
          style = _this$props4.style,
          value = _this$props4.value,
          initialHeight = _this$props4.initialHeight,
          maxHeight = _this$props4.maxHeight,
          placeholder = _this$props4.placeholder,
          maxLength = _this$props4.maxLength;
      var _this$state = this.state,
          isFocus = _this$state.isFocus,
          isHasPlaceholder = _this$state.isHasPlaceholder;
      var valueLen = value.length;
      return /*#__PURE__*/_react["default"].createElement(_style.TextareaWrapper, {
        className: className,
        style: style
      }, isHasPlaceholder && /*#__PURE__*/_react["default"].createElement(_style.TextareaPlaceholder, {
        dangerouslySetInnerHTML: {
          __html: placeholder
        },
        onClick: this._handlerPlaceholderClick,
        style: {
          opacity: isFocus ? "0.3" : "1"
        }
      }), /*#__PURE__*/_react["default"].createElement(_style.Textarea, {
        ref: "textarea",
        className: "".concat(isFocus ? "actived" : ""),
        height: initialHeight,
        maxHeight: maxHeight,
        value: value,
        style: {
          borderColor: valueLen > maxLength ? "#CC2929" : "#E4EBF0"
        },
        onFocus: this._handlerFocus,
        onBlur: this._handlerBlur,
        onChange: this._handlerChange
      }), valueLen > maxLength && /*#__PURE__*/_react["default"].createElement(_style.TextareaWarning, null, /*#__PURE__*/_react["default"].createElement("p", null, (0, _i18nFrontTranslate.inputWordTrans)(valueLen, maxLength))));
    }
  }]);

  return TextareaView;
}(_react.PureComponent);

exports["default"] = TextareaView;
TextareaView.defaultProps = {
  initialHeight: 23,
  maxHeight: 56,
  autoResizing: false,
  maxLength: 2000
};