"use strict";

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

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["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 _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

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 _classnames = _interopRequireDefault(require("classnames"));

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

var _icon = _interopRequireDefault(require("../icon"));

var _CheckableTag = _interopRequireDefault(require("./CheckableTag"));

var _animate = _interopRequireDefault(require("../animate"));

var _colors = require("../_util/colors");

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

var _excluded = ["prefixCls", "closable", "color", "className", "children", "style"];

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

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

  function Tag() {
    var _this;

    (0, _classCallCheck2["default"])(this, Tag);
    _this = _super.apply(this, arguments);
    _this.state = {
      closing: false,
      closed: false,
      visible: true
    };

    _this.handleIconClick = function (e) {
      var onClose = _this.props.onClose;

      if (onClose) {
        onClose(e);
      }

      if (e.defaultPrevented || 'visible' in _this.props) {
        return;
      }

      _this.setState({
        visible: false
      });
    };

    _this.close = function () {
      var _this$state = _this.state,
          closing = _this$state.closing,
          closed = _this$state.closed;

      if (closing || closed) {
        return;
      }

      var dom = (0, _reactDom.findDOMNode)((0, _assertThisInitialized2["default"])(_this));
      dom.style.width = "".concat(dom.getBoundingClientRect().width, "px"); // It's Magic Code, don't know why

      dom.style.width = "".concat(dom.getBoundingClientRect().width, "px");

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

    _this.show = function () {
      _this.setState({
        closed: false
      });
    };

    _this.animationEnd = function (_, existed) {
      var closed = _this.state.closed;

      if (!existed && !closed) {
        _this.setState({
          closed: true,
          closing: false
        });

        var afterClose = _this.props.afterClose;

        if (afterClose) {
          afterClose();
        }
      } else {
        _this.setState({
          closed: false
        });
      }
    };

    return _this;
  }

  (0, _createClass2["default"])(Tag, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(_prevProps, prevState) {
      var visible = this.state.visible;

      if (prevState.visible && !visible) {
        this.close();
      } else if (!prevState.visible && visible) {
        this.show();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props = this.props,
          customizePrefixCls = _this$props.prefixCls,
          closable = _this$props.closable,
          color = _this$props.color,
          className = _this$props.className,
          children = _this$props.children,
          style = _this$props.style,
          otherProps = (0, _objectWithoutProperties2["default"])(_this$props, _excluded);
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('tag', customizePrefixCls);
      var _this$state2 = this.state,
          closing = _this$state2.closing,
          closed = _this$state2.closed;
      var closeIcon = closable ? /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        type: "close",
        onClick: this.handleIconClick
      }) : '';
      var isPresetColor = (0, _colors.isPresetColor)(color);
      var classString = (0, _classnames["default"])(prefixCls, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-").concat(color), isPresetColor), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-has-color"), color && !isPresetColor), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-close"), closing), _classNames), className); // fix https://fb.me/react-unknown-prop

      var divProps = (0, _omit["default"])(otherProps, ['onClose', 'afterClose', 'visible']);
      var tagStyle = (0, _objectSpread2["default"])({}, style);

      if (color && !isPresetColor) {
        tagStyle.backgroundColor = color;
      }

      var tag = closed ? null : /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
        hidden: closing
      }, divProps, {
        className: classString,
        style: tagStyle
      }), children, closeIcon);
      return /*#__PURE__*/_react["default"].createElement(_animate["default"], {
        component: "",
        hiddenProp: "hidden",
        transitionName: "".concat(prefixCls, "-zoom"),
        transitionAppear: true,
        onEnd: this.animationEnd
      }, tag);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }, {
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps) {
      return 'visible' in nextProps ? {
        visible: nextProps.visible
      } : null;
    }
  }]);
  return Tag;
}(_react.Component);

exports["default"] = Tag;
Tag.displayName = 'Tag';
Tag.CheckableTag = _CheckableTag["default"];
Tag.defaultProps = {
  closable: false
};
//# sourceMappingURL=index.js.map
