2019-01-12 11:33:27 +08: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 { connect } from '../_util/store';
|
|
|
|
import { noop, menuAllProps } from './util';
|
2020-01-19 16:58:38 +08:00
|
|
|
import { getComponentFromProp, getListeners } from '../_util/props-util';
|
2018-09-05 21:28:54 +08:00
|
|
|
|
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,
|
2019-01-05 11:24:25 +08:00
|
|
|
title: PropTypes.any,
|
2018-01-16 15:41:00 +08:00
|
|
|
index: PropTypes.number,
|
|
|
|
inlineIndent: PropTypes.number.def(24),
|
|
|
|
level: PropTypes.number.def(1),
|
2019-01-12 11:33:27 +08:00
|
|
|
mode: PropTypes.oneOf([
|
|
|
|
'horizontal',
|
|
|
|
'vertical',
|
|
|
|
'vertical-left',
|
|
|
|
'vertical-right',
|
|
|
|
'inline',
|
|
|
|
]).def('vertical'),
|
2018-01-16 15:41:00 +08:00
|
|
|
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-11-05 21:03:25 +08:00
|
|
|
itemIcon: PropTypes.any,
|
2018-01-17 16:12:53 +08:00
|
|
|
// clearSubMenuTimers: PropTypes.func.def(noop),
|
2019-01-12 11:33:27 +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,
|
2019-01-12 11:33:27 +08:00
|
|
|
created() {
|
2020-03-07 19:45:13 +08:00
|
|
|
this.prevActive = this.active;
|
2018-09-05 21:28:54 +08:00
|
|
|
// invoke customized ref to expose component to mixin
|
2019-01-12 11:33:27 +08:00
|
|
|
this.callRef();
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
updated() {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.$nextTick(() => {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { active, parentMenu, eventKey } = this.$props;
|
|
|
|
if (!this.prevActive && active && (!parentMenu || !parentMenu[`scrolled-${eventKey}`])) {
|
2018-09-05 21:28:54 +08:00
|
|
|
scrollIntoView(this.$el, this.parentMenu.$el, {
|
|
|
|
onlyScrollIfNeeded: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2020-03-07 19:45:13 +08:00
|
|
|
parentMenu[`scrolled-${eventKey}`] = true;
|
|
|
|
} else if (parentMenu && parentMenu[`scrolled-${eventKey}`]) {
|
|
|
|
delete parentMenu[`scrolled-${eventKey}`];
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
this.prevActive = active;
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
this.callRef();
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
beforeDestroy() {
|
|
|
|
const props = this.$props;
|
|
|
|
this.__emit('destroy', props.eventKey);
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
onKeyDown(e) {
|
|
|
|
const keyCode = e.keyCode;
|
2017-12-12 13:49:02 +08:00
|
|
|
if (keyCode === KeyCode.ENTER) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.onClick(e);
|
|
|
|
return true;
|
2017-12-12 13:49:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
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,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('mouseleave', {
|
2017-12-12 13:49:02 +08:00
|
|
|
key: eventKey,
|
|
|
|
domEvent: e,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
onMouseEnter(e) {
|
|
|
|
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,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-01-12 16:10:41 +08:00
|
|
|
this.__emit('mouseenter', {
|
2017-12-12 13:49:02 +08:00
|
|
|
key: eventKey,
|
|
|
|
domEvent: e,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
onClick(e) {
|
|
|
|
const { eventKey, multiple, isSelected } = this.$props;
|
2017-12-12 13:49:02 +08:00
|
|
|
const info = {
|
|
|
|
key: eventKey,
|
|
|
|
keyPath: [eventKey],
|
|
|
|
item: this,
|
|
|
|
domEvent: e,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-01-17 16:12:53 +08:00
|
|
|
|
2019-01-12 11:33:27 +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) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.__emit('deselect', info);
|
2017-12-12 13:49:02 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +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) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.__emit('select', info);
|
2017-12-12 13:49:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getPrefixCls() {
|
|
|
|
return `${this.$props.rootPrefixCls}-item`;
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getActiveClassName() {
|
|
|
|
return `${this.getPrefixCls()}-active`;
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getSelectedClassName() {
|
|
|
|
return `${this.getPrefixCls()}-selected`;
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getDisabledClassName() {
|
|
|
|
return `${this.getPrefixCls()}-disabled`;
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
callRef() {
|
2018-09-05 21:28:54 +08:00
|
|
|
if (this.manualRef) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.manualRef(this);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
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,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
let attrs = {
|
2017-12-12 13:49:02 +08:00
|
|
|
...props.attribute,
|
|
|
|
title: props.title,
|
2018-11-05 21:03:25 +08:00
|
|
|
role: props.role || 'menuitem',
|
2017-12-12 13:49:02 +08:00
|
|
|
'aria-disabled': props.disabled,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
if (props.role === 'option') {
|
|
|
|
// overwrite to option
|
|
|
|
attrs = {
|
|
|
|
...attrs,
|
|
|
|
role: 'option',
|
|
|
|
'aria-selected': props.isSelected,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-11-05 21:03:25 +08:00
|
|
|
} else if (props.role === null || props.role === 'none') {
|
2018-09-05 21:28:54 +08: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 21:03:25 +08: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 11:33:27 +08:00
|
|
|
attrs.role = 'none';
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
// 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,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-01-08 18:31:04 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const style = {};
|
2017-12-12 13:49:02 +08:00
|
|
|
if (props.mode === 'inline') {
|
2019-01-12 11:33:27 +08:00
|
|
|
style.paddingLeft = `${props.inlineIndent * props.level}px`;
|
2017-12-12 13:49:02 +08:00
|
|
|
}
|
2020-01-19 16:58:38 +08:00
|
|
|
const listeners = { ...getListeners(this) };
|
2019-01-12 11:33:27 +08:00
|
|
|
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
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2017-12-12 13:49:02 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<li {...liProps} style={style} class={className}>
|
2017-12-12 13:49:02 +08:00
|
|
|
{this.$slots.default}
|
2018-11-05 21:03:25 +08:00
|
|
|
{getComponentFromProp(this, 'itemIcon', props)}
|
2017-12-12 13:49:02 +08:00
|
|
|
</li>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2017-12-12 13:49:02 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2017-12-12 13:49:02 +08:00
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
const connected = connect(({ activeKey, selectedKeys }, { eventKey, subMenuKey }) => ({
|
|
|
|
active: activeKey[subMenuKey] === eventKey,
|
|
|
|
isSelected: selectedKeys.indexOf(eventKey) !== -1,
|
2019-01-12 11:33:27 +08:00
|
|
|
}))(MenuItem);
|
2018-03-19 10:16:27 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default connected;
|
|
|
|
export { props as menuItemProps };
|