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 = {}; 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]; } 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 _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

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

import React from 'react';
import PropTypes from 'prop-types';
import { omit } from 'lodash';
import { EventOverlay, Icon, ListItem } from "./..";

var MenuItem =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(MenuItem, _React$Component);

  function MenuItem() {
    var _this;

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

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      anchorRef: null
    };

    _this.handleKeyDown = function (e) {
      var _this$props = _this.props,
          onClick = _this$props.onClick,
          index = _this$props.index;
      var _this$context = _this.context,
          handleKeyDown = _this$context.handleKeyDown,
          handleClick = _this$context.handleClick;

      if (e.which === 32 || e.which === 13 || e.charCode === 32 || e.charCode === 13) {
        onClick && onClick(e);
        handleClick && handleClick(index, _assertThisInitialized(_assertThisInitialized(_this)), e);
        e.preventDefault();
      } else {
        handleKeyDown && handleKeyDown(index, _assertThisInitialized(_assertThisInitialized(_this)), e);
      }
    };

    _this.handleClick = function (e) {
      var handleClick = _this.context.handleClick;
      var _this$props2 = _this.props,
          index = _this$props2.index,
          onClick = _this$props2.onClick;
      onClick && onClick(e);
      handleClick && handleClick(index, _assertThisInitialized(_assertThisInitialized(_this)), e);
    };

    return _this;
  }

  var _proto = MenuItem.prototype;

  _proto.render = function render() {
    var _this2 = this;

    var _this$props3 = this.props,
        children = _this$props3.children,
        className = _this$props3.className,
        content = _this$props3.content,
        isHeader = _this$props3.isHeader,
        isOpen = _this$props3.isOpen,
        label = _this$props3.label,
        selectedValue = _this$props3.selectedValue,
        props = _objectWithoutProperties(_this$props3, ["children", "className", "content", "isHeader", "isOpen", "label", "selectedValue"]);

    var otherProps = omit(_extends({}, props), ['onClick', 'keepMenuOpen']);
    return React.createElement("div", {
      className: 'cui-menu-item' + ("" + (className && " " + className || '')),
      "aria-expanded": isOpen,
      "aria-haspopup": !!children
    }, React.createElement(ListItem, _extends({
      active: isOpen,
      focusOnLoad: true,
      isReadOnly: isHeader,
      onClick: this.handleClick,
      onKeyDown: this.handleKeyDown,
      ref: function ref(_ref) {
        return !_this2.state.anchorRef && _this2.setState({
          anchorRef: _ref
        });
      },
      role: "menuitem"
    }, otherProps), React.createElement("div", {
      className: "cui-menu-item__content"
    }, content || label), React.createElement("div", {
      className: "cui-menu-item__selected-value",
      title: selectedValue
    }, children && selectedValue), React.createElement("div", {
      className: "cui-menu-item__arrow"
    }, children && React.createElement(Icon, {
      name: "arrow-right_16"
    }))), isOpen && React.createElement(EventOverlay, {
      anchorNode: this.state.anchorRef,
      isOpen: isOpen,
      direction: "right-top",
      closeOnClick: false
    }, React.createElement("div", {
      "aria-label": label,
      role: "menu",
      className: "cui-menu-item-container"
    }, children)));
  };

  return MenuItem;
}(React.Component);

MenuItem.displayName = 'MenuItem';
MenuItem.contextTypes = {
  handleClick: PropTypes.func,
  handleKeyDown: PropTypes.func
};
MenuItem.propTypes = {
  children: PropTypes.node,
  className: PropTypes.string,
  content: PropTypes.element,
  index: PropTypes.array,
  isHeader: PropTypes.bool,
  isOpen: PropTypes.bool,
  keepMenuOpen: PropTypes.bool,
  label: PropTypes.string,
  onClick: PropTypes.func,
  selectedValue: PropTypes.string
};
MenuItem.defaultProps = {
  children: null,
  className: '',
  content: null,
  isHeader: false,
  isOpen: false,
  keepMenuOpen: false,
  label: '',
  onClick: null,
  selectedValue: ''
};
export default MenuItem;