2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import KeyCode from '../_util/KeyCode';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import scrollIntoView from 'dom-scroll-into-view';
|
|
|
|
import { noop, menuAllProps } from './util';
|
2020-06-18 10:51:56 +00:00
|
|
|
import { getComponent, getSlot, findDOMNode } from '../_util/props-util';
|
2020-12-08 03:50:52 +00:00
|
|
|
import { computed, defineComponent, inject, onBeforeUnmount, onMounted, toRaw } from 'vue';
|
2020-11-23 06:21:18 +00:00
|
|
|
import InjectExtraProps from './InjectExtraProps';
|
2018-01-16 07:41:00 +00:00
|
|
|
const props = {
|
2018-09-05 13:28:54 +00:00
|
|
|
attribute: PropTypes.object,
|
2018-01-16 07:41:00 +00:00
|
|
|
rootPrefixCls: PropTypes.string,
|
2018-02-22 04:07:37 +00:00
|
|
|
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2020-10-10 10:16:28 +00:00
|
|
|
active: PropTypes.looseBool,
|
2018-01-16 07:41:00 +00:00
|
|
|
selectedKeys: PropTypes.array,
|
2020-10-10 10:16:28 +00:00
|
|
|
disabled: PropTypes.looseBool,
|
2019-01-05 03:24:25 +00:00
|
|
|
title: PropTypes.any,
|
2018-01-16 07:41:00 +00:00
|
|
|
index: PropTypes.number,
|
|
|
|
inlineIndent: PropTypes.number.def(24),
|
|
|
|
level: PropTypes.number.def(1),
|
2020-06-18 10:51:56 +00:00
|
|
|
mode: PropTypes.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']),
|
2020-10-10 10:16:28 +00:00
|
|
|
multiple: PropTypes.looseBool,
|
2018-02-28 11:07:04 +00:00
|
|
|
value: PropTypes.any,
|
2018-09-05 13:28:54 +00:00
|
|
|
manualRef: PropTypes.func.def(noop),
|
|
|
|
role: PropTypes.any,
|
|
|
|
subMenuKey: PropTypes.string,
|
2018-11-05 13:03:25 +00:00
|
|
|
itemIcon: PropTypes.any,
|
2020-11-23 06:21:18 +00:00
|
|
|
parentUniKeys: PropTypes.array.def(() => []),
|
|
|
|
parentUniKey: PropTypes.string,
|
2018-01-17 08:12:53 +00:00
|
|
|
// clearSubMenuTimers: PropTypes.func.def(noop),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-11-23 06:21:18 +00:00
|
|
|
let indexGuid = 0;
|
|
|
|
const MenuItem = defineComponent({
|
2020-06-18 10:51:56 +00:00
|
|
|
name: 'AMenuItem',
|
2020-11-23 06:21:18 +00:00
|
|
|
mixins: [BaseMixin],
|
2020-06-16 14:55:02 +00:00
|
|
|
inheritAttrs: false,
|
2018-01-16 07:41:00 +00:00
|
|
|
props,
|
2018-01-04 11:06:54 +00:00
|
|
|
isMenuItem: true,
|
2020-11-23 06:21:18 +00:00
|
|
|
setup(props) {
|
|
|
|
const uniKey = `menu_item_${++indexGuid}`;
|
|
|
|
const store = inject('menuStore', () => ({}));
|
2021-02-06 05:47:44 +00:00
|
|
|
const isSelected = computed(
|
|
|
|
() => store.selectedKeys && store.selectedKeys.indexOf(props.eventKey) !== -1,
|
|
|
|
);
|
2020-11-23 06:21:18 +00:00
|
|
|
onMounted(() => {
|
|
|
|
store.addChildrenInfo(
|
|
|
|
uniKey,
|
|
|
|
computed(() => ({
|
|
|
|
parentUniKeys: props.parentUniKeys,
|
|
|
|
parentUniKey: props.parentUniKey,
|
|
|
|
eventKey: props.eventKey,
|
|
|
|
isSelected: isSelected.value,
|
|
|
|
disabled: props.disabled,
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
store.removeChildrenInfo(uniKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
parentMenu: inject('parentMenu', undefined),
|
|
|
|
isSelected,
|
|
|
|
};
|
2020-07-30 07:41:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
created() {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.prevActive = this.active;
|
2018-09-05 13:28:54 +00:00
|
|
|
// invoke customized ref to expose component to mixin
|
2019-01-12 03:33:27 +00:00
|
|
|
this.callRef();
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
updated() {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.$nextTick(() => {
|
2020-07-30 07:41:54 +00:00
|
|
|
const { active, parentMenu, eventKey } = this;
|
2020-03-07 11:45:13 +00:00
|
|
|
if (!this.prevActive && active && (!parentMenu || !parentMenu[`scrolled-${eventKey}`])) {
|
2020-11-23 06:21:18 +00:00
|
|
|
scrollIntoView(findDOMNode(this.node), parentMenu.getEl(), {
|
2018-09-05 13:28:54 +00:00
|
|
|
onlyScrollIfNeeded: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
parentMenu[`scrolled-${eventKey}`] = true;
|
|
|
|
} else if (parentMenu && parentMenu[`scrolled-${eventKey}`]) {
|
|
|
|
delete parentMenu[`scrolled-${eventKey}`];
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
this.prevActive = active;
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
this.callRef();
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2017-12-12 05:49:02 +00:00
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
onKeyDown(e) {
|
|
|
|
const keyCode = e.keyCode;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (keyCode === KeyCode.ENTER) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.onClick(e);
|
|
|
|
return true;
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
onMouseLeave(e) {
|
|
|
|
const { eventKey } = this.$props;
|
2018-01-12 08:10:41 +00:00
|
|
|
this.__emit('itemHover', {
|
2017-12-12 05:49:02 +00:00
|
|
|
key: eventKey,
|
|
|
|
hover: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-01-12 08:10:41 +00:00
|
|
|
this.__emit('mouseleave', {
|
2017-12-12 05:49:02 +00:00
|
|
|
key: eventKey,
|
|
|
|
domEvent: e,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
onMouseEnter(e) {
|
|
|
|
const { eventKey } = this;
|
2018-01-12 08:10:41 +00:00
|
|
|
this.__emit('itemHover', {
|
2017-12-12 05:49:02 +00:00
|
|
|
key: eventKey,
|
|
|
|
hover: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-01-12 08:10:41 +00:00
|
|
|
this.__emit('mouseenter', {
|
2017-12-12 05:49:02 +00:00
|
|
|
key: eventKey,
|
|
|
|
domEvent: e,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
onClick(e) {
|
2020-11-23 06:21:18 +00:00
|
|
|
const { eventKey, multiple } = this.$props;
|
|
|
|
const { isSelected } = this;
|
2017-12-12 05:49:02 +00:00
|
|
|
const info = {
|
|
|
|
key: eventKey,
|
|
|
|
keyPath: [eventKey],
|
2020-12-08 03:50:52 +00:00
|
|
|
item: { ...toRaw(this.$props), isSelected },
|
2017-12-12 05:49:02 +00:00
|
|
|
domEvent: e,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-01-17 08:12:53 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
this.__emit('click', info);
|
2017-12-12 05:49:02 +00:00
|
|
|
if (multiple) {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (isSelected) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.__emit('deselect', info);
|
2017-12-12 05:49:02 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.__emit('select', info);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
} else if (!isSelected) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.__emit('select', info);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getPrefixCls() {
|
|
|
|
return `${this.$props.rootPrefixCls}-item`;
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getActiveClassName() {
|
|
|
|
return `${this.getPrefixCls()}-active`;
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getSelectedClassName() {
|
|
|
|
return `${this.getPrefixCls()}-selected`;
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getDisabledClassName() {
|
|
|
|
return `${this.getPrefixCls()}-disabled`;
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2020-07-30 07:41:54 +00:00
|
|
|
saveNode(node) {
|
|
|
|
this.node = node;
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
callRef() {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (this.manualRef) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.manualRef(this);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-06-18 15:50:02 +00:00
|
|
|
const { class: cls, style, ...props } = { ...this.$props, ...this.$attrs };
|
2020-06-16 14:55:02 +00:00
|
|
|
|
2017-12-12 05:49:02 +00:00
|
|
|
const className = {
|
2020-06-18 15:50:02 +00:00
|
|
|
[cls]: !!cls,
|
2017-12-12 05:49:02 +00:00
|
|
|
[this.getPrefixCls()]: true,
|
2020-11-23 06:21:18 +00:00
|
|
|
[this.getActiveClassName()]: !props.disabled && this.active,
|
|
|
|
[this.getSelectedClassName()]: this.isSelected,
|
2017-12-12 05:49:02 +00:00
|
|
|
[this.getDisabledClassName()]: props.disabled,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
let attrs = {
|
2017-12-12 05:49:02 +00:00
|
|
|
...props.attribute,
|
|
|
|
title: props.title,
|
2018-11-05 13:03:25 +00:00
|
|
|
role: props.role || 'menuitem',
|
2017-12-12 05:49:02 +00:00
|
|
|
'aria-disabled': props.disabled,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
if (props.role === 'option') {
|
|
|
|
// overwrite to option
|
|
|
|
attrs = {
|
|
|
|
...attrs,
|
|
|
|
role: 'option',
|
2020-11-23 06:21:18 +00:00
|
|
|
'aria-selected': this.isSelected,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-11-05 13:03:25 +00:00
|
|
|
} else if (props.role === null || props.role === 'none') {
|
2018-09-05 13:28:54 +00:00
|
|
|
// sometimes we want to specify role inside <li/> element
|
|
|
|
// <li><a role='menuitem'>Link</a></li> would be a good example
|
2018-11-05 13:03:25 +00:00
|
|
|
// 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
|
2019-01-12 03:33:27 +00:00
|
|
|
attrs.role = 'none';
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
// In case that onClick/onMouseLeave/onMouseEnter is passed down from owner
|
|
|
|
const mouseEvent = {
|
2020-06-16 14:55:02 +00:00
|
|
|
onClick: props.disabled ? noop : this.onClick,
|
|
|
|
onMouseleave: props.disabled ? noop : this.onMouseLeave,
|
|
|
|
onMouseenter: props.disabled ? noop : this.onMouseEnter,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-01-08 10:31:04 +00:00
|
|
|
|
2020-06-18 15:50:02 +00:00
|
|
|
const styles = { ...(style || {}) };
|
2017-12-12 05:49:02 +00:00
|
|
|
if (props.mode === 'inline') {
|
2020-06-18 15:50:02 +00:00
|
|
|
styles.paddingLeft = `${props.inlineIndent * props.level}px`;
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2020-06-17 16:19:14 +00:00
|
|
|
menuAllProps.forEach(key => delete props[key]);
|
2017-12-12 05:49:02 +00:00
|
|
|
const liProps = {
|
2020-06-16 14:55:02 +00:00
|
|
|
...props,
|
|
|
|
...attrs,
|
|
|
|
...mouseEvent,
|
2020-07-30 07:41:54 +00:00
|
|
|
ref: this.saveNode,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2017-12-12 05:49:02 +00:00
|
|
|
return (
|
2020-06-18 15:50:02 +00:00
|
|
|
<li {...liProps} style={styles} class={className}>
|
2020-06-16 14:55:02 +00:00
|
|
|
{getSlot(this)}
|
|
|
|
{getComponent(this, 'itemIcon', props)}
|
2017-12-12 05:49:02 +00:00
|
|
|
</li>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2020-11-23 06:21:18 +00:00
|
|
|
});
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2020-11-23 06:21:18 +00:00
|
|
|
export default InjectExtraProps(MenuItem);
|
2019-01-12 03:33:27 +00:00
|
|
|
export { props as menuItemProps };
|