fix: menu not close

pull/4134/head
tanjinzhou 4 years ago
parent 3825c6507f
commit a049a66ffe

@ -58,6 +58,7 @@ export const menuProps = {
export type MenuProps = Partial<ExtractPropTypes<typeof menuProps>>; export type MenuProps = Partial<ExtractPropTypes<typeof menuProps>>;
const EMPTY_LIST: string[] = [];
export default defineComponent({ export default defineComponent({
name: 'AMenu', name: 'AMenu',
props: menuProps, props: menuProps,
@ -169,10 +170,12 @@ export default defineComponent({
const exist = mergedSelectedKeys.value.includes(targetKey); const exist = mergedSelectedKeys.value.includes(targetKey);
let newSelectedKeys: Key[]; let newSelectedKeys: Key[];
if (exist && props.multiple) { if (props.multiple) {
newSelectedKeys = mergedSelectedKeys.value.filter(key => key !== targetKey); if (exist) {
} else if (props.multiple) { newSelectedKeys = mergedSelectedKeys.value.filter(key => key !== targetKey);
newSelectedKeys = [...mergedSelectedKeys.value, targetKey]; } else {
newSelectedKeys = [...mergedSelectedKeys.value, targetKey];
}
} else { } else {
newSelectedKeys = [targetKey]; newSelectedKeys = [targetKey];
} }
@ -193,6 +196,10 @@ export default defineComponent({
emit('select', selectInfo); emit('select', selectInfo);
} }
} }
if (mergedMode.value !== 'inline' && !props.multiple && mergedOpenKeys.value.length) {
triggerOpenKeys(EMPTY_LIST);
}
}; };
const mergedOpenKeys = ref([]); const mergedOpenKeys = ref([]);
@ -231,6 +238,12 @@ export default defineComponent({
const isInlineMode = computed(() => mergedMode.value === 'inline'); const isInlineMode = computed(() => mergedMode.value === 'inline');
const triggerOpenKeys = (keys: string[]) => {
mergedOpenKeys.value = keys;
emit('update:openKeys', keys);
emit('openChange', keys);
};
// >>>>> Cache & Reset open keys when inlineCollapsed changed // >>>>> Cache & Reset open keys when inlineCollapsed changed
const inlineCacheOpenKeys = ref(mergedOpenKeys.value); const inlineCacheOpenKeys = ref(mergedOpenKeys.value);
@ -259,11 +272,8 @@ export default defineComponent({
if (isInlineMode.value) { if (isInlineMode.value) {
mergedOpenKeys.value = inlineCacheOpenKeys.value; mergedOpenKeys.value = inlineCacheOpenKeys.value;
} else { } else {
const empty = [];
mergedOpenKeys.value = empty;
// Trigger open event in case its in control // Trigger open event in case its in control
emit('update:openKeys', empty); triggerOpenKeys(EMPTY_LIST);
emit('openChange', empty);
} }
}, },
{ immediate: true }, { immediate: true },
@ -319,9 +329,7 @@ export default defineComponent({
} }
if (!shallowEqual(mergedOpenKeys, newOpenKeys)) { if (!shallowEqual(mergedOpenKeys, newOpenKeys)) {
mergedOpenKeys.value = newOpenKeys; triggerOpenKeys(newOpenKeys);
emit('update:openKeys', newOpenKeys);
emit('openChange', newOpenKeys);
} }
}; };

Loading…
Cancel
Save