rc-menu-rtl
Version:
rtl menu ui component for react
235 lines (208 loc) • 7.01 kB
JavaScript
import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import KeyCode from 'rc-util/es/KeyCode';
import classNames from 'classnames';
import scrollIntoView from 'dom-scroll-into-view';
import { connect } from 'mini-store';
import { noop, menuAllProps } from './util';
/* eslint react/no-is-mounted:0 */
export var MenuItem = function (_React$Component) {
_inherits(MenuItem, _React$Component);
function MenuItem(props) {
_classCallCheck(this, MenuItem);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
_this.onKeyDown = function (e) {
var keyCode = e.keyCode;
if (keyCode === KeyCode.ENTER) {
_this.onClick(e);
return true;
}
};
_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
});
};
_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
});
};
_this.onClick = function (e) {
var _this$props3 = _this.props,
eventKey = _this$props3.eventKey,
multiple = _this$props3.multiple,
onClick = _this$props3.onClick,
onSelect = _this$props3.onSelect,
onDeselect = _this$props3.onDeselect,
isSelected = _this$props3.isSelected;
var info = {
key: eventKey,
keyPath: [eventKey],
item: _this,
domEvent: e
};
onClick(info);
if (multiple) {
if (isSelected) {
onDeselect(info);
} else {
onSelect(info);
}
} else if (!isSelected) {
onSelect(info);
}
};
return _this;
}
MenuItem.prototype.componentDidMount = function componentDidMount() {
// invoke customized ref to expose component to mixin
this.callRef();
};
MenuItem.prototype.componentDidUpdate = function componentDidUpdate() {
if (this.props.active) {
scrollIntoView(ReactDOM.findDOMNode(this), ReactDOM.findDOMNode(this.props.parentMenu), {
onlyScrollIfNeeded: true
});
}
this.callRef();
};
MenuItem.prototype.componentWillUnmount = function componentWillUnmount() {
var props = this.props;
if (props.onDestroy) {
props.onDestroy(props.eventKey);
}
};
MenuItem.prototype.getPrefixCls = function getPrefixCls() {
return this.props.rootPrefixCls + '-item';
};
MenuItem.prototype.getActiveClassName = function getActiveClassName() {
return this.getPrefixCls() + '-active';
};
MenuItem.prototype.getSelectedClassName = function getSelectedClassName() {
return this.getPrefixCls() + '-selected';
};
MenuItem.prototype.getDisabledClassName = function getDisabledClassName() {
return this.getPrefixCls() + '-disabled';
};
MenuItem.prototype.callRef = function callRef() {
if (this.props.manualRef) {
this.props.manualRef(this);
}
};
MenuItem.prototype.render = function render() {
var _classNames;
var props = _extends({}, this.props);
var className = classNames(this.getPrefixCls(), props.className, (_classNames = {}, _classNames[this.getActiveClassName()] = !props.disabled && props.active, _classNames[this.getSelectedClassName()] = props.isSelected, _classNames[this.getDisabledClassName()] = props.disabled, _classNames));
var attrs = _extends({}, props.attribute, {
title: props.title,
className: className,
// set to menuitem by default
role: props.role || 'menuitem',
'aria-disabled': props.disabled
});
if (props.role === 'option') {
// overwrite to option
attrs = _extends({}, attrs, {
role: 'option',
'aria-selected': props.isSelected
});
} else if (props.role === null || props.role === 'none') {
// sometimes we want to specify role inside <li/> element
// <li><a role='menuitem'>Link</a></li> would be a good example
// in this case the role on <li/> should be "none" to
// remove the implied listitem role.
// https://www.w3.org/TR/wai-aria-practices-1.1/examples/menubar/menubar-1/menubar-1.html
attrs.role = 'none';
}
// In case that onClick/onMouseLeave/onMouseEnter is passed down from owner
var mouseEvent = {
onClick: props.disabled ? null : this.onClick,
onMouseLeave: props.disabled ? null : this.onMouseLeave,
onMouseEnter: props.disabled ? null : this.onMouseEnter
};
var style = _extends({}, props.style);
if (props.mode === 'inline') {
style.paddingRight = props.inlineIndent * props.level;
}
menuAllProps.forEach(function (key) {
return delete props[key];
});
var icon = this.props.itemIcon;
if (typeof this.props.itemIcon === 'function') {
icon = React.createElement(this.props.itemIcon, this.props);
}
return React.createElement(
'li',
_extends({}, props, attrs, mouseEvent, {
style: style
}),
props.children,
icon
);
};
return MenuItem;
}(React.Component);
MenuItem.propTypes = {
attribute: PropTypes.object,
rootPrefixCls: PropTypes.string,
eventKey: PropTypes.string,
active: PropTypes.bool,
children: PropTypes.any,
selectedKeys: PropTypes.array,
disabled: PropTypes.bool,
title: PropTypes.string,
onItemHover: PropTypes.func,
onSelect: PropTypes.func,
onClick: PropTypes.func,
onDeselect: PropTypes.func,
parentMenu: PropTypes.object,
onDestroy: PropTypes.func,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
multiple: PropTypes.bool,
isSelected: PropTypes.bool,
manualRef: PropTypes.func,
itemIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node])
};
MenuItem.defaultProps = {
onSelect: noop,
onMouseEnter: noop,
onMouseLeave: noop,
manualRef: noop
};
MenuItem.isMenuItem = true;
var connected = 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);
export default connected;