UNPKG

rc-menu-rtl

Version:
596 lines (513 loc) 17.3 kB
import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import _extends from 'babel-runtime/helpers/extends'; import React from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import Trigger from 'rc-trigger'; import KeyCode from 'rc-util/es/KeyCode'; import classNames from 'classnames'; import { connect } from 'mini-store'; import SubPopupMenu from './SubPopupMenu'; import placements from './placements'; import Animate from 'rc-animate'; import { noop, loopMenuItemRecursively, getMenuIdFromSubMenuEventKey, menuAllProps } from './util'; var guid = 0; var popupPlacementMap = { horizontal: 'bottomRight', vertical: 'leftTop', 'vertical-left': 'leftTop', 'vertical-right': 'rightTop' }; var updateDefaultActiveFirst = function updateDefaultActiveFirst(store, eventKey, defaultActiveFirst) { var _extends2; var menuId = getMenuIdFromSubMenuEventKey(eventKey); var state = store.getState(); store.setState({ defaultActiveFirst: _extends({}, state.defaultActiveFirst, (_extends2 = {}, _extends2[menuId] = defaultActiveFirst, _extends2)) }); }; export var SubMenu = function (_React$Component) { _inherits(SubMenu, _React$Component); function SubMenu(props) { _classCallCheck(this, SubMenu); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _initialiseProps.call(_this); var store = props.store; var eventKey = props.eventKey; var defaultActiveFirst = store.getState().defaultActiveFirst; _this.isRootMenu = false; var value = false; if (defaultActiveFirst) { value = defaultActiveFirst[eventKey]; } updateDefaultActiveFirst(store, eventKey, value); return _this; } SubMenu.prototype.componentDidMount = function componentDidMount() { this.componentDidUpdate(); }; SubMenu.prototype.componentDidUpdate = function componentDidUpdate() { var _this2 = this; var _props = this.props, mode = _props.mode, parentMenu = _props.parentMenu, manualRef = _props.manualRef; // invoke customized ref to expose component to mixin if (manualRef) { manualRef(this); } if (mode !== 'horizontal' || !parentMenu.isRootMenu || !this.props.isOpen) { return; } this.minWidthTimeout = setTimeout(function () { return _this2.adjustWidth(); }, 0); }; SubMenu.prototype.componentWillUnmount = function componentWillUnmount() { var _props2 = this.props, onDestroy = _props2.onDestroy, eventKey = _props2.eventKey; if (onDestroy) { onDestroy(eventKey); } /* istanbul ignore if */ if (this.minWidthTimeout) { clearTimeout(this.minWidthTimeout); } /* istanbul ignore if */ if (this.mouseenterTimeout) { clearTimeout(this.mouseenterTimeout); } }; SubMenu.prototype.renderChildren = function renderChildren(children) { var props = this.props; var baseProps = { mode: props.mode === 'horizontal' ? 'vertical' : props.mode, visible: this.props.isOpen, level: props.level + 1, inlineIndent: props.inlineIndent, focusable: false, onClick: this.onSubMenuClick, onSelect: this.onSelect, onDeselect: this.onDeselect, onDestroy: this.onDestroy, selectedKeys: props.selectedKeys, eventKey: props.eventKey + '-menu-', openKeys: props.openKeys, openTransitionName: props.openTransitionName, openAnimation: props.openAnimation, onOpenChange: this.onOpenChange, subMenuOpenDelay: props.subMenuOpenDelay, parentMenu: this, subMenuCloseDelay: props.subMenuCloseDelay, forceSubMenuRender: props.forceSubMenuRender, triggerSubMenuAction: props.triggerSubMenuAction, builtinPlacements: props.builtinPlacements, defaultActiveFirst: props.store.getState().defaultActiveFirst[getMenuIdFromSubMenuEventKey(props.eventKey)], multiple: props.multiple, prefixCls: props.rootPrefixCls, id: this._menuId, manualRef: this.saveMenuInstance, itemIcon: props.itemIcon, expandIcon: props.expandIcon }; var haveRendered = this.haveRendered; this.haveRendered = true; this.haveOpened = this.haveOpened || baseProps.visible || baseProps.forceSubMenuRender; // never rendered not planning to, don't render if (!this.haveOpened) { return React.createElement('div', null); } // don't show transition on first rendering (no animation for opened menu) // show appear transition if it's not visible (not sure why) // show appear transition if it's not inline mode var transitionAppear = haveRendered || !baseProps.visible || !baseProps.mode === 'inline'; baseProps.className = ' ' + baseProps.prefixCls + '-sub'; var animProps = {}; if (baseProps.openTransitionName) { animProps.transitionName = baseProps.openTransitionName; } else if (typeof baseProps.openAnimation === 'object') { animProps.animation = _extends({}, baseProps.openAnimation); if (!transitionAppear) { delete animProps.animation.appear; } } return React.createElement( Animate, _extends({}, animProps, { showProp: 'visible', component: '', transitionAppear: transitionAppear }), React.createElement( SubPopupMenu, _extends({}, baseProps, { id: this._menuId }), children ) ); }; SubMenu.prototype.render = function render() { var _classNames; var props = _extends({}, this.props); var isOpen = props.isOpen; var prefixCls = this.getPrefixCls(); var isInlineMode = props.mode === 'inline'; var className = classNames(prefixCls, prefixCls + '-' + props.mode, (_classNames = {}, _classNames[props.className] = !!props.className, _classNames[this.getOpenClassName()] = isOpen, _classNames[this.getActiveClassName()] = props.active || isOpen && !isInlineMode, _classNames[this.getDisabledClassName()] = props.disabled, _classNames[this.getSelectedClassName()] = this.isChildrenSelected(), _classNames)); if (!this._menuId) { if (props.eventKey) { this._menuId = props.eventKey + '$Menu'; } else { this._menuId = '$__$' + ++guid + '$Menu'; } } var mouseEvents = {}; var titleClickEvents = {}; var titleMouseEvents = {}; if (!props.disabled) { mouseEvents = { onMouseLeave: this.onMouseLeave, onMouseEnter: this.onMouseEnter }; // only works in title, not outer li titleClickEvents = { onClick: this.onTitleClick }; titleMouseEvents = { onMouseEnter: this.onTitleMouseEnter, onMouseLeave: this.onTitleMouseLeave }; } var style = {}; if (isInlineMode) { style.paddingRight = props.inlineIndent * props.level; } var ariaOwns = {}; // only set aria-owns when menu is open // otherwise it would be an invalid aria-owns value // since corresponding node cannot be found if (this.props.isOpen) { ariaOwns = { 'aria-owns': this._menuId }; } // expand custom icon should NOT be displayed in menu with horizontal mode. var icon = null; if (props.mode !== 'horizontal') { icon = this.props.expandIcon; // ReactNode if (typeof this.props.expandIcon === 'function') { icon = React.createElement(this.props.expandIcon, _extends({}, this.props)); } } var title = React.createElement( 'div', _extends({ ref: this.saveSubMenuTitle, style: style, className: prefixCls + '-title' }, titleMouseEvents, titleClickEvents, { 'aria-expanded': isOpen }, ariaOwns, { 'aria-haspopup': 'true', title: typeof props.title === 'string' ? props.title : undefined }), props.title, icon || React.createElement('i', { className: prefixCls + '-arrow' }) ); var children = this.renderChildren(props.children); var getPopupContainer = props.parentMenu.isRootMenu ? props.parentMenu.props.getPopupContainer : function (triggerNode) { return triggerNode.parentNode; }; var popupPlacement = popupPlacementMap[props.mode]; var popupAlign = props.popupOffset ? { offset: props.popupOffset } : {}; var popupClassName = props.mode === 'inline' ? '' : props.popupClassName; var disabled = props.disabled, triggerSubMenuAction = props.triggerSubMenuAction, subMenuOpenDelay = props.subMenuOpenDelay, forceSubMenuRender = props.forceSubMenuRender, subMenuCloseDelay = props.subMenuCloseDelay, builtinPlacements = props.builtinPlacements; menuAllProps.forEach(function (key) { return delete props[key]; }); // Set onClick to null, to ignore propagated onClick event delete props.onClick; return React.createElement( 'li', _extends({}, props, mouseEvents, { className: className, role: 'menuitem' }), isInlineMode && title, isInlineMode && children, !isInlineMode && React.createElement( Trigger, { prefixCls: prefixCls, popupClassName: prefixCls + '-popup ' + popupClassName, getPopupContainer: getPopupContainer, builtinPlacements: _extends({}, placements, builtinPlacements), popupPlacement: popupPlacement, popupVisible: isOpen, popupAlign: popupAlign, popup: children, action: disabled ? [] : [triggerSubMenuAction], mouseEnterDelay: subMenuOpenDelay, mouseLeaveDelay: subMenuCloseDelay, onPopupVisibleChange: this.onPopupVisibleChange, forceRender: forceSubMenuRender }, title ) ); }; return SubMenu; }(React.Component); SubMenu.propTypes = { parentMenu: PropTypes.object, title: PropTypes.node, children: PropTypes.any, selectedKeys: PropTypes.array, openKeys: PropTypes.array, onClick: PropTypes.func, onOpenChange: PropTypes.func, rootPrefixCls: PropTypes.string, eventKey: PropTypes.string, multiple: PropTypes.bool, active: PropTypes.bool, // TODO: remove onItemHover: PropTypes.func, onSelect: PropTypes.func, triggerSubMenuAction: PropTypes.string, onDeselect: PropTypes.func, onDestroy: PropTypes.func, onMouseEnter: PropTypes.func, onMouseLeave: PropTypes.func, onTitleMouseEnter: PropTypes.func, onTitleMouseLeave: PropTypes.func, onTitleClick: PropTypes.func, popupOffset: PropTypes.array, isOpen: PropTypes.bool, store: PropTypes.object, mode: PropTypes.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']), manualRef: PropTypes.func, itemIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), expandIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node]) }; SubMenu.defaultProps = { onMouseEnter: noop, onMouseLeave: noop, onTitleMouseEnter: noop, onTitleMouseLeave: noop, onTitleClick: noop, manualRef: noop, mode: 'vertical', title: '' }; var _initialiseProps = function _initialiseProps() { var _this3 = this; this.onDestroy = function (key) { _this3.props.onDestroy(key); }; this.onKeyDown = function (e) { var keyCode = e.keyCode; var menu = _this3.menuInstance; var _props3 = _this3.props, isOpen = _props3.isOpen, store = _props3.store; if (keyCode === KeyCode.ENTER) { _this3.onTitleClick(e); updateDefaultActiveFirst(store, _this3.props.eventKey, true); return true; } if (keyCode === KeyCode.RIGHT) { if (isOpen) { menu.onKeyDown(e); } else { _this3.triggerOpenChange(true); // need to update current menu's defaultActiveFirst value updateDefaultActiveFirst(store, _this3.props.eventKey, true); } return true; } if (keyCode === KeyCode.LEFT) { var handled = void 0; if (isOpen) { handled = menu.onKeyDown(e); } else { return undefined; } if (!handled) { _this3.triggerOpenChange(false); handled = true; } return handled; } if (isOpen && (keyCode === KeyCode.UP || keyCode === KeyCode.DOWN)) { return menu.onKeyDown(e); } }; this.onOpenChange = function (e) { _this3.props.onOpenChange(e); }; this.onPopupVisibleChange = function (visible) { _this3.triggerOpenChange(visible, visible ? 'mouseenter' : 'mouseleave'); }; this.onMouseEnter = function (e) { var _props4 = _this3.props, key = _props4.eventKey, onMouseEnter = _props4.onMouseEnter, store = _props4.store; updateDefaultActiveFirst(store, _this3.props.eventKey, false); onMouseEnter({ key: key, domEvent: e }); }; this.onMouseLeave = function (e) { var _props5 = _this3.props, parentMenu = _props5.parentMenu, eventKey = _props5.eventKey, onMouseLeave = _props5.onMouseLeave; parentMenu.subMenuInstance = _this3; onMouseLeave({ key: eventKey, domEvent: e }); }; this.onTitleMouseEnter = function (domEvent) { var _props6 = _this3.props, key = _props6.eventKey, onItemHover = _props6.onItemHover, onTitleMouseEnter = _props6.onTitleMouseEnter; onItemHover({ key: key, hover: true }); onTitleMouseEnter({ key: key, domEvent: domEvent }); }; this.onTitleMouseLeave = function (e) { var _props7 = _this3.props, parentMenu = _props7.parentMenu, eventKey = _props7.eventKey, onItemHover = _props7.onItemHover, onTitleMouseLeave = _props7.onTitleMouseLeave; parentMenu.subMenuInstance = _this3; onItemHover({ key: eventKey, hover: false }); onTitleMouseLeave({ key: eventKey, domEvent: e }); }; this.onTitleClick = function (e) { var props = _this3.props; props.onTitleClick({ key: props.eventKey, domEvent: e }); if (props.triggerSubMenuAction === 'hover') { return; } _this3.triggerOpenChange(!props.isOpen, 'click'); updateDefaultActiveFirst(props.store, _this3.props.eventKey, false); }; this.onSubMenuClick = function (info) { // in the case of overflowed submenu // onClick is not copied over if (typeof _this3.props.onClick === 'function') { _this3.props.onClick(_this3.addKeyPath(info)); } }; this.onSelect = function (info) { _this3.props.onSelect(info); }; this.onDeselect = function (info) { _this3.props.onDeselect(info); }; this.getPrefixCls = function () { return _this3.props.rootPrefixCls + '-submenu'; }; this.getActiveClassName = function () { return _this3.getPrefixCls() + '-active'; }; this.getDisabledClassName = function () { return _this3.getPrefixCls() + '-disabled'; }; this.getSelectedClassName = function () { return _this3.getPrefixCls() + '-selected'; }; this.getOpenClassName = function () { return _this3.props.rootPrefixCls + '-submenu-open'; }; this.saveMenuInstance = function (c) { // children menu instance _this3.menuInstance = c; }; this.addKeyPath = function (info) { return _extends({}, info, { keyPath: (info.keyPath || []).concat(_this3.props.eventKey) }); }; this.triggerOpenChange = function (open, type) { var key = _this3.props.eventKey; var openChange = function openChange() { _this3.onOpenChange({ key: key, item: _this3, trigger: type, open: open }); }; if (type === 'mouseenter') { // make sure mouseenter happen after other menu item's mouseleave _this3.mouseenterTimeout = setTimeout(function () { openChange(); }, 0); } else { openChange(); } }; this.isChildrenSelected = function () { var ret = { find: false }; loopMenuItemRecursively(_this3.props.children, _this3.props.selectedKeys, ret); return ret.find; }; this.isOpen = function () { return _this3.props.openKeys.indexOf(_this3.props.eventKey) !== -1; }; this.adjustWidth = function () { /* istanbul ignore if */ if (!_this3.subMenuTitle || !_this3.menuInstance) { return; } var popupMenu = ReactDOM.findDOMNode(_this3.menuInstance); if (popupMenu.offsetWidth >= _this3.subMenuTitle.offsetWidth) { return; } /* istanbul ignore next */ popupMenu.style.minWidth = _this3.subMenuTitle.offsetWidth + 'px'; }; this.saveSubMenuTitle = function (subMenuTitle) { _this3.subMenuTitle = subMenuTitle; }; }; var connected = connect(function (_ref, _ref2) { var openKeys = _ref.openKeys, activeKey = _ref.activeKey, selectedKeys = _ref.selectedKeys; var eventKey = _ref2.eventKey, subMenuKey = _ref2.subMenuKey; return { isOpen: openKeys.indexOf(eventKey) > -1, active: activeKey[subMenuKey] === eventKey, selectedKeys: selectedKeys }; })(SubMenu); connected.isSubMenu = true; export default connected;