import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * MapTagGroup.jsx
 *
 * @Author: zhangce
 * @Date: 18/8/7 下午5:35
 */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import MapTag from './MapTag';
/**
 * MapTagGroup
 * */

var MapTagGroup =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(MapTagGroup, _React$PureComponent);

  _createClass(MapTagGroup, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      if (props.defaultValue !== state.prevDefaultValue) {
        return {
          maptags: props.defaultValue,
          prevDefaultValue: props.defaultValue
        };
      }

      return null;
    }
  }]);

  function MapTagGroup(props, context) {
    var _this;

    _classCallCheck(this, MapTagGroup);

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

    _defineProperty(_assertThisInitialized(_this), "handleClose", function (maptagRemove) {
      var onChange = _this.props.onChange;
      var maptags = _this.state.maptags;
      var newMapTags = maptags.filter(function (maptag) {
        return maptag.value !== maptagRemove;
      });

      _this.setState({
        maptags: newMapTags
      });

      onChange && onChange(newMapTags);
    });

    _defineProperty(_assertThisInitialized(_this), "triggerAddMapTagClick", function (e) {
      var autoTriggerAdd = _this.props.autoTriggerAdd;

      if (autoTriggerAdd && _this.addMapTag) {
        _this.addMapTag.setInput(e);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getContainer", function (el) {
      _this.maptagContainer = el;
    });

    var defaultValue = props.defaultValue;
    _this.state = {
      maptags: defaultValue,
      prevDefaultValue: defaultValue
    };
    var _onChange = _this.props.onChange;
    _onChange && _onChange(defaultValue);
    return _this;
  }

  _createClass(MapTagGroup, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          maptagType = _this$props.maptagType,
          addText = _this$props.addText,
          className = _this$props.className,
          children = _this$props.children,
          hasAddableMapTag = _this$props.hasAddableMapTag,
          enterKey = _this$props.enterKey,
          prefixCls = _this$props.prefixCls,
          size = _this$props.size,
          onChange = _this$props.onChange,
          autoTriggerAdd = _this$props.autoTriggerAdd,
          other = _objectWithoutProperties(_this$props, ["maptagType", "addText", "className", "children", "hasAddableMapTag", "enterKey", "prefixCls", "size", "onChange", "autoTriggerAdd"]);

      var maptags = this.state.maptags;
      return React.createElement("div", _extends({
        className: classNames("".concat(prefixCls), className, "".concat(prefixCls, "-").concat(size)),
        onClick: this.triggerAddMapTagClick
      }, other), maptags.map(function (maptag) {
        return React.createElement(MapTag, {
          onDelete: _this2.handleClose,
          uuid: maptag.value,
          key: maptag.value,
          closable: true
        }, maptag.label);
      }));
    }
  }]);

  return MapTagGroup;
}(React.PureComponent);

_defineProperty(MapTagGroup, "propTypes", {
  /**
   * 类前缀
   * */
  prefixCls: PropTypes.string,

  /**
   * 类名
   * */
  className: PropTypes.string,

  /**
   * 默认值
   * */
  defaultValue: PropTypes.array,

  /**
   * 标签样式类型
   * */
  maptagType: PropTypes.oneOf(['primary', 'secondary', 'success', 'warning', 'error', 'info']),

  /**
   * 添加新标签按钮的文案说明
   * */
  addText: PropTypes.string,

  /**
   * 值改变时回调函数
   * */
  onChange: PropTypes.func,

  /**
   * 是否显示添加按钮
   * */
  hasAddableMapTag: PropTypes.bool,

  /**
   * 创建新标签的键值
   * */
  enterKey: PropTypes.array,

  /**
   * 自动聚焦input框
   * */
  autoTriggerAdd: PropTypes.bool,
  size: PropTypes.oneOf(['small', 'medium', 'large'])
});

_defineProperty(MapTagGroup, "defaultProps", {
  prefixCls: 'ne-maptag-group',
  defaultValue: [],
  maptagType: 'error',
  addText: '+ new MapTag',
  hasAddableMapTag: true,
  autoTriggerAdd: false,
  size: 'medium'
});

export default MapTagGroup;