Skip to content

Commit 0a4d0e8

Browse files
committed
fix(ui:menu): remove the use of the debug interface
In fact, the difference between the test environment and the production environment is indistinguishable.
1 parent 3d5a0e0 commit 0a4d0e8

19 files changed

Lines changed: 245 additions & 237 deletions

commitlint.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ module.exports = {
3535
rules: {
3636
'type-enum': [2, 'always', types],
3737
'scope-enum': getScopesRule,
38+
'body-max-length': [0],
39+
'body-max-line-length': [0],
3840
},
3941
};

packages/site/src/app/components/sidebar/Sidebar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ export function AppSidebar() {
3737
<nav className="app-sidebar">
3838
<DMenu dActive={activeId} onActiveChange={handleActiveChange}>
3939
{menu.map((group) => (
40-
<DMenuGroup key={group.title} dTitle={t(`menu-group.${group.title}`)}>
40+
<DMenuGroup key={group.title} dId={group.title} dTitle={t(`menu-group.${group.title}`)}>
4141
{group.children.map((child) => (
42-
<DMenuItem key={child.title} onClick={() => navigate(child.to, { replace: true })}>
42+
<DMenuItem key={child.title} dId={child.title} onClick={() => navigate(child.to, { replace: true })}>
4343
{child.title}
4444
{i18n.language !== 'en-US' && <span className="app-sidebar__subtitle">{t(`menu.${child.title}`)}</span>}
4545
</DMenuItem>

packages/ui/src/components/menu/Menu.tsx

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import React, { useCallback, useMemo, useState } from 'react';
44
import { useImmer } from 'use-immer';
55

66
import { useDPrefixConfig, useDComponentConfig, useManualOrAutoState } from '../../hooks';
7-
import { getClassName, getComponentName } from '../../utils';
7+
import { getClassName } from '../../utils';
88
import { DCollapseTransition } from '../_transition';
99
import { DTrigger } from '../_trigger';
10-
import { generateChildren, isMenuComponent } from './utils';
10+
import { generateChildren } from './utils';
1111

1212
enableMapSet();
1313

@@ -34,6 +34,8 @@ export interface DMenuProps extends React.HTMLAttributes<HTMLElement> {
3434
dMode?: 'horizontal' | 'vertical' | 'popup' | 'icon';
3535
dExpandOne?: boolean;
3636
dExpandTrigger?: 'hover' | 'click';
37+
dHeader?: React.ReactNode;
38+
dFooter?: React.ReactNode;
3739
onActiveChange?: (id: string) => void;
3840
onExpandsChange?: (ids: string[]) => void;
3941
}
@@ -46,6 +48,8 @@ export function DMenu(props: DMenuProps) {
4648
dMode = 'vertical',
4749
dExpandOne = false,
4850
dExpandTrigger,
51+
dHeader,
52+
dFooter,
4953
onActiveChange,
5054
onExpandsChange,
5155
className,
@@ -137,38 +141,28 @@ export function DMenu(props: DMenuProps) {
137141
const childs = useMemo(() => {
138142
const arr: string[] = [];
139143
const _childs = generateChildren(children).map((child, index) => {
140-
if (isMenuComponent(child)) {
141-
arr.push(child.props.__id);
142-
143-
let tabIndex = child.props.tabIndex;
144-
if (index === 0) {
145-
tabIndex = 0;
146-
}
147-
148-
let __navMenu = undefined;
149-
const componentName = getComponentName(child);
150-
if (componentName === 'DMenuSub' || componentName === 'DMenuItem') {
151-
__navMenu = true;
152-
}
153-
154-
return React.cloneElement(child, {
155-
...child.props,
156-
tabIndex,
157-
__navMenu,
158-
__onFocus: (id: string) => {
159-
setFocusId((draft) => {
160-
draft.add(id);
161-
});
162-
},
163-
__onBlur: (id: string) => {
164-
setFocusId((draft) => {
165-
draft.delete(id);
166-
});
167-
},
168-
});
144+
arr.push(child.props.id);
145+
146+
let tabIndex = child.props.tabIndex;
147+
if (index === 0) {
148+
tabIndex = 0;
169149
}
170150

171-
return child;
151+
return React.cloneElement(child, {
152+
...child.props,
153+
tabIndex,
154+
__navMenu: true,
155+
__onFocus: (id: string) => {
156+
setFocusId((draft) => {
157+
draft.add(id);
158+
});
159+
},
160+
__onBlur: (id: string) => {
161+
setFocusId((draft) => {
162+
draft.delete(id);
163+
});
164+
},
165+
});
172166
});
173167
// eslint-disable-next-line @typescript-eslint/no-explicit-any
174168
currentData.ids.set(Symbol('menu') as any, arr);
@@ -205,7 +199,9 @@ export function DMenu(props: DMenuProps) {
205199
aria-orientation={dMode === 'horizontal' ? 'horizontal' : 'vertical'}
206200
aria-activedescendant={Array.from(focusId)[0] ?? undefined}
207201
>
202+
{dHeader}
208203
{childs}
204+
{dFooter}
209205
</nav>
210206
</DTrigger>
211207
</DCollapseTransition>

packages/ui/src/components/menu/MenuGroup.tsx

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import React, { useCallback, useMemo } from 'react';
44
import { useDPrefixConfig, useDComponentConfig, useCustomContext } from '../../hooks';
55
import { getClassName, toId } from '../../utils';
66
import { DMenuContext } from './Menu';
7-
import { generateChildren, isMenuComponent } from './utils';
7+
import { generateChildren } from './utils';
88

99
export interface DMenuGroupProps extends React.HTMLAttributes<HTMLDivElement> {
10+
dId: string;
1011
dTitle: React.ReactNode;
11-
__id?: string;
1212
__level?: number;
13+
__navMenu?: boolean;
1314
__onFocus?: (id: string) => void;
1415
__onBlur?: (id: string) => void;
1516
}
1617

1718
export function DMenuGroup(props: DMenuGroupProps) {
1819
const {
20+
dId,
1921
dTitle,
20-
__id = '',
2122
__level = 0,
23+
__navMenu = false,
2224
__onFocus,
2325
__onBlur,
2426
className,
@@ -56,17 +58,17 @@ export function DMenuGroup(props: DMenuGroupProps) {
5658
const handleFocus = useCallback(
5759
(e) => {
5860
onFocus?.(e);
59-
__onFocus?.(`menu-group-${toId(__id)}`);
61+
__onFocus?.(`menu-group-${toId(dId)}`);
6062
},
61-
[__id, __onFocus, onFocus]
63+
[__onFocus, dId, onFocus]
6264
);
6365

6466
const handleBlur = useCallback(
6567
(e) => {
6668
onBlur?.(e);
67-
__onBlur?.(`menu-group-${toId(__id)}`);
69+
__onBlur?.(`menu-group-${toId(dId)}`);
6870
},
69-
[__id, __onBlur, onBlur]
71+
[__onBlur, onBlur, dId]
7072
);
7173
//#endregion
7274

@@ -82,32 +84,28 @@ export function DMenuGroup(props: DMenuGroupProps) {
8284
const childs = useMemo(() => {
8385
const arr: string[] = [];
8486
const _childs = generateChildren(children, true).map((child) => {
85-
if (isMenuComponent(child)) {
86-
arr.push(child.props.__id);
87-
return React.cloneElement(child, {
88-
...child.props,
89-
__level: __level + 1,
90-
__onFocus: (id: string) => {
91-
__onFocus?.(id);
92-
},
93-
__onBlur: (id: string) => {
94-
__onBlur?.(id);
95-
},
96-
});
97-
}
98-
99-
return child;
87+
arr.push(child.props.dId);
88+
return React.cloneElement(child, {
89+
...child.props,
90+
__level: __level + 1,
91+
__onFocus: (id: string) => {
92+
__onFocus?.(id);
93+
},
94+
__onBlur: (id: string) => {
95+
__onBlur?.(id);
96+
},
97+
});
10098
});
101-
_currentData?.ids.set(__id, arr);
99+
_currentData?.ids.set(dId, arr);
102100
return _childs;
103-
}, [__id, __level, __onFocus, __onBlur, _currentData, children]);
101+
}, [__level, __onFocus, __onBlur, _currentData, dId, children]);
104102
//#endregion
105103

106104
return (
107105
<>
108106
<div
109107
{...restProps}
110-
id={`menu-group-${toId(__id)}`}
108+
id={`menu-group-${toId(dId)}`}
111109
className={getClassName(className, `${dPrefix}menu-group`)}
112110
style={{
113111
...style,

packages/ui/src/components/menu/MenuItem.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { DTooltip } from '../tooltip';
88
import { DMenuContext } from './Menu';
99

1010
export interface DMenuItemProps extends React.LiHTMLAttributes<HTMLLIElement> {
11+
dId: string;
1112
dIcon?: React.ReactNode;
1213
dDisabled?: boolean;
13-
__id?: string;
1414
__level?: number;
1515
__navMenu?: boolean;
1616
__onFocus?: (id: string) => void;
@@ -19,9 +19,9 @@ export interface DMenuItemProps extends React.LiHTMLAttributes<HTMLLIElement> {
1919

2020
export function DMenuItem(props: DMenuItemProps) {
2121
const {
22+
dId,
2223
dIcon,
2324
dDisabled = false,
24-
__id = '',
2525
__level = 0,
2626
__navMenu = false,
2727
__onFocus,
@@ -62,25 +62,25 @@ export function DMenuItem(props: DMenuItemProps) {
6262
const handleClick = useCallback(
6363
(e) => {
6464
onClick?.(e);
65-
_onActiveChange?.(__id);
65+
_onActiveChange?.(dId);
6666
},
67-
[_onActiveChange, __id, onClick]
67+
[_onActiveChange, dId, onClick]
6868
);
6969

7070
const handleFocus = useCallback(
7171
(e) => {
7272
onFocus?.(e);
73-
__onFocus?.(`menu-item-${toId(__id)}`);
73+
__onFocus?.(`menu-item-${toId(dId)}`);
7474
},
75-
[__id, __onFocus, onFocus]
75+
[__onFocus, dId, onFocus]
7676
);
7777

7878
const handleBlur = useCallback(
7979
(e) => {
8080
onBlur?.(e);
81-
__onBlur?.(`menu-item-${toId(__id)}`);
81+
__onBlur?.(`menu-item-${toId(dId)}`);
8282
},
83-
[__id, __onBlur, onBlur]
83+
[__onBlur, dId, onBlur]
8484
);
8585
//#endregion
8686

@@ -89,9 +89,9 @@ export function DMenuItem(props: DMenuItemProps) {
8989
<DTooltip dTitle={_dMode === 'icon' && __navMenu && children} dPlacement="right">
9090
<li
9191
{...restProps}
92-
id={`menu-item-${toId(__id)}`}
92+
id={`menu-item-${toId(dId)}`}
9393
className={getClassName(className, `${dPrefix}menu-item`, {
94-
'is-active': _activeId === __id,
94+
'is-active': _activeId === dId,
9595
'is-horizontal': _dMode === 'horizontal' && __navMenu,
9696
'is-icon': _dMode === 'icon' && __navMenu,
9797
})}

0 commit comments

Comments
 (0)