Skip to content

Commit

Permalink
fix: menu overflowed error #3262
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjinzhou committed Nov 26, 2020
1 parent f5cf7e0 commit 8c94b78
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion components/menu/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ describe('Menu', () => {
}, 500);
});

fit('horizontal', async () => {
it('horizontal', async () => {
const wrapper = mount(
{
render() {
Expand Down
30 changes: 23 additions & 7 deletions components/vc-menu/DOMWrap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import SubMenu from './SubMenu';
import BaseMixin from '../_util/BaseMixin';
import { getWidth, setStyle, menuAllProps } from './util';
import { cloneElement } from '../_util/vnode';
import { getPropsData, getAllProps, getSlot, findDOMNode } from '../_util/props-util';
import { getAllProps, getSlot, findDOMNode } from '../_util/props-util';

const MENUITEM_OVERFLOWED_CLASSNAME = 'menuitem-overflowed';
const FLOAT_PRECISION_ADJUST = 0.5;
const MENUITEM_OVERFLOWED_UNI_KEY = 'MENUITEM_OVERFLOWED_UNI_KEY';
const MENUITEM_OVERFLOWED_UNI_KEYS = [MENUITEM_OVERFLOWED_UNI_KEY];

const DOMWrap = {
name: 'DOMWrap',
Expand Down Expand Up @@ -136,6 +138,7 @@ const DOMWrap = {
class: `${prefixCls}-overflowed-submenu`,
key,
style,
isOverflowedSubMenu: true,
};
return <SubMenu {...subMenuProps}>{overflowedItems}</SubMenu>;
},
Expand Down Expand Up @@ -227,7 +230,8 @@ const DOMWrap = {
const className = this.$attrs.class || '';
return (children || []).reduce((acc, childNode, index) => {
let item = childNode;
const eventKey = getPropsData(childNode).eventKey;
const { extraProps = {} } = item.props || {};
const { eventKey } = extraProps;
if (this.mode === 'horizontal') {
let overflowed = this.getOverflowedSubMenuItem(eventKey, []);
if (
Expand All @@ -239,21 +243,33 @@ const DOMWrap = {
childNode,
// 这里修改 eventKey 是为了防止隐藏状态下还会触发 openkeys 事件
{
style: { display: 'none' },
eventKey: `${eventKey}-hidden`,
class: MENUITEM_OVERFLOWED_CLASSNAME,
extraProps: {
...extraProps,
style: { display: 'none' },
eventKey: `${eventKey}-hidden`,
class: MENUITEM_OVERFLOWED_CLASSNAME,
parentUniKey: MENUITEM_OVERFLOWED_UNI_KEY,
parentUniKeys: MENUITEM_OVERFLOWED_UNI_KEYS,
},
},
);
}
if (index === lastVisibleIndex + 1) {
this.overflowedItems = children.slice(lastVisibleIndex + 1).map(c => {
const { extraProps = {} } = c.props || {};
const { eventKey } = extraProps;
return cloneElement(
c,
// children[index].key will become '.$key' in clone by default,
// we have to overwrite with the correct key explicitly
{
key: getPropsData(c).eventKey,
mode: 'vertical-left',
extraProps: {
...extraProps,
key: eventKey,
mode: 'vertical-left',
parentUniKey: MENUITEM_OVERFLOWED_UNI_KEY,
parentUniKeys: MENUITEM_OVERFLOWED_UNI_KEYS,
},
},
);
});
Expand Down
1 change: 1 addition & 0 deletions components/vc-menu/FunctionProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { propTypes } from '../vc-progress/src/types';
export const injectExtraPropsKey = Symbol();
const FunctionProvider = {
inheritAttrs: false,
isMenuProvider: true,
props: {
extraProps: propTypes.object,
},
Expand Down
5 changes: 4 additions & 1 deletion components/vc-menu/Menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
toRaw,
watch,
} from 'vue';
import { isEqual } from 'lodash-es';

const Menu = {
name: 'Menu',
Expand Down Expand Up @@ -62,7 +63,9 @@ const Menu = {
.reduce((allKeys, { parentUniKeys = [] }) => {
return [...allKeys, ...toRaw(parentUniKeys)];
}, []);
selectedParentUniKeys.value = keys || [];
if (!isEqual(selectedParentUniKeys.value, keys)) {
selectedParentUniKeys.value = keys || [];
}
});
const store = reactive({
selectedKeys,
Expand Down
6 changes: 4 additions & 2 deletions components/vc-menu/SubMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ const SubMenu = defineComponent({
theme: PropTypes.string,
parentUniKeys: PropTypes.array.def(() => []),
parentUniKey: PropTypes.string,
isOverflowedSubMenu: PropTypes.looseBool.def(false),
},

isSubMenu: true,
setup(props) {
const uniKey = `sub_menu_${++indexGuid}`;
const uniKey = props.isOverflowedSubMenu
? 'MENUITEM_OVERFLOWED_UNI_KEY'
: `sub_menu_${++indexGuid}`;
const store = inject('menuStore', () => ({}));
onMounted(() => {
store.addChildrenInfo(
Expand Down Expand Up @@ -439,7 +442,6 @@ const SubMenu = defineComponent({
[this.getDisabledClassName()]: props.disabled,
[this.getSelectedClassName()]: this.isChildrenSelected || this.isChildrenSelected2(),
};

if (!this.internalMenuId) {
if (props.eventKey) {
this.internalMenuId = `${props.eventKey}$Menu`;
Expand Down
7 changes: 6 additions & 1 deletion components/vc-menu/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ export function loopMenuItemRecursively(children, keys, ret) {
if (construct && isObject(construct)) {
if (
!construct ||
!(construct.isSubMenu || construct.isMenuItem || construct.isMenuItemGroup)
!(
construct.isSubMenu ||
construct.isMenuItem ||
construct.isMenuItemGroup ||
construct.isMenuProvider
)
) {
return;
}
Expand Down

0 comments on commit 8c94b78

Please sign in to comment.