2018-09-05 21:28:54 +08:00
|
|
|
|
import omit from 'omit.js'
|
|
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
|
import { connect } from '../_util/store'
|
2018-02-06 17:22:36 +08:00
|
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
2018-09-05 21:28:54 +08:00
|
|
|
|
import KeyCode from '../_util/KeyCode'
|
|
|
|
|
import classNames from 'classnames'
|
|
|
|
|
import { getKeyFromChildrenIndex, loopMenuItem, noop } from './util'
|
|
|
|
|
import DOMWrap from './DOMWrap'
|
|
|
|
|
import { cloneElement } from '../_util/vnode'
|
2018-11-05 21:03:25 +08:00
|
|
|
|
import { initDefaultProps, getOptionProps, getPropsData, getEvents, getComponentFromProp } from '../_util/props-util'
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
|
|
function allDisabled (arr) {
|
|
|
|
|
if (!arr.length) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return arr.every(c => {
|
|
|
|
|
return !!c.disabled
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateActiveKey (store, menuId, activeKey) {
|
|
|
|
|
const state = store.getState()
|
|
|
|
|
store.setState({
|
|
|
|
|
activeKey: {
|
|
|
|
|
...state.activeKey,
|
|
|
|
|
[menuId]: activeKey,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 21:03:25 +08:00
|
|
|
|
function getEventKey (props) {
|
|
|
|
|
// when eventKey not available ,it's menu and return menu id '0-menu-'
|
|
|
|
|
return props.eventKey || '0-menu-'
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
|
export function saveRef (key, c) {
|
|
|
|
|
if (c) {
|
|
|
|
|
const index = this.instanceArrayKeyIndexMap[key]
|
|
|
|
|
this.instanceArray[index] = c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export function getActiveKey (props, originalActiveKey) {
|
|
|
|
|
let activeKey = originalActiveKey
|
|
|
|
|
const { eventKey, defaultActiveFirst, children } = props
|
2018-09-19 13:14:46 +08:00
|
|
|
|
if (activeKey !== undefined && activeKey !== null) {
|
2018-09-05 21:28:54 +08:00
|
|
|
|
let found
|
|
|
|
|
loopMenuItem(children, (c, i) => {
|
|
|
|
|
const propsData = c.componentOptions.propsData || {}
|
|
|
|
|
if (c && !propsData.disabled && activeKey === getKeyFromChildrenIndex(c, eventKey, i)) {
|
|
|
|
|
found = true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if (found) {
|
|
|
|
|
return activeKey
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
activeKey = null
|
|
|
|
|
if (defaultActiveFirst) {
|
|
|
|
|
loopMenuItem(children, (c, i) => {
|
|
|
|
|
const propsData = c.componentOptions.propsData || {}
|
|
|
|
|
if (!activeKey && c && !propsData.disabled) {
|
|
|
|
|
activeKey = getKeyFromChildrenIndex(c, eventKey, i)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return activeKey
|
|
|
|
|
}
|
|
|
|
|
return activeKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SubPopupMenu = {
|
2017-12-29 18:45:11 +08:00
|
|
|
|
name: 'SubPopupMenu',
|
2018-09-05 21:28:54 +08:00
|
|
|
|
props: initDefaultProps({
|
|
|
|
|
// onSelect: PropTypes.func,
|
|
|
|
|
// onClick: PropTypes.func,
|
|
|
|
|
// onDeselect: PropTypes.func,
|
|
|
|
|
// onOpenChange: PropTypes.func,
|
|
|
|
|
// onDestroy: PropTypes.func,
|
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
|
openTransitionName: PropTypes.string,
|
|
|
|
|
openAnimation: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
|
|
|
|
openKeys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
|
|
|
|
visible: PropTypes.bool,
|
|
|
|
|
parentMenu: PropTypes.object,
|
|
|
|
|
eventKey: PropTypes.string,
|
|
|
|
|
store: PropTypes.object,
|
|
|
|
|
|
|
|
|
|
// adding in refactor
|
|
|
|
|
focusable: PropTypes.bool,
|
|
|
|
|
multiple: PropTypes.bool,
|
|
|
|
|
defaultActiveFirst: PropTypes.bool,
|
|
|
|
|
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
|
selectedKeys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
|
|
|
|
defaultSelectedKeys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
|
|
|
|
defaultOpenKeys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
|
|
|
|
level: PropTypes.number,
|
|
|
|
|
mode: PropTypes.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']),
|
|
|
|
|
triggerSubMenuAction: PropTypes.oneOf(['click', 'hover']),
|
|
|
|
|
inlineIndent: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
|
|
|
|
manualRef: PropTypes.func,
|
2018-11-05 21:03:25 +08:00
|
|
|
|
itemIcon: PropTypes.any,
|
|
|
|
|
expandIcon: PropTypes.any,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
children: PropTypes.any.def([]),
|
|
|
|
|
__propsSymbol__: PropTypes.any, // mock componentWillReceiveProps
|
|
|
|
|
}, {
|
|
|
|
|
prefixCls: 'rc-menu',
|
|
|
|
|
mode: 'vertical',
|
|
|
|
|
level: 1,
|
|
|
|
|
inlineIndent: 24,
|
|
|
|
|
visible: true,
|
|
|
|
|
focusable: true,
|
|
|
|
|
manualRef: noop,
|
|
|
|
|
}),
|
2017-12-08 14:31:53 +08:00
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
|
created () {
|
|
|
|
|
const props = this.$props
|
|
|
|
|
props.store.setState({
|
|
|
|
|
activeKey: {
|
|
|
|
|
...props.store.getState().activeKey,
|
|
|
|
|
[props.eventKey]: getActiveKey(props, props.activeKey),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
this.instanceArray = []
|
|
|
|
|
},
|
|
|
|
|
mounted () {
|
|
|
|
|
// invoke customized ref to expose component to mixin
|
|
|
|
|
if (this.manualRef) {
|
|
|
|
|
this.manualRef(this)
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-11-05 21:03:25 +08:00
|
|
|
|
updated () {
|
|
|
|
|
const props = getOptionProps(this)
|
|
|
|
|
const originalActiveKey = 'activeKey' in props ? props.activeKey
|
|
|
|
|
: props.store.getState().activeKey[getEventKey(props)]
|
|
|
|
|
const activeKey = getActiveKey(props, originalActiveKey)
|
|
|
|
|
if (activeKey !== originalActiveKey) {
|
|
|
|
|
updateActiveKey(props.store, getEventKey(props), activeKey)
|
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
},
|
2018-01-03 18:30:12 +08:00
|
|
|
|
methods: {
|
2018-09-05 21:28:54 +08:00
|
|
|
|
// all keyboard events callbacks run from here at first
|
|
|
|
|
onKeyDown (e, callback) {
|
|
|
|
|
const keyCode = e.keyCode
|
|
|
|
|
let handled
|
|
|
|
|
this.getFlatInstanceArray().forEach((obj) => {
|
|
|
|
|
if (obj && obj.active && obj.onKeyDown) {
|
|
|
|
|
handled = obj.onKeyDown(e)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if (handled) {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
let activeItem = null
|
|
|
|
|
if (keyCode === KeyCode.UP || keyCode === KeyCode.DOWN) {
|
|
|
|
|
activeItem = this.step(keyCode === KeyCode.UP ? -1 : 1)
|
|
|
|
|
}
|
|
|
|
|
if (activeItem) {
|
|
|
|
|
e.preventDefault()
|
2018-11-05 21:03:25 +08:00
|
|
|
|
updateActiveKey(this.$props.store, getEventKey(this.$props), activeItem.eventKey)
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
callback(activeItem)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onItemHover (e) {
|
|
|
|
|
const { key, hover } = e
|
2018-11-05 21:03:25 +08:00
|
|
|
|
updateActiveKey(this.$props.store, getEventKey(this.$props), hover ? key : null)
|
2018-09-05 21:28:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
2018-01-03 18:30:12 +08:00
|
|
|
|
onDeselect (selectInfo) {
|
2018-01-12 16:10:41 +08:00
|
|
|
|
this.__emit('deselect', selectInfo)
|
2018-01-03 18:30:12 +08:00
|
|
|
|
},
|
2017-12-08 14:31:53 +08:00
|
|
|
|
|
2018-01-03 18:30:12 +08:00
|
|
|
|
onSelect (selectInfo) {
|
2018-01-12 16:10:41 +08:00
|
|
|
|
this.__emit('select', selectInfo)
|
2018-01-03 18:30:12 +08:00
|
|
|
|
},
|
2017-12-08 14:31:53 +08:00
|
|
|
|
|
2018-01-03 18:30:12 +08:00
|
|
|
|
onClick (e) {
|
2018-01-12 16:10:41 +08:00
|
|
|
|
this.__emit('click', e)
|
2018-01-03 18:30:12 +08:00
|
|
|
|
},
|
2017-12-08 14:31:53 +08:00
|
|
|
|
|
2018-01-03 18:30:12 +08:00
|
|
|
|
onOpenChange (e) {
|
2018-01-12 16:10:41 +08:00
|
|
|
|
this.__emit('openChange', e)
|
2018-01-03 18:30:12 +08:00
|
|
|
|
},
|
2017-12-08 14:31:53 +08:00
|
|
|
|
|
2018-01-03 18:30:12 +08:00
|
|
|
|
onDestroy (key) {
|
2018-01-12 16:10:41 +08:00
|
|
|
|
this.__emit('destroy', key)
|
2018-01-03 18:30:12 +08:00
|
|
|
|
},
|
2017-12-08 14:31:53 +08:00
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
|
getFlatInstanceArray () {
|
|
|
|
|
return this.instanceArray
|
|
|
|
|
},
|
|
|
|
|
|
2018-01-03 18:30:12 +08:00
|
|
|
|
getOpenTransitionName () {
|
|
|
|
|
return this.$props.openTransitionName
|
|
|
|
|
},
|
2017-12-08 14:31:53 +08:00
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
|
step (direction) {
|
|
|
|
|
let children = this.getFlatInstanceArray()
|
2018-11-06 10:05:23 +08:00
|
|
|
|
const activeKey = this.$props.store.getState().activeKey[getEventKey(this.$props)]
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const len = children.length
|
|
|
|
|
if (!len) {
|
2018-01-03 18:30:12 +08:00
|
|
|
|
return null
|
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
if (direction < 0) {
|
|
|
|
|
children = children.concat().reverse()
|
|
|
|
|
}
|
|
|
|
|
// find current activeIndex
|
|
|
|
|
let activeIndex = -1
|
|
|
|
|
children.every((c, ci) => {
|
|
|
|
|
if (c && c.eventKey === activeKey) {
|
|
|
|
|
activeIndex = ci
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
if (
|
|
|
|
|
!this.defaultActiveFirst && activeIndex !== -1 &&
|
|
|
|
|
allDisabled(children.slice(activeIndex, len - 1))
|
|
|
|
|
) {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
const start = (activeIndex + 1) % len
|
|
|
|
|
let i = start
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
const child = children[i]
|
|
|
|
|
if (!child || child.disabled) {
|
|
|
|
|
i = (i + 1) % len
|
|
|
|
|
} else {
|
|
|
|
|
return child
|
|
|
|
|
}
|
|
|
|
|
} while (i !== start)
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
},
|
2018-11-05 21:03:25 +08:00
|
|
|
|
getIcon (instance, name) {
|
|
|
|
|
if (instance.$createElement) {
|
|
|
|
|
const temp = instance[name]
|
|
|
|
|
if (temp !== undefined) {
|
|
|
|
|
return temp
|
|
|
|
|
}
|
|
|
|
|
return instance.$slots[name] || instance.$scopedSlots[name]
|
|
|
|
|
} else {
|
|
|
|
|
const temp = getPropsData(instance)[name]
|
|
|
|
|
if (temp !== undefined) {
|
|
|
|
|
return temp
|
|
|
|
|
}
|
|
|
|
|
const slotsProp = []
|
|
|
|
|
const componentOptions = instance.componentOptions || {};
|
|
|
|
|
(componentOptions.children || []).forEach((child) => {
|
|
|
|
|
if (child.data && child.data.slot === name) {
|
|
|
|
|
if (child.tag === 'template') {
|
|
|
|
|
slotsProp.push(child.children)
|
|
|
|
|
} else {
|
|
|
|
|
slotsProp.push(child)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return slotsProp.length ? slotsProp : undefined
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
|
renderCommonMenuItem (child, i, extraProps) {
|
|
|
|
|
if (child.tag === undefined) { return child }
|
2018-11-05 21:03:25 +08:00
|
|
|
|
const state = this.$props.store.getState()
|
2018-01-03 18:30:12 +08:00
|
|
|
|
const props = this.$props
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const key = getKeyFromChildrenIndex(child, props.eventKey, i)
|
|
|
|
|
const childProps = child.componentOptions.propsData || {}
|
2018-11-06 10:05:23 +08:00
|
|
|
|
|
|
|
|
|
const isActive = key === state.activeKey[getEventKey(this.$props)]
|
2018-09-05 21:28:54 +08:00
|
|
|
|
if (!childProps.disabled) {
|
|
|
|
|
// manualRef的执行顺序不能保证,使用key映射ref在this.instanceArray中的位置
|
|
|
|
|
this.instanceArrayKeyIndexMap[key] = Object.keys(this.instanceArrayKeyIndexMap).length
|
|
|
|
|
}
|
|
|
|
|
const childListeners = getEvents(child)
|
|
|
|
|
const newChildProps = {
|
|
|
|
|
props: {
|
2018-11-05 21:03:25 +08:00
|
|
|
|
mode: childProps.mode || props.mode,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
level: props.level,
|
|
|
|
|
inlineIndent: props.inlineIndent,
|
|
|
|
|
renderMenuItem: this.renderMenuItem,
|
|
|
|
|
rootPrefixCls: props.prefixCls,
|
|
|
|
|
index: i,
|
|
|
|
|
parentMenu: props.parentMenu,
|
|
|
|
|
// customized ref function, need to be invoked manually in child's componentDidMount
|
|
|
|
|
manualRef: childProps.disabled ? noop : saveRef.bind(this, key),
|
|
|
|
|
eventKey: key,
|
|
|
|
|
active: !childProps.disabled && isActive,
|
|
|
|
|
multiple: props.multiple,
|
|
|
|
|
openTransitionName: this.getOpenTransitionName(),
|
|
|
|
|
openAnimation: props.openAnimation,
|
|
|
|
|
subMenuOpenDelay: props.subMenuOpenDelay,
|
|
|
|
|
subMenuCloseDelay: props.subMenuCloseDelay,
|
|
|
|
|
forceSubMenuRender: props.forceSubMenuRender,
|
2018-11-05 21:03:25 +08:00
|
|
|
|
builtinPlacements: props.builtinPlacements,
|
|
|
|
|
itemIcon: this.getIcon(child, 'itemIcon') || this.getIcon(this, 'itemIcon'),
|
|
|
|
|
expandIcon: this.getIcon(child, 'expandIcon') || this.getIcon(this, 'expandIcon'),
|
2018-09-05 21:28:54 +08:00
|
|
|
|
...extraProps,
|
|
|
|
|
},
|
|
|
|
|
on: {
|
|
|
|
|
click: (e) => {
|
|
|
|
|
(childListeners.click || noop)(e)
|
|
|
|
|
this.onClick(e)
|
|
|
|
|
},
|
|
|
|
|
itemHover: this.onItemHover,
|
|
|
|
|
openChange: this.onOpenChange,
|
|
|
|
|
deselect: this.onDeselect,
|
|
|
|
|
// destroy: this.onDestroy,
|
|
|
|
|
select: this.onSelect,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
if (props.mode === 'inline') {
|
|
|
|
|
newChildProps.props.triggerSubMenuAction = 'click'
|
|
|
|
|
}
|
|
|
|
|
return cloneElement(child, newChildProps)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
renderMenuItem (c, i, subMenuKey) {
|
|
|
|
|
if (!c) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
2018-11-05 21:03:25 +08:00
|
|
|
|
const state = this.$props.store.getState()
|
2018-01-03 18:30:12 +08:00
|
|
|
|
const extraProps = {
|
2018-09-05 21:28:54 +08:00
|
|
|
|
openKeys: state.openKeys,
|
|
|
|
|
selectedKeys: state.selectedKeys,
|
|
|
|
|
triggerSubMenuAction: this.triggerSubMenuAction,
|
2018-01-03 18:30:12 +08:00
|
|
|
|
isRootMenu: false,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
subMenuKey,
|
2018-01-03 18:30:12 +08:00
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
return this.renderCommonMenuItem(c, i, extraProps)
|
2018-01-03 18:30:12 +08:00
|
|
|
|
},
|
2017-12-08 14:31:53 +08:00
|
|
|
|
},
|
|
|
|
|
render () {
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const { ...props } = this.$props
|
2018-11-06 11:59:01 +08:00
|
|
|
|
const { eventKey, prefixCls, visible, level, mode, theme } = props
|
2018-09-05 21:28:54 +08:00
|
|
|
|
this.instanceArray = []
|
|
|
|
|
this.instanceArrayKeyIndexMap = {}
|
|
|
|
|
const className = classNames(
|
|
|
|
|
props.prefixCls,
|
|
|
|
|
`${props.prefixCls}-${props.mode}`,
|
|
|
|
|
)
|
|
|
|
|
const domWrapProps = {
|
|
|
|
|
props: {
|
|
|
|
|
tag: 'ul',
|
|
|
|
|
// hiddenClassName: `${prefixCls}-hidden`,
|
|
|
|
|
visible,
|
2018-11-06 11:59:01 +08:00
|
|
|
|
prefixCls,
|
|
|
|
|
level,
|
|
|
|
|
mode,
|
|
|
|
|
theme,
|
2018-11-05 21:03:25 +08:00
|
|
|
|
overflowedIndicator: getComponentFromProp(this, 'overflowedIndicator'),
|
2018-09-05 21:28:54 +08:00
|
|
|
|
},
|
|
|
|
|
attrs: {
|
|
|
|
|
role: props.role || 'menu',
|
|
|
|
|
},
|
|
|
|
|
class: className,
|
|
|
|
|
// Otherwise, the propagated click event will trigger another onClick
|
|
|
|
|
on: omit(this.$listeners || {}, ['click']),
|
|
|
|
|
}
|
|
|
|
|
// if (props.id) {
|
|
|
|
|
// domProps.id = props.id
|
|
|
|
|
// }
|
|
|
|
|
if (props.focusable) {
|
|
|
|
|
domWrapProps.attrs.tabIndex = '0'
|
|
|
|
|
domWrapProps.on.keydown = this.onKeyDown
|
2018-01-19 18:18:17 +08:00
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
return (
|
|
|
|
|
// ESLint is not smart enough to know that the type of `children` was checked.
|
|
|
|
|
/* eslint-disable */
|
|
|
|
|
<DOMWrap
|
|
|
|
|
{...domWrapProps}
|
|
|
|
|
>
|
|
|
|
|
{props.children.map((c, i) => this.renderMenuItem(c, i, eventKey || '0-menu-'))}
|
|
|
|
|
</DOMWrap>
|
|
|
|
|
/*eslint -enable */
|
|
|
|
|
)
|
2017-12-08 14:31:53 +08:00
|
|
|
|
},
|
2017-12-29 18:45:11 +08:00
|
|
|
|
}
|
2018-03-19 10:16:27 +08:00
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
|
export default connect()(SubPopupMenu);
|
|
|
|
|
|