import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { Component } from 'react';
import { findDOMNode } from 'react-dom';
import omit from 'lodash/omit';
import classNames from 'classnames';
import ResizeObserver from 'resize-observer-polyfill';
import calculateNodeHeight from './calculateNodeHeight';
import { InnerRowCtx } from '../rc-components/table/TableRowContext';
import ConfigContext from '../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) {
  _inherits(TextArea, _Component);

  var _super = _createSuper(TextArea);

  function TextArea() {
    var _this;

    _classCallCheck(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 = calculateNodeHeight(_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;
  }

  _createClass(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 classNames(prefixCls, className, "".concat(prefixCls, "-textarea-element"), _defineProperty({}, "".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 classNames("".concat(prefixCls, "-wrapper"), "".concat(prefixCls, "-textarea"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-has-value"), inputLength !== 0), _defineProperty(_classNames2, "".concat(prefixCls, "-focused"), focused), _defineProperty(_classNames2, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames2, "".concat(prefixCls, "-has-label"), !!label), _defineProperty(_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.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.createElement("div", {
          className: "".concat(prefixCls, "-label-wrapper")
        }, /*#__PURE__*/React.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 = omit(props, omits);

      var style = _objectSpread(_objectSpread({}, 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.createElement(InnerRowCtx.Consumer, null, function (options) {
        if (options && !_this2.resizeObserver && _this2.textAreaRef) {
          _this2.resizeObserver = new ResizeObserver(options.syncRowHeight);
          var dom = findDOMNode(_this2.textAreaRef); // eslint-disable-next-line no-unused-expressions

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

        var textarea = /*#__PURE__*/React.createElement("textarea", _extends({}, 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.createElement("div", {
          className: "".concat(prefixCls, "-rendered-wrapper")
        }, textarea, floatLabel) : textarea;

        var lengthInfo = _this2.getLengthInfo(prefixCls);

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

  return TextArea;
}(Component);

export { TextArea as default };
TextArea.displayName = 'TextArea';
TextArea.defaultProps = {
  showLengthInfo: true,
  border: true,
  labelLayout: 'float'
};
//# sourceMappingURL=TextArea.js.map
