You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/vc-menu/MenuItem.vue

164 lines
3.8 KiB

7 years ago
<script>
7 years ago
import PropTypes from '../_util/vue-types'
import KeyCode from '../_util/KeyCode'
7 years ago
// import { noop } from './util'
7 years ago
import BaseMixin from '../_util/BaseMixin'
7 years ago
const props = {
rootPrefixCls: PropTypes.string,
7 years ago
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
7 years ago
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,
multiple: PropTypes.bool,
7 years ago
value: PropTypes.any,
7 years ago
// clearSubMenuTimers: PropTypes.func.def(noop),
7 years ago
}
7 years ago
const MenuItem = {
name: 'MenuItem',
7 years ago
props,
7 years ago
inject: {
parentMenuContext: { default: undefined },
},
7 years ago
mixins: [BaseMixin],
7 years ago
isMenuItem: true,
7 years ago
beforeDestroy () {
const props = this.$props
7 years ago
this.__emit('destroy', props.eventKey)
7 years ago
},
methods: {
onKeyDown (e) {
const keyCode = e.keyCode
if (keyCode === KeyCode.ENTER) {
7 years ago
this.onClick(e)
7 years ago
return true
}
},
onMouseLeave (e) {
const { eventKey } = this.$props
7 years ago
this.__emit('itemHover', {
7 years ago
key: eventKey,
hover: false,
})
7 years ago
this.__emit('mouseleave', {
7 years ago
key: eventKey,
domEvent: e,
})
},
onMouseEnter (e) {
7 years ago
const { eventKey } = this
// if (parentMenuContext && parentMenuContext.subMenuInstance) {
// parentMenuContext.subMenuInstance.clearSubMenuTimers()
// }
7 years ago
this.__emit('itemHover', {
7 years ago
key: eventKey,
hover: true,
})
7 years ago
this.__emit('mouseenter', {
7 years ago
key: eventKey,
domEvent: e,
})
},
onClick (e) {
const { eventKey, multiple } = this.$props
const selected = this.isSelected()
const info = {
key: eventKey,
keyPath: [eventKey],
item: this,
domEvent: e,
}
7 years ago
7 years ago
this.__emit('click', info)
7 years ago
if (multiple) {
if (selected) {
7 years ago
this.__emit('deselect', info)
7 years ago
} else {
7 years ago
this.__emit('select', info)
7 years ago
}
} else if (!selected) {
7 years ago
this.__emit('select', info)
7 years ago
}
},
getPrefixCls () {
return `${this.$props.rootPrefixCls}-item`
},
getActiveClassName () {
return `${this.getPrefixCls()}-active`
},
getSelectedClassName () {
return `${this.getPrefixCls()}-selected`
},
getDisabledClassName () {
return `${this.getPrefixCls()}-disabled`
},
isSelected () {
7 years ago
return this.$props.selectedKeys && this.$props.selectedKeys.indexOf(this.$props.eventKey) !== -1
7 years ago
},
},
render () {
const props = this.$props
const selected = this.isSelected()
const className = {
[this.getPrefixCls()]: true,
[this.getActiveClassName()]: !props.disabled && props.active,
[this.getSelectedClassName()]: selected,
[this.getDisabledClassName()]: props.disabled,
}
const attrs = {
...props.attribute,
title: props.title,
role: 'menuitem',
'aria-selected': selected,
'aria-disabled': props.disabled,
}
let mouseEvent = {}
7 years ago
7 years ago
if (!props.disabled) {
mouseEvent = {
click: this.onClick,
mouseleave: this.onMouseLeave,
mouseenter: this.onMouseEnter,
}
}
7 years ago
7 years ago
const style = {}
if (props.mode === 'inline') {
style.paddingLeft = `${props.inlineIndent * props.level}px`
}
const liProps = {
attrs,
on: {
7 years ago
...mouseEvent,
7 years ago
},
}
return (
<li
{...liProps}
style={style}
class={className}
>
{this.$slots.default}
</li>
)
},
}
export default MenuItem
7 years ago
export { props as menuItemProps }
7 years ago
</script>