Skip to content

Commit

Permalink
fix: show the SubMenu in right place when the Menu is horizontal mode (
Browse files Browse the repository at this point in the history
…#526)

* fix: show the submenu in right place when horizontal mode

* test: update the snap of Menu
  • Loading branch information
wewoor authored Nov 30, 2021
1 parent 8fc4e62 commit 0d76520
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
>
<li
className="mo-menu__item"
data-mode="vertical"
data-submenu={true}
id="File"
>
Expand Down Expand Up @@ -76,6 +77,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
</li>
<li
className="mo-menu__item"
data-mode="vertical"
data-submenu={true}
id="Edit"
>
Expand Down Expand Up @@ -137,6 +139,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
</li>
<li
className="mo-menu__item"
data-mode="vertical"
data-submenu={true}
id="Selection"
>
Expand Down Expand Up @@ -198,6 +201,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
</li>
<li
className="mo-menu__item"
data-mode="vertical"
data-submenu={true}
id="View"
>
Expand Down Expand Up @@ -257,6 +261,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
</li>
<li
className="mo-menu__item"
data-mode="vertical"
data-submenu={true}
id="Appearance"
>
Expand Down Expand Up @@ -364,6 +369,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
</li>
<li
className="mo-menu__item"
data-mode="vertical"
data-submenu={true}
id="Run"
>
Expand Down Expand Up @@ -409,6 +415,7 @@ exports[`Test the Menu Component Match the List snapshot 1`] = `
</li>
<li
className="mo-menu__item"
data-mode="vertical"
data-submenu={true}
id="Help"
>
Expand Down
28 changes: 18 additions & 10 deletions src/components/menu/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import React, { useEffect, useCallback, useRef } from 'react';
import { classNames } from 'mo/common/className';
import { MenuItem } from './menuItem';
import { isHorizontal, ISubMenuProps, MenuMode, SubMenu } from './subMenu';
import { debounce } from 'lodash';
import { mergeFunctions } from 'mo/common/utils';
import { cloneReactChildren } from 'mo/react';
import { em2Px } from 'mo/common/css';
import { getRelativePosition, triggerEvent } from 'mo/common/dom';

import {
activeClassName,
defaultMenuClassName,
defaultSubMenuClassName,
horizontalMenuClassName,
verticalMenuClassName,
} from './base';
import { mergeFunctions } from 'mo/common/utils';
import { cloneReactChildren } from 'mo/react';
import { em2Px } from 'mo/common/css';
import { getRelativePosition, triggerEvent } from 'mo/common/dom';
import { Divider } from './divider';
import { MenuItem } from './menuItem';
import { isHorizontal, ISubMenuProps, MenuMode, SubMenu } from './subMenu';

export type IMenuProps = ISubMenuProps;

Expand Down Expand Up @@ -64,13 +65,14 @@ export function Menu(props: React.PropsWithChildren<IMenuProps>) {
onClick,
trigger = 'hover',
title,
...custom
...restProps
} = props;
const menuRef = useRef<HTMLUListElement>(null);
const isMouseInMenu = useRef(false);
let content = cloneReactChildren(children, { onClick });
// Only when the trigger is hover need to set the delay
const delay = trigger === 'hover' ? 200 : 0;

const modeClassName =
mode === MenuMode.Horizontal
? horizontalMenuClassName
Expand All @@ -81,12 +83,13 @@ export function Menu(props: React.PropsWithChildren<IMenuProps>) {
const renderMenusByData = (menus: IMenuProps[]) => {
return menus.map((item: IMenuProps) => {
if (item.type === 'divider') return <Divider />;

const handleClick = mergeFunctions(onClick, item.onClick);
if (item.data && item.data.length > 0) {
return (
<SubMenu
key={item.id}
mode={mode}
mode={item.mode || mode}
{...item}
onClick={handleClick}
>
Expand Down Expand Up @@ -140,7 +143,12 @@ export function Menu(props: React.PropsWithChildren<IMenuProps>) {
}
visibleMenuItem(liDom);
const subMenu = liDom?.querySelector('ul') || undefined;
setPositionForSubMenu(liDom, subMenu, isHorizontal(mode));
const subMenuMode = liDom?.dataset.mode || mode;
setPositionForSubMenu(
liDom,
subMenu,
isHorizontal(subMenuMode as MenuMode)
);
}
}, delay);

Expand Down Expand Up @@ -199,7 +207,7 @@ export function Menu(props: React.PropsWithChildren<IMenuProps>) {
// prevent JSX render in HTMLElement
{...(typeof title === 'string' ? { title } : {})}
{...getEventListener()}
{...custom}
{...restProps}
>
{content}
</ul>
Expand Down
11 changes: 7 additions & 4 deletions src/components/menu/menuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { classNames } from 'mo/common/className';
import { Icon } from '../icon';
import type { HTMLElementProps, UniqueId } from 'mo/common/types';

import {
checkClassName,
disabledClassName,
Expand All @@ -9,7 +10,7 @@ import {
labelClassName,
menuContentClassName,
} from './base';
import type { HTMLElementProps, UniqueId } from 'mo/common/types';
import { Icon } from '../icon';

export interface IMenuItemProps extends HTMLElementProps {
id: UniqueId;
Expand Down Expand Up @@ -52,7 +53,8 @@ export function MenuItem(
name,
title,
id,
...custom
sortIndex,
...restProps
} = props;

const events = {
Expand All @@ -69,10 +71,11 @@ export function MenuItem(
disabled ? disabledClassName : null
)}
id={id?.toString()}
data-sort={sortIndex}
// prevent render JSX title in HTMLElement
{...(typeof title === 'string' ? { title } : {})}
{...events}
{...custom}
{...restProps}
>
<div className={menuContentClassName}>
<Icon type={icon} className={checkClassName} />
Expand Down
9 changes: 6 additions & 3 deletions src/components/menu/subMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export function SubMenu(props: React.PropsWithChildren<ISubMenuProps>) {
children,
onClick,
title,
...custom
sortIndex,
...restProps
} = props;
const cNames = classNames(defaultSubMenuClassName, className);
const isAlignHorizontal = isHorizontal(mode);
Expand All @@ -63,7 +64,7 @@ export function SubMenu(props: React.PropsWithChildren<ISubMenuProps>) {
style={{ opacity: '0', pointerEvents: 'none' }}
data={data}
onClick={onClick}
{...custom}
{...restProps}
/>
) : (
<Menu
Expand All @@ -82,9 +83,11 @@ export function SubMenu(props: React.PropsWithChildren<ISubMenuProps>) {
disabled ? disabledClassName : null
)}
data-submenu
data-mode={mode}
data-sort={sortIndex}
// prevent render JSX title in HTMLElement
{...(typeof title === 'string' ? { title } : {})}
{...custom}
{...restProps}
>
<div className={menuContentClassName}>
{typeof icon === 'string' ? (
Expand Down

0 comments on commit 0d76520

Please sign in to comment.