"use strict";

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

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

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

var _overlay = _interopRequireDefault(require("../overlay"));

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

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,
    makeChain = _util.func.makeChain;
var Popup = _overlay["default"].Popup;
/**
 * Dropdown
 * @description 继承 Popup 的 API，除非特别说明
 */

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

  function Dropdown(_props) {
    var _this;

    _classCallCheck(this, Dropdown);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Dropdown).call(this, _props));

    _defineProperty(_assertThisInitialized(_this), "open", function () {
      _this.onVisibleChange(true, 'fromComponentMethod');
    });

    _defineProperty(_assertThisInitialized(_this), "close", function () {
      _this.onVisibleChange(false, 'fromComponentMethod');
    });

    _defineProperty(_assertThisInitialized(_this), "toggle", function () {
      if (_this.getVisible()) {
        _this.close();
      } else {
        _this.open();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getVisible", function () {
      var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props;
      return 'visible' in props ? props.visible : _this.state.visible;
    });

    _defineProperty(_assertThisInitialized(_this), "getOverlay", function () {
      try {
        return _this.popup.getInstance().getOverlay();
      } catch (err) {
        return null;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onMenuClick", function () {
      _this.onVisibleChange(false, 'fromContent');
    });

    _defineProperty(_assertThisInitialized(_this), "onVisibleChange", function (visible, from) {
      _this.setState({
        visible: visible
      });

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

    _defineProperty(_assertThisInitialized(_this), "onTriggerKeyDown", function () {
      var autoFocus = true;

      if ('autoFocus' in _this.props) {
        autoFocus = _this.props.autoFocus;
      }

      _this.setState({
        autoFocus: autoFocus
      });
    });

    _this.state = {
      visible: 'visible' in _props ? _props.visible : _props.defaultVisible || false,
      autoFocus: 'autoFocus' in _props ? _props.autoFocus : false
    };
    _this.popup = null;
    return _this;
  }

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

      var child = _react.Children.only(this.props.children);

      if (typeof child.type === 'function' && child.type.isNextMenu) {
        child = _react["default"].cloneElement(child, {
          onItemClick: makeChain(this.onMenuClick, child.props.onItemClick)
        });
      }

      var _this$props = this.props,
          trigger = _this$props.trigger,
          rtl = _this$props.rtl;

      var newTrigger = _react["default"].cloneElement(trigger, {
        onKeyDown: makeChain(this.onTriggerKeyDown, trigger.props.onKeyDown)
      });

      return _react["default"].createElement(Popup, _extends({
        animation: {
          "in": 'fadeInDown',
          out: 'fadeOutUp'
        }
      }, this.props, {
        ref: function ref(popup) {
          _this2.popup = popup;
        },
        rtl: rtl,
        autoFocus: this.state.autoFocus,
        trigger: newTrigger,
        visible: this.getVisible(),
        onVisibleChange: this.onVisibleChange
      }), child);
    }
  }]);

  return Dropdown;
}(_react.Component);

exports["default"] = Dropdown;

_defineProperty(Dropdown, "propTypes", {
  prefix: _propTypes["default"].string,
  pure: _propTypes["default"].bool,
  rtl: _propTypes["default"].bool,
  className: _propTypes["default"].string,

  /**
   * 弹层内容
   */
  children: _propTypes["default"].node,

  /**
   * 弹层当前是否显示
   */
  visible: _propTypes["default"].bool,

  /**
   * 弹层默认是否显示
   */
  defaultVisible: _propTypes["default"].bool,

  /**
   * 弹层显示或隐藏时触发的回调函数
   * @param {Boolean} visible 弹层是否显示
   * @param {String} type 触发弹层显示或隐藏的来源 fromContent 表示由Dropdown内容触发； fromTrigger 表示由trigger的点击触发； docClick 表示由document的点击触发
   */
  onVisibleChange: _propTypes["default"].func,

  /**
   * 触发弹层显示或者隐藏的元素
   */
  trigger: _propTypes["default"].node,

  /**
   * 触发弹层显示或隐藏的操作类型，可以是 'click'，'hover'，或者它们组成的数组，如 ['hover', 'click']
   */
  triggerType: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].array]),

  /**
   * 设置此属性，弹层无法显示或隐藏
   */
  disabled: _propTypes["default"].bool,

  /**
   * 弹层相对于触发元素的定位, 详见 Overlay 的定位部分
   */
  align: _propTypes["default"].string,

  /**
   * 弹层相对于trigger的定位的微调, 接收数组[hoz, ver], 表示弹层在 left / top 上的增量
   * e.g. [100, 100] 表示往右(RTL 模式下是往左) 、下分布偏移100px
   */
  offset: _propTypes["default"].array,

  /**
   * 弹层显示或隐藏的延时时间（以毫秒为单位），在 triggerType 被设置为 hover 时生效
   */
  delay: _propTypes["default"].number,

  /**
   * 弹层打开时是否让其中的元素自动获取焦点
   */
  autoFocus: _propTypes["default"].bool,

  /**
   * 是否显示遮罩
   */
  hasMask: _propTypes["default"].bool,

  /**
   * 隐藏时是否保留子节点
   */
  cache: _propTypes["default"].bool,

  /**
   * 配置动画的播放方式，支持 { in: 'enter-class', out: 'leave-class' } 的对象参数，如果设置为 false，则不播放动画
   * @default { in: 'expandInDown', out: 'expandOutUp' }
   */
  animation: _propTypes["default"].oneOfType([_propTypes["default"].object, _propTypes["default"].bool])
});

_defineProperty(Dropdown, "defaultProps", {
  prefix: 'next-',
  pure: false,
  defaultVisible: false,
  onVisibleChange: noop,
  triggerType: 'hover',
  disabled: false,
  align: 'tl bl',
  offset: [0, 4],
  delay: 200,
  hasMask: false,
  cache: false
});