Skip to content

Commit c867a82

Browse files
committed
Merge branch 'master' of https://github.com/react-component/menu
2 parents 32a6222 + 36fe210 commit c867a82

File tree

8 files changed

+40
-45
lines changed

8 files changed

+40
-45
lines changed

docs/examples/debug.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const motionMap: Record<MenuProps['mode'], CSSMotionProps> = {
4747
};
4848

4949
export default () => {
50-
const [mode, setMode] = React.useState<MenuProps['mode']>('horizontal');
50+
const [mode, setMode] = React.useState<MenuProps['mode']>('inline');
5151
const [narrow, setNarrow] = React.useState(false);
5252
const [inlineCollapsed, setInlineCollapsed] = React.useState(false);
5353
const [forceRender, setForceRender] = React.useState(false);
@@ -105,6 +105,7 @@ export default () => {
105105
<div style={{ width: narrow ? 350 : undefined }}>
106106
<Menu
107107
// direction="rtl"
108+
defaultOpenKeys={['sub', 'nest']}
108109
forceSubMenuRender={forceRender}
109110
mode={mode}
110111
style={{ width: mode === 'horizontal' ? undefined : 256 }}

src/Divider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import classNames from 'classnames';
33
import { MenuContext } from './context/MenuContext';
4-
import { useMeasure } from './context/MeasureContext';
4+
import { useMeasure } from './context/PathContext';
55

66
export interface DividerProps {
77
className?: string;

src/Menu.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { warnItemProp } from './utils/warnUtil';
2323
import SubMenu from './SubMenu';
2424
import useAccessibility from './hooks/useAccessibility';
2525
import useUUID from './hooks/useUUID';
26-
import { PathRegisterContext, PathUserContext } from './context/MeasureContext';
26+
import { PathRegisterContext, PathUserContext } from './context/PathContext';
2727
import useKeyRecords, { OVERFLOW_KEY } from './hooks/useKeyRecords';
2828
import { IdContext } from './context/IdContext';
2929

@@ -277,9 +277,8 @@ const Menu: React.FC<MenuProps> = props => {
277277
[registerPath, unregisterPath],
278278
);
279279

280-
const pathUserContext = React.useMemo(() => ({ isSubPathKey, getKeyPath }), [
280+
const pathUserContext = React.useMemo(() => ({ isSubPathKey }), [
281281
isSubPathKey,
282-
getKeyPath,
283282
]);
284283

285284
React.useEffect(() => {

src/MenuItem.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ import useActive from './hooks/useActive';
1515
import { warnItemProp } from './utils/warnUtil';
1616
import Icon from './Icon';
1717
import useDirectionStyle from './hooks/useDirectionStyle';
18-
import {
19-
PathUserContext,
20-
useKeyPath,
21-
useMeasure,
22-
} from './context/MeasureContext';
18+
import { useFullPath, useMeasure } from './context/PathContext';
2319
import { useMenuId } from './context/IdContext';
2420

2521
export interface MenuItemProps
@@ -122,8 +118,7 @@ const InternalMenuItem = (props: MenuItemProps) => {
122118
const elementRef = React.useRef<HTMLLIElement>();
123119
const mergedDisabled = contextDisabled || disabled;
124120

125-
const { getKeyPath } = React.useContext(PathUserContext);
126-
const connectedKeys = getKeyPath(eventKey);
121+
const connectedKeys = useFullPath(eventKey);
127122

128123
// ============================= Info =============================
129124
const getEventInfo = (
@@ -240,7 +235,7 @@ function MenuItem(props: MenuItemProps): React.ReactElement {
240235

241236
// ==================== Record KeyPath ====================
242237
const measure = useMeasure();
243-
const connectedKeyPath = useKeyPath(eventKey);
238+
const connectedKeyPath = useFullPath(eventKey);
244239

245240
// eslint-disable-next-line consistent-return
246241
React.useEffect(() => {

src/MenuItemGroup.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import * as React from 'react';
22
import classNames from 'classnames';
33
import { parseChildren } from './utils/nodeUtil';
44
import { MenuContext } from './context/MenuContext';
5-
import {
6-
useKeyPath,
7-
useMeasure,
8-
} from './context/MeasureContext';
5+
import { useFullPath, useMeasure } from './context/PathContext';
96

107
export interface MenuItemGroupProps {
118
className?: string;
@@ -48,7 +45,7 @@ export default function MenuItemGroup({
4845
children,
4946
...props
5047
}: MenuItemGroupProps): React.ReactElement {
51-
const connectedKeyPath = useKeyPath(props.eventKey);
48+
const connectedKeyPath = useFullPath(props.eventKey);
5249
const childList: React.ReactElement[] = parseChildren(
5350
children,
5451
connectedKeyPath,

src/SubMenu/index.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import { warnItemProp } from '../utils/warnUtil';
1919
import useDirectionStyle from '../hooks/useDirectionStyle';
2020
import InlineSubMenuList from './InlineSubMenuList';
2121
import {
22-
PathConnectContext,
22+
PathTrackerContext,
2323
PathUserContext,
24-
useKeyPath,
24+
useFullPath,
2525
useMeasure,
26-
} from '../context/MeasureContext';
26+
} from '../context/PathContext';
2727
import { useMenuId } from '../context/IdContext';
2828

2929
export interface SubMenuProps {
@@ -122,8 +122,8 @@ const InternalSubMenu = (props: SubMenuProps) => {
122122
onActive,
123123
} = React.useContext(MenuContext);
124124

125-
const { isSubPathKey, getKeyPath } = React.useContext(PathUserContext);
126-
const keyPath = getKeyPath(eventKey);
125+
const { isSubPathKey } = React.useContext(PathUserContext);
126+
const connectedPath = useFullPath();
127127

128128
const subMenuPrefixCls = `${prefixCls}-submenu`;
129129
const mergedDisabled = contextDisabled || disabled;
@@ -189,7 +189,7 @@ const InternalSubMenu = (props: SubMenuProps) => {
189189
}, [mode, active, activeKey, childrenActive, eventKey, isSubPathKey]);
190190

191191
// ========================== DirectionStyle ==========================
192-
const directionStyle = useDirectionStyle(keyPath.length);
192+
const directionStyle = useDirectionStyle(connectedPath.length);
193193

194194
// =============================== Events ===============================
195195
// >>>> Title click
@@ -319,7 +319,7 @@ const InternalSubMenu = (props: SubMenuProps) => {
319319

320320
{/* Inline mode */}
321321
{!overflowDisabled && (
322-
<InlineSubMenuList id={popupId} open={open} keyPath={keyPath}>
322+
<InlineSubMenuList id={popupId} open={open} keyPath={connectedPath}>
323323
{children}
324324
</InlineSubMenuList>
325325
)}
@@ -331,7 +331,7 @@ const InternalSubMenu = (props: SubMenuProps) => {
331331
export default function SubMenu(props: SubMenuProps) {
332332
const { eventKey, children } = props;
333333

334-
const connectedKeyPath = useKeyPath(eventKey);
334+
const connectedKeyPath = useFullPath(eventKey);
335335
const childList: React.ReactElement[] = parseChildren(
336336
children,
337337
connectedKeyPath,
@@ -351,15 +351,18 @@ export default function SubMenu(props: SubMenuProps) {
351351
}
352352
}, [connectedKeyPath]);
353353

354-
if (measure) {
355-
return (
356-
<PathConnectContext.Provider value={connectedKeyPath}>
357-
{childList}
358-
</PathConnectContext.Provider>
359-
);
360-
}
354+
let renderNode: React.ReactNode;
361355

362356
// ======================== Render ========================
357+
if (measure) {
358+
renderNode = childList;
359+
} else {
360+
renderNode = <InternalSubMenu {...props}>{childList}</InternalSubMenu>;
361+
}
363362

364-
return <InternalSubMenu {...props}>{childList}</InternalSubMenu>;
363+
return (
364+
<PathTrackerContext.Provider value={connectedKeyPath}>
365+
{renderNode}
366+
</PathTrackerContext.Provider>
367+
);
365368
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ export function useMeasure() {
1616
return React.useContext(PathRegisterContext);
1717
}
1818

19-
// ========================= Path Connect ==========================
20-
export const PathConnectContext = React.createContext<string[]>(EmptyList);
21-
22-
export function useKeyPath(eventKey: string) {
23-
const parentKeyPath = React.useContext(PathConnectContext);
24-
return React.useMemo(() => [...parentKeyPath, eventKey], [
25-
parentKeyPath,
26-
eventKey,
27-
]);
19+
// ========================= Path Tracker ==========================
20+
export const PathTrackerContext = React.createContext<string[]>(EmptyList);
21+
22+
export function useFullPath(eventKey?: string) {
23+
const parentKeyPath = React.useContext(PathTrackerContext);
24+
return React.useMemo(
25+
() =>
26+
eventKey !== undefined ? [...parentKeyPath, eventKey] : parentKeyPath,
27+
[parentKeyPath, eventKey],
28+
);
2829
}
2930

3031
// =========================== Path User ===========================
3132
export interface PathUserContextProps {
3233
isSubPathKey: (pathKeys: string[], eventKey: string) => boolean;
33-
getKeyPath: (eventKey: string) => string[];
3434
}
3535

3636
export const PathUserContext = React.createContext<PathUserContextProps>(null);

tests/__snapshots__/Keyboard.spec.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ exports[`Menu.Keyboard no data-menu-id by init 1`] = `
3232
<li
3333
class="rc-menu-item"
3434
role="menuitem"
35-
style="padding-left:24px"
35+
style="padding-left:48px"
3636
tabindex="-1"
3737
>
3838
Bamboo

0 commit comments

Comments
 (0)