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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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; }

import React from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Icon from '../icon';
import Button from '../button';
import Overlay from '../overlay';
import Menu from '../menu';
import ConfigProvider from '../config-provider';
import { dom, obj, func } from '../util';
var Popup = Overlay.Popup;
/**
 * SplitButton
 */

var SplitButton =
/*#__PURE__*/
function (_React$Component) {
  _inherits(SplitButton, _React$Component);

  function SplitButton(props, context) {
    var _this;

    _classCallCheck(this, SplitButton);

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

    _defineProperty(_assertThisInitialized(_this), "selectMenuItem", function (keys) {
      var _this$props;

      if (!('selectedKeys' in _this.props)) {
        _this.setState({
          selectedKeys: keys
        });
      }

      for (var _len = arguments.length, others = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        others[_key - 1] = arguments[_key];
      }

      (_this$props = _this.props).onSelect.apply(_this$props, [keys].concat(others));
    });

    _defineProperty(_assertThisInitialized(_this), "clickMenuItem", function (key) {
      var _this$props2;

      for (var _len2 = arguments.length, others = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
        others[_key2 - 1] = arguments[_key2];
      }

      (_this$props2 = _this.props).onItemClick.apply(_this$props2, [key].concat(others));

      _this.onVisibleChange(false, 'menuSelect');
    });

    _defineProperty(_assertThisInitialized(_this), "onPopupOpen", function () {
      if (_this.props.autoWidth && _this.wrapper && _this.menu) {
        dom.setStyle(_this.menu, {
          width: _this.wrapper.offsetWidth
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onVisibleChange", function (visible, reason) {
      if (!('visible' in _this.props)) {
        _this.setState({
          visible: visible
        });
      }

      _this.props.onVisibleChange(visible, reason);
    });

    _defineProperty(_assertThisInitialized(_this), "_menuRefHandler", function (ref) {
      _this.menu = findDOMNode(ref);
      var refFn = _this.props.menuProps.ref;

      if (typeof refFn === 'function') {
        refFn(ref);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "_wrapperRefHandler", function (ref) {
      _this.wrapper = findDOMNode(ref);
    });

    _this.state = {
      selectedKeys: props.selectedKeys || props.defaultSelectedKeys,
      visible: props.visible || props.defaultVisible
    };
    return _this;
  }

  _createClass(SplitButton, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      // 由于定位目标是 wrapper，如果弹层默认展开，wrapper 还未渲染，didMount 后强制再渲染一次，弹层重新定位
      if (this.state.visible) {
        this.forceUpdate();
      }
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('visible' in nextProps) {
        this.setState({
          visible: nextProps.visible
        });
      }

      if ('selectedKeys' in nextProps) {
        this.setState({
          selectedKeys: nextProps.selectedKeys
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classnames2,
          _this2 = this;

      var _this$props3 = this.props,
          prefix = _this$props3.prefix,
          label = _this$props3.label,
          size = _this$props3.size,
          type = _this$props3.type,
          component = _this$props3.component,
          ghost = _this$props3.ghost,
          className = _this$props3.className,
          style = _this$props3.style,
          children = _this$props3.children,
          triggerProps = _this$props3.triggerProps,
          popupAlign = _this$props3.popupAlign,
          popupTriggerType = _this$props3.popupTriggerType,
          popupStyle = _this$props3.popupStyle,
          popupClassName = _this$props3.popupClassName,
          popupProps = _this$props3.popupProps,
          followTrigger = _this$props3.followTrigger,
          selectMode = _this$props3.selectMode,
          menuProps = _this$props3.menuProps,
          leftButtonProps = _this$props3.leftButtonProps,
          disabled = _this$props3.disabled,
          warning = _this$props3.warning,
          loading = _this$props3.loading,
          others = _objectWithoutProperties(_this$props3, ["prefix", "label", "size", "type", "component", "ghost", "className", "style", "children", "triggerProps", "popupAlign", "popupTriggerType", "popupStyle", "popupClassName", "popupProps", "followTrigger", "selectMode", "menuProps", "leftButtonProps", "disabled", "warning", "loading"]);

      var state = this.state;
      var classNames = classnames(_defineProperty({}, "".concat(prefix, "split-btn"), true), className);
      var sharedBtnProps = {
        type: type,
        size: size,
        component: component,
        ghost: ghost,
        disabled: disabled,
        warning: warning
      };
      var triggerClassNames = classnames((_classnames2 = {}, _defineProperty(_classnames2, "".concat(prefix, "split-btn-trigger"), true), _defineProperty(_classnames2, "".concat(prefix, "expand"), state.visible), _defineProperty(_classnames2, "opened", state.visible), _classnames2));
      var trigger = React.createElement(Button, _extends({}, triggerProps, sharedBtnProps, {
        className: triggerClassNames
      }), React.createElement(Icon, {
        type: "chevron-down-s"
      }));
      return React.createElement(Button.Group, _extends({}, obj.pickOthers(SplitButton.propTypes, others), {
        className: classNames,
        style: style,
        size: size,
        ref: this._wrapperRefHandler
      }), React.createElement(Button, _extends({}, sharedBtnProps, {
        loading: loading
      }, leftButtonProps), label), React.createElement(Popup, _extends({
        animation: {
          "in": 'fadeInDown',
          out: 'fadeOutUp'
        }
      }, popupProps, {
        followTrigger: followTrigger,
        visible: state.visible,
        onVisibleChange: this.onVisibleChange,
        trigger: trigger,
        triggerType: popupTriggerType,
        offset: [0, 4],
        align: popupAlign,
        target: function target() {
          return _this2.wrapper;
        },
        style: popupStyle,
        shouldUpdatePosition: true,
        className: popupClassName,
        onOpen: this.onPopupOpen
      }), React.createElement(Menu, _extends({}, menuProps, {
        className: "".concat(prefix, "btn-menu"),
        selectMode: selectMode,
        selectedKeys: state.selectedKeys,
        onSelect: this.selectMenuItem,
        onItemClick: this.clickMenuItem,
        ref: this._menuRefHandler
      }), children)));
    }
  }]);

  return SplitButton;
}(React.Component);

_defineProperty(SplitButton, "propTypes", {
  prefix: PropTypes.string,
  style: PropTypes.object,

  /**
   * 按钮的类型
   */
  type: PropTypes.oneOf(['normal', 'primary', 'secondary']),

  /**
   * 按钮组的尺寸
   */
  size: PropTypes.oneOf(['small', 'medium']),

  /**
   * 主按钮的文案
   */
  label: PropTypes.node,

  /**
   * 设置标签类型
   */
  component: PropTypes.oneOf(['button', 'a']),

  /**
   * 是否为幽灵按钮
   */
  ghost: PropTypes.oneOf(['light', 'dark', false, true]),

  /**
   * 是否为警告按钮
   */
  warning: PropTypes.bool,

  /**
   * 是否禁用
   */
  disabled: PropTypes.bool,

  /**
   * 设置按钮的载入状态
   */
  loading: PropTypes.bool,

  /**
   * 默认激活的菜单项（用法同 Menu 非受控）
   */
  defaultSelectedKeys: PropTypes.array,

  /**
   * 激活的菜单项（用法同 Menu 受控）
   */
  selectedKeys: PropTypes.array,

  /**
   * 菜单的选择模式
   */
  selectMode: PropTypes.oneOf(['single', 'multiple']),

  /**
   * 选择菜单项时的回调，参考 Menu
   */
  onSelect: PropTypes.func,

  /**
   * 点击菜单项时的回调，参考 Menu
   */
  onItemClick: PropTypes.func,

  /**
   * 触发按钮的属性（支持 Button 的所有属性透传）
   */
  triggerProps: PropTypes.object,

  /**
   * 弹层菜单的宽度是否与按钮组一致
   */
  autoWidth: PropTypes.bool,

  /**
   * 弹层是否显示
   */
  visible: PropTypes.bool,

  /**
   * 弹层默认是否显示
   */
  defaultVisible: PropTypes.bool,

  /**
   * 弹层显示状态变化时的回调函数
   * @param {Boolean} visible 弹层显示状态
   * @param {String} type 触发弹层显示或隐藏的来源 menuSelect 表示由menu触发； fromTrigger 表示由trigger的点击触发； docClick 表示由document的点击触发
   */
  onVisibleChange: PropTypes.func,

  /**
   * 弹层的触发方式
   */
  popupTriggerType: PropTypes.oneOf(['click', 'hover']),

  /**
   * 弹层对齐方式, 详情见Overlay align
   */
  popupAlign: PropTypes.string,

  /**
   * 弹层自定义样式
   */
  popupStyle: PropTypes.object,

  /**
   * 弹层自定义样式类
   */
  popupClassName: PropTypes.string,

  /**
   * 透传给弹层的属性
   */
  popupProps: PropTypes.object,

  /**
   * 是否跟随滚动
   */
  followTrigger: PropTypes.bool,

  /**
   * 透传给 Menu 的属性
   */
  menuProps: PropTypes.object,

  /**
   * 透传给 左侧按钮 的属性
   */
  leftButtonProps: PropTypes.object,
  className: PropTypes.string,
  children: PropTypes.any
});

_defineProperty(SplitButton, "defaultProps", {
  prefix: 'next-',
  type: 'normal',
  size: 'medium',
  warning: false,
  disabled: false,
  loading: false,
  autoWidth: true,
  popupTriggerType: 'click',
  onVisibleChange: func.noop,
  onItemClick: func.noop,
  onSelect: func.noop,
  defaultSelectedKeys: [],
  menuProps: {},
  leftButtonProps: {}
});

SplitButton.Item = Menu.Item;
SplitButton.Divider = Menu.Divider;
SplitButton.Group = Menu.Group;
export default ConfigProvider.config(SplitButton);