2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { Provider, create } from '../_util/store';
|
|
|
|
import { default as SubPopupMenu, getActiveKey } from './SubPopupMenu';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-06-16 14:55:02 +00:00
|
|
|
import hasProp, { getOptionProps, getComponent, filterEmpty } from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
import commonPropsType from './commonPropsType';
|
2020-10-12 05:27:16 +00:00
|
|
|
import { defineComponent, provide } from 'vue';
|
2017-12-12 05:49:02 +00:00
|
|
|
|
|
|
|
const Menu = {
|
|
|
|
name: 'Menu',
|
2020-06-16 14:55:02 +00:00
|
|
|
inheritAttrs: false,
|
2017-12-12 05:49:02 +00:00
|
|
|
props: {
|
2018-01-04 11:06:54 +00:00
|
|
|
...commonPropsType,
|
2020-10-10 10:16:28 +00:00
|
|
|
selectable: PropTypes.looseBool.def(true),
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
mixins: [BaseMixin],
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
const props = getOptionProps(this);
|
|
|
|
let selectedKeys = props.defaultSelectedKeys;
|
|
|
|
let openKeys = props.defaultOpenKeys;
|
2018-09-05 13:28:54 +00:00
|
|
|
if ('selectedKeys' in props) {
|
2019-01-12 03:33:27 +00:00
|
|
|
selectedKeys = props.selectedKeys || [];
|
2018-01-08 10:31:04 +00:00
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
if ('openKeys' in props) {
|
2019-01-12 03:33:27 +00:00
|
|
|
openKeys = props.openKeys || [];
|
2018-01-08 10:31:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
this.store = create({
|
|
|
|
selectedKeys,
|
|
|
|
openKeys,
|
2019-01-12 03:33:27 +00:00
|
|
|
activeKey: {
|
2020-06-16 14:55:02 +00:00
|
|
|
'0-menu-': getActiveKey({ ...props, children: props.children || [] }, props.activeKey),
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
// this.isRootMenu = true // 声明在props上
|
2019-01-12 03:33:27 +00:00
|
|
|
return {};
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2020-07-30 07:41:54 +00:00
|
|
|
created() {
|
|
|
|
provide('parentMenu', this);
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
|
|
|
this.updateMiniStore();
|
2018-11-05 13:03:25 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
updated() {
|
|
|
|
this.updateMiniStore();
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
onSelect(selectInfo) {
|
|
|
|
const props = this.$props;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (props.selectable) {
|
2019-01-12 03:33:27 +00:00
|
|
|
// root menu
|
|
|
|
let selectedKeys = this.store.getState().selectedKeys;
|
|
|
|
const selectedKey = selectInfo.key;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (props.multiple) {
|
2019-01-12 03:33:27 +00:00
|
|
|
selectedKeys = selectedKeys.concat([selectedKey]);
|
2017-12-12 05:49:02 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
selectedKeys = [selectedKey];
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-01-02 11:05:02 +00:00
|
|
|
if (!hasProp(this, 'selectedKeys')) {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.store.setState({
|
|
|
|
selectedKeys,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-01-12 08:10:41 +00:00
|
|
|
this.__emit('select', {
|
2017-12-12 05:49:02 +00:00
|
|
|
...selectInfo,
|
2020-03-18 06:52:02 +00:00
|
|
|
selectedKeys,
|
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) {
|
|
|
|
this.__emit('click', e);
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
// onKeyDown needs to be exposed as a instance method
|
|
|
|
// e.g., in rc-select, we need to navigate menu item while
|
|
|
|
// current active item is rc-select input box rather than the menu itself
|
2019-01-12 03:33:27 +00:00
|
|
|
onKeyDown(e, callback) {
|
2020-07-15 10:22:32 +00:00
|
|
|
this.innerMenu.getWrappedInstance().onKeyDown(e, callback);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onOpenChange(event) {
|
|
|
|
const openKeys = this.store.getState().openKeys.concat();
|
|
|
|
let changed = false;
|
|
|
|
const processSingle = e => {
|
|
|
|
let oneChanged = false;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (e.open) {
|
2019-01-12 03:33:27 +00:00
|
|
|
oneChanged = openKeys.indexOf(e.key) === -1;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (oneChanged) {
|
2019-01-12 03:33:27 +00:00
|
|
|
openKeys.push(e.key);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
const index = openKeys.indexOf(e.key);
|
|
|
|
oneChanged = index !== -1;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (oneChanged) {
|
2019-01-12 03:33:27 +00:00
|
|
|
openKeys.splice(index, 1);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
changed = changed || oneChanged;
|
|
|
|
};
|
2018-01-17 08:12:53 +00:00
|
|
|
if (Array.isArray(event)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
// batch change call
|
|
|
|
event.forEach(processSingle);
|
2017-12-12 05:49:02 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
processSingle(event);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
if (changed) {
|
2018-01-02 11:05:02 +00:00
|
|
|
if (!hasProp(this, 'openKeys')) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.store.setState({ openKeys });
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.__emit('openChange', openKeys);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
onDeselect(selectInfo) {
|
|
|
|
const props = this.$props;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (props.selectable) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const selectedKeys = this.store.getState().selectedKeys.concat();
|
|
|
|
const selectedKey = selectInfo.key;
|
|
|
|
const index = selectedKeys.indexOf(selectedKey);
|
2017-12-12 05:49:02 +00:00
|
|
|
if (index !== -1) {
|
2019-01-12 03:33:27 +00:00
|
|
|
selectedKeys.splice(index, 1);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-01-02 11:05:02 +00:00
|
|
|
if (!hasProp(this, 'selectedKeys')) {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.store.setState({
|
|
|
|
selectedKeys,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-01-12 08:10:41 +00:00
|
|
|
this.__emit('deselect', {
|
2017-12-12 05:49:02 +00:00
|
|
|
...selectInfo,
|
2020-03-18 06:52:02 +00:00
|
|
|
selectedKeys,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getOpenTransitionName() {
|
|
|
|
const props = this.$props;
|
|
|
|
let transitionName = props.openTransitionName;
|
|
|
|
const animationName = props.openAnimation;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (!transitionName && typeof animationName === 'string') {
|
2019-01-12 03:33:27 +00:00
|
|
|
transitionName = `${props.prefixCls}-open-${animationName}`;
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return transitionName;
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
updateMiniStore() {
|
|
|
|
const props = getOptionProps(this);
|
2018-11-05 13:03:25 +00:00
|
|
|
if ('selectedKeys' in props) {
|
|
|
|
this.store.setState({
|
|
|
|
selectedKeys: props.selectedKeys || [],
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-11-05 13:03:25 +00:00
|
|
|
}
|
|
|
|
if ('openKeys' in props) {
|
|
|
|
this.store.setState({
|
|
|
|
openKeys: props.openKeys || [],
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-11-05 13:03:25 +00:00
|
|
|
}
|
|
|
|
},
|
2020-07-15 10:22:32 +00:00
|
|
|
saveInnerMenu(ref) {
|
|
|
|
this.innerMenu = ref;
|
|
|
|
},
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-06-16 14:55:02 +00:00
|
|
|
const props = { ...getOptionProps(this), ...this.$attrs };
|
2020-06-29 10:43:43 +00:00
|
|
|
props.class = props.class
|
|
|
|
? `${props.class} ${props.prefixCls}-root`
|
|
|
|
: `${props.prefixCls}-root`;
|
2018-09-05 13:28:54 +00:00
|
|
|
const subPopupMenuProps = {
|
2020-06-16 14:55:02 +00:00
|
|
|
...props,
|
|
|
|
itemIcon: getComponent(this, 'itemIcon', props),
|
|
|
|
expandIcon: getComponent(this, 'expandIcon', props),
|
|
|
|
overflowedIndicator: getComponent(this, 'overflowedIndicator', props) || <span>···</span>,
|
|
|
|
openTransitionName: this.getOpenTransitionName(),
|
|
|
|
children: filterEmpty(props.children),
|
|
|
|
onClick: this.onClick,
|
|
|
|
onOpenChange: this.onOpenChange,
|
|
|
|
onDeselect: this.onDeselect,
|
|
|
|
onSelect: this.onSelect,
|
2020-07-15 10:22:32 +00:00
|
|
|
ref: this.saveInnerMenu,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-06-27 05:27:36 +00:00
|
|
|
|
|
|
|
const subPopupMenu = <SubPopupMenu {...subPopupMenuProps} />;
|
|
|
|
return <Provider store={this.store}>{subPopupMenu}</Provider>;
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-06-27 05:27:36 +00:00
|
|
|
|
2020-10-12 05:27:16 +00:00
|
|
|
export default defineComponent(Menu);
|