ant-design-vue/components/vc-menu/Menu.jsx

215 lines
6.0 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../_util/vue-types';
2020-11-23 06:21:18 +00:00
import { default as SubPopupMenu } from './SubPopupMenu';
2019-01-12 03:33:27 +00:00
import BaseMixin from '../_util/BaseMixin';
2020-11-23 06:21:18 +00:00
import hasProp, { getOptionProps, getComponent } from '../_util/props-util';
2019-01-12 03:33:27 +00:00
import commonPropsType from './commonPropsType';
2020-11-23 06:21:18 +00:00
import {
computed,
defineComponent,
getCurrentInstance,
provide,
reactive,
ref,
toRaw,
watch,
} 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-23 06:29:39 +00:00
onClick: PropTypes.func,
selectable: PropTypes.looseBool.def(true),
2017-12-12 05:49:02 +00:00
},
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
2018-09-05 13:28:54 +00:00
mixins: [BaseMixin],
2020-11-23 06:21:18 +00:00
setup(props) {
const menuChildrenInfo = reactive({});
const selectedKeys = ref(props.selectedKeys || props.defaultSelectedKeys || []);
const openKeys = ref(props.openKeys || props.defaultOpenKeys || []);
// computed(() => {
// return props.openKeys || props.defaultOpenKeys || [];
// });
watch(
() => props.selectedKeys,
() => {
selectedKeys.value = props.selectedKeys;
},
);
watch(
() => props.openKeys,
() => {
openKeys.value = props.openKeys;
},
);
const activeKey = reactive({
'0-menu-': props.activeKey,
});
const defaultActiveFirst = reactive({});
const addChildrenInfo = (key, info) => {
menuChildrenInfo[key] = info;
};
const removeChildrenInfo = key => {
delete menuChildrenInfo[key];
};
const getActiveKey = key => {
return key;
}; // TODO
const selectedParentUniKeys = ref([]);
watch(menuChildrenInfo, () => {
const keys = Object.values(menuChildrenInfo)
.filter(info => info.isSelected)
.reduce((allKeys, { parentUniKeys = [] }) => {
return [...allKeys, ...toRaw(parentUniKeys)];
}, []);
selectedParentUniKeys.value = keys || [];
});
const store = reactive({
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
2018-09-05 13:28:54 +00:00
selectedKeys,
openKeys,
2020-11-23 06:21:18 +00:00
activeKey,
defaultActiveFirst,
menuChildrenInfo,
selectedParentUniKeys,
addChildrenInfo,
removeChildrenInfo,
getActiveKey,
2019-01-12 03:33:27 +00:00
});
2020-11-23 06:21:18 +00:00
const ins = getCurrentInstance();
const getEl = () => {
return ins.vnode.el;
};
provide('menuStore', store);
provide(
'parentMenu',
reactive({
isRootMenu: computed(() => props.isRootMenu),
getPopupContainer: computed(() => props.getPopupContainer),
getEl,
}),
);
return {
store,
};
2017-12-12 05:49:02 +00:00
},
methods: {
2020-10-24 14:38:25 +00:00
handleSelect(selectInfo) {
2019-01-12 03:33:27 +00:00
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
2020-11-23 06:21:18 +00:00
let selectedKeys = this.store.selectedKeys;
2019-01-12 03:33:27 +00:00
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')) {
2020-11-23 06:21:18 +00:00
this.store.selectedKeys = selectedKeys;
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,
selectedKeys,
2019-01-12 03:33:27 +00:00
});
2017-12-12 05:49:02 +00:00
}
},
2020-10-24 14:38:25 +00:00
handleClick(e) {
2019-01-12 03:33:27 +00:00
this.__emit('click', e);
2017-12-12 05:49:02 +00:00
},
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
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);
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
2018-09-05 13:28:54 +00:00
},
2019-01-12 03:33:27 +00:00
onOpenChange(event) {
2020-11-23 06:21:18 +00:00
const openKeys = this.store.openKeys.concat();
2019-01-12 03:33:27 +00:00
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')) {
2020-11-23 06:21:18 +00:00
this.store.openKeys = 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
}
},
2020-10-24 14:38:25 +00:00
handleDeselect(selectInfo) {
2019-01-12 03:33:27 +00:00
const props = this.$props;
2017-12-12 05:49:02 +00:00
if (props.selectable) {
2020-11-23 06:21:18 +00:00
const selectedKeys = this.store.selectedKeys.concat();
2019-01-12 03:33:27 +00:00
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')) {
2020-11-23 06:21:18 +00:00
this.store.selectedKeys = selectedKeys;
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,
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
},
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`;
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
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(),
2020-10-24 14:38:25 +00:00
onClick: this.handleClick,
2020-06-16 14:55:02 +00:00
onOpenChange: this.onOpenChange,
2020-10-24 14:38:25 +00:00
onDeselect: this.handleDeselect,
onSelect: this.handleSelect,
2020-07-15 10:22:32 +00:00
ref: this.saveInnerMenu,
2020-11-23 06:21:18 +00:00
store: this.store,
2019-01-12 03:33:27 +00:00
};
2020-11-23 06:21:18 +00:00
return <SubPopupMenu {...subPopupMenuProps} v-slots={this.$slots} />;
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);