"use strict";

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

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _reactDom = require("react-dom");

var _domScrollIntoView = _interopRequireDefault(require("dom-scroll-into-view"));

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

var _miniStore = require("mini-store");

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

var _noop = _interopRequireDefault(require("lodash/noop"));

var _singleton = require("../../../pro/lib/tooltip/singleton");

var _util = _interopRequireDefault(require("../../../pro/lib/overflow-tip/util"));

var _KeyCode = _interopRequireDefault(require("../../_util/KeyCode"));

var _Checkbox = _interopRequireDefault(require("../../checkbox/Checkbox"));

var _ripple = _interopRequireDefault(require("../../ripple"));

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

/* eslint react/no-is-mounted:0 */
var MenuItem = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(MenuItem, _React$Component);

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

  function MenuItem() {
    var _this;

    (0, _classCallCheck2["default"])(this, MenuItem);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onKeyDown", function (e) {
      var keyCode = e.keyCode;

      if (keyCode === _KeyCode["default"].ENTER) {
        _this.onClick(e);

        return true;
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onMouseLeave", function (e) {
      var _this$props = _this.props,
          eventKey = _this$props.eventKey,
          onItemHover = _this$props.onItemHover,
          onMouseLeave = _this$props.onMouseLeave;
      onItemHover({
        key: eventKey,
        hover: false
      });
      onMouseLeave({
        key: eventKey,
        domEvent: e
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onMouseEnter", function (e) {
      var _this$props2 = _this.props,
          eventKey = _this$props2.eventKey,
          onItemHover = _this$props2.onItemHover,
          onMouseEnter = _this$props2.onMouseEnter;
      onItemHover({
        key: eventKey,
        hover: true
      });
      onMouseEnter({
        key: eventKey,
        domEvent: e
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleRippleMouseEnter", function (e) {
      var _this$props3 = _this.props,
          tooltip = _this$props3.tooltip,
          tooltipTheme = _this$props3.tooltipTheme,
          tooltipPlacement = _this$props3.tooltipPlacement,
          children = _this$props3.children,
          mode = _this$props3.mode;
      var currentTarget = e.currentTarget;

      if (children && (tooltip === 'always' || tooltip === 'overflow' && (0, _util["default"])(currentTarget))) {
        (0, _singleton.show)(currentTarget, {
          title: children,
          placement: tooltipPlacement || mode === 'horizontal' ? 'top' : 'right',
          theme: tooltipTheme
        });
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onClick", function (e) {
      var _this$props4 = _this.props,
          eventKey = _this$props4.eventKey,
          multiple = _this$props4.multiple,
          onClick = _this$props4.onClick,
          onSelect = _this$props4.onSelect,
          onDeselect = _this$props4.onDeselect,
          isSelected = _this$props4.isSelected;
      var info = {
        key: eventKey,
        keyPath: [eventKey],
        item: (0, _assertThisInitialized2["default"])(_this),
        domEvent: e
      };
      onClick(info);

      if (multiple) {
        if (isSelected) {
          onDeselect(info);
        } else {
          onSelect(info);
        }
      } else if (!isSelected) {
        onSelect(info);
      }
    });
    return _this;
  }

  (0, _createClass2["default"])(MenuItem, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var props = this.props;

      if (props.onDestroy) {
        props.onDestroy(props.eventKey);
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      // invoke customized ref to expose component to mixin
      this.callRef();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (this.props.active) {
        (0, _domScrollIntoView["default"])((0, _reactDom.findDOMNode)(this), (0, _reactDom.findDOMNode)(this.props.parentMenu), {
          onlyScrollIfNeeded: true
        });
      }

      this.callRef();
    }
  }, {
    key: "getPrefixCls",
    value: function getPrefixCls() {
      return "".concat(this.props.rootPrefixCls, "-item");
    }
  }, {
    key: "getActiveClassName",
    value: function getActiveClassName() {
      return "".concat(this.getPrefixCls(), "-active");
    }
  }, {
    key: "getSelectedClassName",
    value: function getSelectedClassName() {
      return "".concat(this.getPrefixCls(), "-selected");
    }
  }, {
    key: "getDisabledClassName",
    value: function getDisabledClassName() {
      return "".concat(this.getPrefixCls(), "-disabled");
    }
  }, {
    key: "callRef",
    value: function callRef() {
      if (this.props.manualRef) {
        this.props.manualRef(this);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var props = (0, _objectSpread2["default"])({}, this.props);
      var className = (0, _classnames["default"])(this.getPrefixCls(), props.className, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, this.getActiveClassName(), !props.disabled && props.active), (0, _defineProperty2["default"])(_classNames, this.getSelectedClassName(), props.isSelected), (0, _defineProperty2["default"])(_classNames, this.getDisabledClassName(), props.disabled), _classNames));
      var attrs = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, props.attribute), {}, {
        title: props.title,
        className: className,
        role: props.role || 'menuitem',
        'aria-disabled': props.disabled
      });

      if (props.role === 'option') {
        // overwrite to option
        attrs = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, attrs), {}, {
          role: 'option',
          'aria-selected': props.isSelected
        });
      } else if (props.role === null || props.role === 'none') {
        attrs.role = 'none';
      }

      var mouseEvent = {
        onClick: props.disabled ? null : this.onClick,
        onMouseLeave: props.disabled ? null : this.onMouseLeave,
        onMouseEnter: props.disabled ? null : this.onMouseEnter,
        onMouseDown: props.disabled ? null : props.onMouseDown
      };
      var style = (0, _objectSpread2["default"])({}, props.style);

      if (props.mode === 'inline') {
        style.paddingLeft = props.inlineIndent * props.level;
      }

      var checkbox = props.multiple && props.checkable !== false ? /*#__PURE__*/_react["default"].createElement(_Checkbox["default"], {
        disabled: props.disabled,
        checked: props.isSelected,
        tabIndex: -1
      }) : null;
      var rippleProps = {
        disabled: props.disabled || props.rippleDisabled
      };

      if (['overflow', 'always'].includes(props.tooltip)) {
        rippleProps.onMouseEnter = this.handleRippleMouseEnter;
        rippleProps.onMouseLeave = _singleton.hide;
      }

      _util2.menuAllProps.forEach(function (key) {
        return delete props[key];
      });

      return /*#__PURE__*/_react["default"].createElement(_ripple["default"], rippleProps, /*#__PURE__*/_react["default"].createElement("li", (0, _extends2["default"])({}, (0, _omit["default"])(props, ['tooltipPlacement', 'tooltipTheme']), attrs, mouseEvent, {
        style: style
      }), checkbox, props.children));
    }
  }]);
  return MenuItem;
}(_react["default"].Component);

exports.MenuItem = MenuItem;
(0, _defineProperty2["default"])(MenuItem, "displayName", 'MenuItem');
(0, _defineProperty2["default"])(MenuItem, "defaultProps", {
  onSelect: _noop["default"],
  onMouseEnter: _noop["default"],
  onMouseLeave: _noop["default"],
  onMouseDown: _noop["default"],
  manualRef: _noop["default"]
});
MenuItem.isMenuItem = true;
var connected = (0, _miniStore.connect)(function (_ref, _ref2) {
  var activeKey = _ref.activeKey,
      selectedKeys = _ref.selectedKeys;
  var eventKey = _ref2.eventKey,
      subMenuKey = _ref2.subMenuKey;
  return {
    active: activeKey[subMenuKey] === eventKey,
    isSelected: selectedKeys.indexOf(eventKey) !== -1
  };
})(MenuItem);
var _default = connected;
exports["default"] = _default;
//# sourceMappingURL=MenuItem.js.map
