"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

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

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

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

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

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

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

var _reactDom = require("react-dom");

var _omit = _interopRequireDefault(require("lodash/omit"));

var _classnames = _interopRequireDefault(require("classnames"));

var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));

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

var _TableRowContext = require("../rc-components/table/TableRowContext");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

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) {
  (0, _inherits2["default"])(TextArea, _Component);

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

  function TextArea() {
    var _this;

    (0, _classCallCheck2["default"])(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 = (0, _calculateNodeHeight["default"])(_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 && typeof onPressEnter === 'function') {
        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;
  }

  (0, _createClass2["default"])(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 && String(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: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.resizeObserver) {
        this.resizeObserver.disconnect();
        delete this.resizeObserver;
      }
    }
  }, {
    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;
      var getPrefixCls = this.context.getPrefixCls;
      return getPrefixCls('input', prefixCls);
    }
  }, {
    key: "getTextAreaClassName",
    value: function getTextAreaClassName() {
      var _this$props2 = this.props,
          className = _this$props2.className,
          disabled = _this$props2.disabled;
      var prefixCls = this.getPrefixCls();
      return (0, _classnames["default"])(prefixCls, className, "".concat(prefixCls, "-textarea-element"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-disabled"), disabled));
    }
  }, {
    key: "getWrapperClassName",
    value: function getWrapperClassName() {
      var _classNames2;

      var _this$props3 = this.props,
          disabled = _this$props3.disabled,
          label = _this$props3.label,
          border = _this$props3.border,
          labelLayout = _this$props3.labelLayout;
      var _this$state = this.state,
          inputLength = _this$state.inputLength,
          focused = _this$state.focused;
      var prefixCls = this.getPrefixCls();
      return (0, _classnames["default"])("".concat(prefixCls, "-wrapper"), "".concat(prefixCls, "-textarea"), (_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-has-value"), inputLength !== 0), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-focused"), focused), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-has-label"), !!label), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-has-border"), border && labelLayout === 'float'), _classNames2));
    }
  }, {
    key: "getLengthInfo",
    value: function getLengthInfo(prefixCls) {
      var _this$props4 = this.props,
          maxLength = _this$props4.maxLength,
          showLengthInfo = _this$props4.showLengthInfo;
      var inputLength = this.state.inputLength;
      return showLengthInfo !== 'never' && (maxLength && showLengthInfo || maxLength && maxLength > 0 && inputLength === maxLength) ? /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-length-info")
      }, "".concat(inputLength, "/").concat(maxLength)) : null;
    }
  }, {
    key: "renderFloatLabel",
    value: function renderFloatLabel() {
      var label = this.props.label;

      if (label) {
        var prefixCls = this.getPrefixCls();
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-label-wrapper")
        }, /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-label")
        }, label));
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var props = this.props;
      var state = this.state;
      var prefixCls = this.getPrefixCls();
      var omits = ['prefixCls', 'onPressEnter', 'autosize', 'focused', 'showLengthInfo', 'labelLayout'];
      var hasBorder = props.border && props.labelLayout === 'float';
      var floatLabel = hasBorder && this.renderFloatLabel();

      if (floatLabel && !state.focused) {
        omits.push('placeholder');
      }

      var otherProps = (0, _omit["default"])(props, omits);
      var style = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, props.style), state.textareaStyles); // Make sure it could be reset when using form.getFieldDecorator

      if ('value' in otherProps) {
        otherProps.value = otherProps.value || '';
      }

      otherProps.onInput = this.handleInput;
      return /*#__PURE__*/_react["default"].createElement(_TableRowContext.InnerRowCtx.Consumer, null, function (options) {
        if (options && !_this2.resizeObserver && _this2.textAreaRef) {
          _this2.resizeObserver = new _resizeObserverPolyfill["default"](options.syncRowHeight);
          var dom = (0, _reactDom.findDOMNode)(_this2.textAreaRef); // eslint-disable-next-line no-unused-expressions

          dom && _this2.resizeObserver.observe(dom);
        }

        var textarea = /*#__PURE__*/_react["default"].createElement("textarea", (0, _extends2["default"])({}, otherProps, {
          className: _this2.getTextAreaClassName(),
          style: style,
          onKeyDown: _this2.handleKeyDown,
          onChange: _this2.handleTextareaChange,
          ref: _this2.saveTextAreaRef,
          onInput: _this2.handleInput,
          onBlur: _this2.handleBlur,
          onFocus: _this2.handleFocus
        }));

        var labeledTextArea = hasBorder ? /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-rendered-wrapper")
        }, textarea, floatLabel) : textarea;

        var lengthInfo = _this2.getLengthInfo(prefixCls);

        return lengthInfo || hasBorder ? /*#__PURE__*/_react["default"].createElement("span", {
          className: _this2.getWrapperClassName()
        }, labeledTextArea, lengthInfo) : labeledTextArea;
      });
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return TextArea;
}(_react.Component);

exports["default"] = TextArea;
TextArea.displayName = 'TextArea';
TextArea.defaultProps = {
  showLengthInfo: true,
  border: true,
  labelLayout: 'float'
};
//# sourceMappingURL=TextArea.js.map
