"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

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

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

var _util = require("../util");

var _zhCn = _interopRequireDefault(require("../locale/zh-cn"));

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

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

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

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 } }); 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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var noop = _util.func.noop,
    bindCtx = _util.func.bindCtx;
/**
 * Tag
 */

var Tag =
/*#__PURE__*/
function (_Component) {
  _inherits(Tag, _Component);

  function Tag(props) {
    var _this;

    _classCallCheck(this, Tag);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Tag).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
      // 针对无障碍化要求 添加键盘SPACE事件
      var _this$props = _this.props,
          closable = _this$props.closable,
          closeArea = _this$props.closeArea,
          onClick = _this$props.onClick,
          disabled = _this$props.disabled;

      if (e.keyCode !== _util.KEYCODE.SPACE || disabled) {
        return;
      }

      e.preventDefault();
      e.stopPropagation();

      if (closable) {
        _this.handleClose(closeArea);
      } else {
        typeof onClick === 'function' && onClick(e);
      }
    });

    _this.state = {
      visible: true
    };
    bindCtx(_assertThisInitialized(_this), ['handleBodyClick', 'handleTailClick', 'handleAnimationInit', 'handleAnimationEnd', 'renderTailNode']);
    return _this;
  }

  _createClass(Tag, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.__destroyed = true;
    }
  }, {
    key: "handleClose",
    value: function handleClose(from) {
      var _this2 = this;

      var _this$props2 = this.props,
          animation = _this$props2.animation,
          onClose = _this$props2.onClose;
      var hasAnimation = _util.support.animation && animation; // 先执行回调

      var result = onClose(from, this.tagNode); // 如果回调函数返回 false，则阻止关闭行为

      if (result !== false && !this.__destroyed) {
        this.setState({
          visible: false
        }, function () {
          // 如果没有动画，则直接执行 afterClose
          !hasAnimation && _this2.props.afterClose(_this2.tagNode);
        });
      }
    } // 标签体点击

  }, {
    key: "handleBodyClick",
    value: function handleBodyClick(e) {
      var _this$props3 = this.props,
          closable = _this$props3.closable,
          closeArea = _this$props3.closeArea,
          onClick = _this$props3.onClick;

      if (closable && closeArea === 'tag') {
        this.handleClose('tag');
      }

      if (typeof onClick === 'function') {
        return onClick(e);
      }
    }
  }, {
    key: "handleTailClick",
    value: function handleTailClick(e) {
      e && e.preventDefault();
      e && e.stopPropagation();
      this.handleClose('tail');
    }
  }, {
    key: "handleAnimationInit",
    value: function handleAnimationInit(node) {
      this.props.afterAppear(node);
    }
  }, {
    key: "handleAnimationEnd",
    value: function handleAnimationEnd(node) {
      this.props.afterClose(node);
    }
  }, {
    key: "renderAnimatedTag",
    value: function renderAnimatedTag(children, animationName) {
      return _react["default"].createElement(_animate["default"], {
        animation: animationName,
        afterAppear: this.handleAnimationInit,
        afterLeave: this.handleAnimationEnd
      }, children);
    }
  }, {
    key: "renderIcon",
    value: function renderIcon(leftCls) {
      var icon = this.props.icon;

      if (icon) {
        if (typeof icon === 'string') {
          return _react["default"].createElement(_icon["default"], {
            type: icon,
            className: leftCls
          });
        } else {
          return _react["default"].cloneElement(icon, {
            className: leftCls
          });
        }
      }
    }
  }, {
    key: "renderTailNode",
    value: function renderTailNode() {
      var _this$props4 = this.props,
          prefix = _this$props4.prefix,
          closable = _this$props4.closable,
          locale = _this$props4.locale;

      if (!closable) {
        return null;
      }

      return _react["default"].createElement("span", {
        className: "".concat(prefix, "tag-close-btn"),
        onClick: this.handleTailClick,
        role: "button",
        "aria-label": locale["delete"]
      }, _react["default"].createElement(_icon["default"], {
        type: "remove"
      }));
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames,
          _this3 = this;

      var _this$props5 = this.props,
          prefix = _this$props5.prefix,
          type = _this$props5.type,
          size = _this$props5.size,
          _shape = _this$props5._shape,
          closable = _this$props5.closable,
          closeArea = _this$props5.closeArea,
          className = _this$props5.className,
          children = _this$props5.children,
          animation = _this$props5.animation,
          disabled = _this$props5.disabled,
          color = _this$props5.color,
          rtl = _this$props5.rtl;
      var visible = this.state.visible;

      var others = _util.obj.pickOthers(Tag.propTypes, this.props);

      var shape = closable ? 'closable' : _shape;
      var isChecked = _shape === 'checkable' && this.props['aria-checked'];
      var bodyClazz = (0, _classnames["default"])(["".concat(prefix, "tag"), "".concat(prefix, "tag-").concat(color), "".concat(prefix, "tag-").concat(shape), "".concat(prefix, "tag-level-").concat(type), "".concat(prefix, "tag-").concat(size)], (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "tag-closable"), closable), _defineProperty(_classNames, "".concat(prefix, "tag-body-pointer"), closable && closeArea === 'tag'), _classNames), className); // icon class

      var leftCls = "".concat(prefix, "tag-left-icon"); // close btn

      var tailNode = this.renderTailNode(); // tag node

      var tagNode = !visible ? null : _react["default"].createElement("div", _extends({
        className: bodyClazz,
        onClick: this.handleBodyClick,
        onKeyDown: this.onKeyDown,
        tabIndex: disabled ? '' : '0',
        role: "button",
        "aria-disabled": disabled,
        disabled: disabled,
        dir: rtl ? 'rtl' : undefined,
        ref: function ref(n) {
          _this3.tagNode = n;
        }
      }, others), _react["default"].createElement("span", {
        className: "".concat(prefix, "tag-body")
      }, this.renderIcon(leftCls), children), isChecked && type === 'normal' ? _react["default"].createElement(_icon["default"], {
        className: "".concat(prefix, "tag-checkable-tick"),
        size: "xs",
        type: "tick-bold"
      }) : null, tailNode);

      if (animation && _util.support.animation) {
        return this.renderAnimatedTag(tagNode, "".concat(prefix, "tag-zoom"));
      } // 未开启或不支持动画


      return tagNode;
    }
  }]);

  return Tag;
}(_react.Component);

_defineProperty(Tag, "displayName", 'Tag');

_defineProperty(Tag, "propTypes", {
  /**
   * 标签类名前缀,提供给二次开发者用
   * @default next-
   */
  prefix: _propTypes["default"].string,

  /**
   * 标签的类型
   */
  type: _propTypes["default"].oneOf(['normal', 'primary']),

  /**
   * 标签的尺寸（large 尺寸为兼容表单场景 large = medium）
   */
  size: _propTypes["default"].oneOf(['small', 'medium', 'large']),
  closable: _propTypes["default"].bool,

  /**
   * 是否开启动效
   */
  animation: _propTypes["default"].bool,
  closeArea: _propTypes["default"].oneOf(['tag', 'tail']),
  onClose: _propTypes["default"].func,
  afterClose: _propTypes["default"].func,

  /**
   * 标签出现动画结束后执行的回调
   */
  afterAppear: _propTypes["default"].func,
  className: _propTypes["default"].any,
  children: _propTypes["default"].node,

  /**
   * 点击回调
   */
  onClick: _propTypes["default"].func,
  _shape: _propTypes["default"].oneOf(['default', 'closable', 'checkable']),
  disabled: _propTypes["default"].bool,
  rtl: _propTypes["default"].bool,
  locale: _propTypes["default"].object,

  /**
   * color 标签的颜色，可选值 'red'、'green'、'yellow'、'blue'、'gray'
   */
  color: _propTypes["default"].string,

  /**
   * icon 左侧图标
   */
  icon: _propTypes["default"].any
});

_defineProperty(Tag, "defaultProps", {
  prefix: 'next-',
  type: 'normal',
  size: 'medium',
  closeArea: 'tail',
  color: 'blue',
  animation: false,
  onClose: noop,
  afterClose: noop,
  afterAppear: noop,
  onClick: noop,
  _shape: 'default',
  disabled: false,
  rtl: false,
  locale: _zhCn["default"].Tag
});

var _default = _configProvider["default"].config(Tag);

exports["default"] = _default;