Skip to content

Commit 0d76520

Browse files
authored
fix: show the SubMenu in right place when the Menu is horizontal mode (#526)
* fix: show the submenu in right place when horizontal mode * test: update the snap of Menu
1 parent 8fc4e62 commit 0d76520

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

src/components/menu/__tests__/__snapshots__/menu.test.tsx.snap

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
1515
>
1616
<li
1717
className="mo-menu__item"
18+
data-mode="vertical"
1819
data-submenu={true}
1920
id="File"
2021
>
@@ -76,6 +77,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
7677
</li>
7778
<li
7879
className="mo-menu__item"
80+
data-mode="vertical"
7981
data-submenu={true}
8082
id="Edit"
8183
>
@@ -137,6 +139,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
137139
</li>
138140
<li
139141
className="mo-menu__item"
142+
data-mode="vertical"
140143
data-submenu={true}
141144
id="Selection"
142145
>
@@ -198,6 +201,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
198201
</li>
199202
<li
200203
className="mo-menu__item"
204+
data-mode="vertical"
201205
data-submenu={true}
202206
id="View"
203207
>
@@ -257,6 +261,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
257261
</li>
258262
<li
259263
className="mo-menu__item"
264+
data-mode="vertical"
260265
data-submenu={true}
261266
id="Appearance"
262267
>
@@ -364,6 +369,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
364369
</li>
365370
<li
366371
className="mo-menu__item"
372+
data-mode="vertical"
367373
data-submenu={true}
368374
id="Run"
369375
>
@@ -409,6 +415,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
409415
</li>
410416
<li
411417
className="mo-menu__item"
418+
data-mode="vertical"
412419
data-submenu={true}
413420
id="Help"
414421
>

src/components/menu/menu.tsx

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import React, { useEffect, useCallback, useRef } from 'react';
22
import { classNames } from 'mo/common/className';
3-
import { MenuItem } from './menuItem';
4-
import { isHorizontal, ISubMenuProps, MenuMode, SubMenu } from './subMenu';
53
import { debounce } from 'lodash';
4+
import { mergeFunctions } from 'mo/common/utils';
5+
import { cloneReactChildren } from 'mo/react';
6+
import { em2Px } from 'mo/common/css';
7+
import { getRelativePosition, triggerEvent } from 'mo/common/dom';
8+
69
import {
710
activeClassName,
811
defaultMenuClassName,
912
defaultSubMenuClassName,
1013
horizontalMenuClassName,
1114
verticalMenuClassName,
1215
} from './base';
13-
import { mergeFunctions } from 'mo/common/utils';
14-
import { cloneReactChildren } from 'mo/react';
15-
import { em2Px } from 'mo/common/css';
16-
import { getRelativePosition, triggerEvent } from 'mo/common/dom';
1716
import { Divider } from './divider';
17+
import { MenuItem } from './menuItem';
18+
import { isHorizontal, ISubMenuProps, MenuMode, SubMenu } from './subMenu';
1819

1920
export type IMenuProps = ISubMenuProps;
2021

@@ -64,13 +65,14 @@ export function Menu(props: React.PropsWithChildren<IMenuProps>) {
6465
onClick,
6566
trigger = 'hover',
6667
title,
67-
...custom
68+
...restProps
6869
} = props;
6970
const menuRef = useRef<HTMLUListElement>(null);
7071
const isMouseInMenu = useRef(false);
7172
let content = cloneReactChildren(children, { onClick });
7273
// Only when the trigger is hover need to set the delay
7374
const delay = trigger === 'hover' ? 200 : 0;
75+
7476
const modeClassName =
7577
mode === MenuMode.Horizontal
7678
? horizontalMenuClassName
@@ -81,12 +83,13 @@ export function Menu(props: React.PropsWithChildren<IMenuProps>) {
8183
const renderMenusByData = (menus: IMenuProps[]) => {
8284
return menus.map((item: IMenuProps) => {
8385
if (item.type === 'divider') return <Divider />;
86+
8487
const handleClick = mergeFunctions(onClick, item.onClick);
8588
if (item.data && item.data.length > 0) {
8689
return (
8790
<SubMenu
8891
key={item.id}
89-
mode={mode}
92+
mode={item.mode || mode}
9093
{...item}
9194
onClick={handleClick}
9295
>
@@ -140,7 +143,12 @@ export function Menu(props: React.PropsWithChildren<IMenuProps>) {
140143
}
141144
visibleMenuItem(liDom);
142145
const subMenu = liDom?.querySelector('ul') || undefined;
143-
setPositionForSubMenu(liDom, subMenu, isHorizontal(mode));
146+
const subMenuMode = liDom?.dataset.mode || mode;
147+
setPositionForSubMenu(
148+
liDom,
149+
subMenu,
150+
isHorizontal(subMenuMode as MenuMode)
151+
);
144152
}
145153
}, delay);
146154

@@ -199,7 +207,7 @@ export function Menu(props: React.PropsWithChildren<IMenuProps>) {
199207
// prevent JSX render in HTMLElement
200208
{...(typeof title === 'string' ? { title } : {})}
201209
{...getEventListener()}
202-
{...custom}
210+
{...restProps}
203211
>
204212
{content}
205213
</ul>

src/components/menu/menuItem.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { classNames } from 'mo/common/className';
3-
import { Icon } from '../icon';
3+
import type { HTMLElementProps, UniqueId } from 'mo/common/types';
4+
45
import {
56
checkClassName,
67
disabledClassName,
@@ -9,7 +10,7 @@ import {
910
labelClassName,
1011
menuContentClassName,
1112
} from './base';
12-
import type { HTMLElementProps, UniqueId } from 'mo/common/types';
13+
import { Icon } from '../icon';
1314

1415
export interface IMenuItemProps extends HTMLElementProps {
1516
id: UniqueId;
@@ -52,7 +53,8 @@ export function MenuItem(
5253
name,
5354
title,
5455
id,
55-
...custom
56+
sortIndex,
57+
...restProps
5658
} = props;
5759

5860
const events = {
@@ -69,10 +71,11 @@ export function MenuItem(
6971
disabled ? disabledClassName : null
7072
)}
7173
id={id?.toString()}
74+
data-sort={sortIndex}
7275
// prevent render JSX title in HTMLElement
7376
{...(typeof title === 'string' ? { title } : {})}
7477
{...events}
75-
{...custom}
78+
{...restProps}
7679
>
7780
<div className={menuContentClassName}>
7881
<Icon type={icon} className={checkClassName} />

src/components/menu/subMenu.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export function SubMenu(props: React.PropsWithChildren<ISubMenuProps>) {
5050
children,
5151
onClick,
5252
title,
53-
...custom
53+
sortIndex,
54+
...restProps
5455
} = props;
5556
const cNames = classNames(defaultSubMenuClassName, className);
5657
const isAlignHorizontal = isHorizontal(mode);
@@ -63,7 +64,7 @@ export function SubMenu(props: React.PropsWithChildren<ISubMenuProps>) {
6364
style={{ opacity: '0', pointerEvents: 'none' }}
6465
data={data}
6566
onClick={onClick}
66-
{...custom}
67+
{...restProps}
6768
/>
6869
) : (
6970
<Menu
@@ -82,9 +83,11 @@ export function SubMenu(props: React.PropsWithChildren<ISubMenuProps>) {
8283
disabled ? disabledClassName : null
8384
)}
8485
data-submenu
86+
data-mode={mode}
87+
data-sort={sortIndex}
8588
// prevent render JSX title in HTMLElement
8689
{...(typeof title === 'string' ? { title } : {})}
87-
{...custom}
90+
{...restProps}
8891
>
8992
<div className={menuContentClassName}>
9093
{typeof icon === 'string' ? (

0 commit comments

Comments
 (0)