2018-03-19 10:16:27 +08:00
|
|
|
|
2018-02-06 17:22:36 +08:00
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
import KeyCode from '../_util/KeyCode'
|
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
2018-09-05 21:28:54 +08:00
|
|
|
import scrollIntoView from 'dom-scroll-into-view'
|
|
|
|
import { connect } from '../_util/store'
|
|
|
|
import { noop, menuAllProps } from './util'
|
|
|
|
|
2018-01-16 15:41:00 +08:00
|
|
|
const props = {
|
2018-09-05 21:28:54 +08:00
|
|
|
attribute: PropTypes.object,
|
2018-01-16 15:41:00 +08:00
|
|
|
rootPrefixCls: PropTypes.string,
|
2018-02-22 12:07:37 +08:00
|
|
|
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2018-01-16 15:41:00 +08:00
|
|
|
active: PropTypes.bool,
|
|
|
|
selectedKeys: PropTypes.array,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
title: PropTypes.string,
|
|
|
|
index: PropTypes.number,
|
|
|
|
inlineIndent: PropTypes.number.def(24),
|
|
|
|
level: PropTypes.number.def(1),
|
|
|
|
mode: PropTypes.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']).def('vertical'),
|
|
|
|
parentMenu: PropTypes.object,
|
2018-02-12 14:45:26 +08:00
|
|
|
multiple: PropTypes.bool,
|
2018-02-28 19:07:04 +08:00
|
|
|
value: PropTypes.any,
|
2018-09-05 21:28:54 +08:00
|
|
|
isSelected: PropTypes.bool,
|
|
|
|
manualRef: PropTypes.func.def(noop),
|
|
|
|
role: PropTypes.any,
|
|
|
|
subMenuKey: PropTypes.string,
|
2018-01-17 16:12:53 +08:00
|
|
|
// clearSubMenuTimers: PropTypes.func.def(noop),
|
2018-01-16 15:41:00 +08:00
|
|
|
}
|
2017-12-12 13:49:02 +08:00
|
|
|
const MenuItem = {
|
|
|
|
name: 'MenuItem',
|
2018-01-16 15:41:00 +08:00
|
|
|
props,
|
2018-01-12 16:10:41 +08:00
|
|
|
mixins: [BaseMixin],
|
2018-01-04 19:06:54 +08:00
|
|
|
isMenuItem: true,
|
2018-09-05 21:28:54 +08:00
|
|
|
created () {
|
|
|
|
// invoke customized ref to expose component to mixin
|
|
|
|
this.callRef()
|
|
|
|
},
|
|
|
|
updated () {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.active) {
|
|
|
|
scrollIntoView(this.$el, this.parentMenu.$el, {
|
|
|
|
onlyScrollIfNeeded: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.callRef()
|
|
|
|
},
|
2017-12-12 13:49:02 +08:00
|
|
|
beforeDestroy () {
|
|
|
|
const props = this.$props
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('destroy', props.eventKey)
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onKeyDown (e) {
|
|
|
|
const keyCode = e.keyCode
|
|
|
|
if (keyCode === KeyCode.ENTER) {
|
2018-02-09 18:42:19 +08:00
|
|
|
this.onClick(e)
|
2017-12-12 13:49:02 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onMouseLeave (e) {
|
|
|
|
const { eventKey } = this.$props
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('itemHover', {
|
2017-12-12 13:49:02 +08:00
|
|
|
key: eventKey,
|
|
|
|
hover: false,
|
|
|
|
})
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('mouseleave', {
|
2017-12-12 13:49:02 +08:00
|
|
|
key: eventKey,
|
|
|
|
domEvent: e,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
onMouseEnter (e) {
|
2018-01-17 16:12:53 +08:00
|
|
|
const { eventKey } = this
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('itemHover', {
|
2017-12-12 13:49:02 +08:00
|
|
|
key: eventKey,
|
|
|
|
hover: true,
|
|
|
|
})
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('mouseenter', {
|
2017-12-12 13:49:02 +08:00
|
|
|
key: eventKey,
|
|
|
|
domEvent: e,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
onClick (e) {
|
2018-09-05 21:28:54 +08:00
|
|
|
const { eventKey, multiple, isSelected } = this.$props
|
2017-12-12 13:49:02 +08:00
|
|
|
const info = {
|
|
|
|
key: eventKey,
|
|
|
|
keyPath: [eventKey],
|
|
|
|
item: this,
|
|
|
|
domEvent: e,
|
|
|
|
}
|
2018-01-17 16:12:53 +08:00
|
|
|
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('click', info)
|
2017-12-12 13:49:02 +08:00
|
|
|
if (multiple) {
|
2018-09-05 21:28:54 +08:00
|
|
|
if (isSelected) {
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('deselect', info)
|
2017-12-12 13:49:02 +08:00
|
|
|
} else {
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('select', info)
|
2017-12-12 13:49:02 +08:00
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
} else if (!isSelected) {
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('select', info)
|
2017-12-12 13:49:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getPrefixCls () {
|
|
|
|
return `${this.$props.rootPrefixCls}-item`
|
|
|
|
},
|
|
|
|
|
|
|
|
getActiveClassName () {
|
|
|
|
return `${this.getPrefixCls()}-active`
|
|
|
|
},
|
|
|
|
|
|
|
|
getSelectedClassName () {
|
|
|
|
return `${this.getPrefixCls()}-selected`
|
|
|
|
},
|
|
|
|
|
|
|
|
getDisabledClassName () {
|
|
|
|
return `${this.getPrefixCls()}-disabled`
|
|
|
|
},
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
callRef () {
|
|
|
|
if (this.manualRef) {
|
|
|
|
this.manualRef(this)
|
|
|
|
}
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
2018-09-05 21:28:54 +08:00
|
|
|
const props = { ...this.$props }
|
2017-12-12 13:49:02 +08:00
|
|
|
const className = {
|
|
|
|
[this.getPrefixCls()]: true,
|
|
|
|
[this.getActiveClassName()]: !props.disabled && props.active,
|
2018-09-05 21:28:54 +08:00
|
|
|
[this.getSelectedClassName()]: props.isSelected,
|
2017-12-12 13:49:02 +08:00
|
|
|
[this.getDisabledClassName()]: props.disabled,
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
let attrs = {
|
2017-12-12 13:49:02 +08:00
|
|
|
...props.attribute,
|
|
|
|
title: props.title,
|
|
|
|
role: 'menuitem',
|
|
|
|
'aria-disabled': props.disabled,
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
if (props.role === 'option') {
|
|
|
|
// overwrite to option
|
|
|
|
attrs = {
|
|
|
|
...attrs,
|
|
|
|
role: 'option',
|
|
|
|
'aria-selected': props.isSelected,
|
2017-12-12 13:49:02 +08:00
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
} else if (props.role === null) {
|
|
|
|
// sometimes we want to specify role inside <li/> element
|
|
|
|
// <li><a role='menuitem'>Link</a></li> would be a good example
|
|
|
|
delete attrs.role
|
|
|
|
}
|
|
|
|
// In case that onClick/onMouseLeave/onMouseEnter is passed down from owner
|
|
|
|
const mouseEvent = {
|
|
|
|
click: props.disabled ? noop : this.onClick,
|
|
|
|
mouseleave: props.disabled ? noop : this.onMouseLeave,
|
|
|
|
mouseenter: props.disabled ? noop : this.onMouseEnter,
|
2017-12-12 13:49:02 +08:00
|
|
|
}
|
2018-01-08 18:31:04 +08:00
|
|
|
|
2017-12-12 13:49:02 +08:00
|
|
|
const style = {}
|
|
|
|
if (props.mode === 'inline') {
|
|
|
|
style.paddingLeft = `${props.inlineIndent * props.level}px`
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
const listeners = { ...this.$listeners }
|
|
|
|
menuAllProps.props.forEach(key => delete props[key])
|
|
|
|
menuAllProps.on.forEach(key => delete listeners[key])
|
2017-12-12 13:49:02 +08:00
|
|
|
const liProps = {
|
2018-09-05 21:28:54 +08:00
|
|
|
attrs: {
|
|
|
|
...props,
|
|
|
|
...attrs,
|
|
|
|
},
|
2017-12-12 13:49:02 +08:00
|
|
|
on: {
|
2018-09-05 21:28:54 +08:00
|
|
|
...listeners,
|
2017-12-29 18:45:11 +08:00
|
|
|
...mouseEvent,
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<li
|
|
|
|
{...liProps}
|
|
|
|
style={style}
|
|
|
|
class={className}
|
|
|
|
>
|
|
|
|
{this.$slots.default}
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
const connected = connect(({ activeKey, selectedKeys }, { eventKey, subMenuKey }) => ({
|
|
|
|
active: activeKey[subMenuKey] === eventKey,
|
|
|
|
isSelected: selectedKeys.indexOf(eventKey) !== -1,
|
|
|
|
}))(MenuItem)
|
|
|
|
|
|
|
|
export default connected
|
2018-01-16 15:41:00 +08:00
|
|
|
export { props as menuItemProps }
|
2018-03-19 10:16:27 +08:00
|
|
|
|