SideNav.tsx
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { useCallback, useEffect, useRef, useState } from 'react';
import type { ReactNode } from 'react';
import { createPortal } from 'react-dom';
import {
DashboardOutlined,
ShopOutlined,
UploadOutlined,
TeamOutlined,
AuditOutlined,
AppstoreOutlined,
FolderOutlined,
GiftOutlined,
FileSyncOutlined,
FundOutlined,
TransactionOutlined,
SolutionOutlined,
HomeOutlined,
SettingOutlined,
CarOutlined,
FundProjectionScreenOutlined,
AimOutlined,
ExperimentOutlined,
ControlOutlined,
ToolOutlined,
SendOutlined,
ReconciliationOutlined,
UserSwitchOutlined,
FileTextOutlined,
SafetyCertificateOutlined,
WechatOutlined,
PayCircleOutlined,
WalletOutlined,
RightOutlined,
} from '@ant-design/icons';
import type { MenuGroupDef } from '@/access';
/**
* 侧栏导航(自绘,不走 antd Menu)。
*
* 为什么自绘:重设计后一级是「章节头」——四列网格(图标 / 标题槽 / 子项数 / 展开指示),
* 二级是导轨下的纯文字项,且章节头与子项高度不同。antd Menu 的 SubMenu 把两者共用
* itemHeight,标题内容包在 .ant-menu-title-content 里由内容撑宽;要让标题槽 flex:1、
* 右端对齐到同一条线,只能覆盖组件内部盒模型 —— design.css 旧注释自己就写过
* 「折叠态居中尤其敏感,任何 width / padding-inline 覆盖都会算歪」。
* 用 type:'group' 自定义 label 也不行:group 标题在 inline 模式下点不动,会把
* BasicLayout 里「原先 group 标题永远展开才换成 SubMenu」的修复回退掉。
*
* 因此只换渲染层:数据仍是后端菜单树经 buildMenus 得到的 MenuGroupDef[],
* 空组剔除 / selectedKey 最长前缀 / openKeys 受控校准 / 切页时的
* PAGE_LEAVE_EVENT + cancelRequests + navigate 全部留在 BasicLayout,本组件不碰。
* 菜单显隐以 /system/menu/tree/current 为准,本组件不做任何权限判断。
*
* 二级菜单刻意不渲染 m.icon:12 个不同线性图标在 34px 行里会形成锯齿边,
* 且 iconMap 是白名单,后端新增页面漏登记就会静默回落。二级改用导轨上的统一节点,
* 选中信号也由该节点承担(半透明点 → 品牌蓝实心 + 亮环)。后端 sys_menu.icon
* 字段不动,一级分组与工作台单入口仍按 icon 渲染。
*/
interface SideNavProps {
/** 权限过滤后的分组(空组已剔除),由 BasicLayout 生成 */
groups: MenuGroupDef[];
selectedKey: string;
/** 当前页所属分组名;null 表示当前页是平铺单入口或非菜单页 */
activeGroupName: string | null;
isGroupOpen: (name: string) => boolean;
onToggleGroup: (name: string) => void;
collapsed: boolean;
onToggleCollapsed: () => void;
onNavigate: (path: string) => void;
/** 底部信息条:当前登录角色,取自登录态而非写死 */
roleLabel: string;
}
/** 图标注册表:只登记用到的字面量,避免 `import * as antdIcons` 把 894 个图标打进首屏。
* 现在只有一级分组与工作台单入口会查表;二级不查,故后端给二级配的图标名无需登记。 */
const iconMap: Record<string, ReactNode> = {
DashboardOutlined: <DashboardOutlined />,
ShopOutlined: <ShopOutlined />,
UploadOutlined: <UploadOutlined />,
TeamOutlined: <TeamOutlined />,
AuditOutlined: <AuditOutlined />,
GiftOutlined: <GiftOutlined />,
FileSyncOutlined: <FileSyncOutlined />,
FundOutlined: <FundOutlined />,
TransactionOutlined: <TransactionOutlined />,
SolutionOutlined: <SolutionOutlined />,
HomeOutlined: <HomeOutlined />,
SettingOutlined: <SettingOutlined />,
CarOutlined: <CarOutlined />,
FundProjectionScreenOutlined: <FundProjectionScreenOutlined />,
AimOutlined: <AimOutlined />,
ExperimentOutlined: <ExperimentOutlined />,
ControlOutlined: <ControlOutlined />,
ToolOutlined: <ToolOutlined />,
SendOutlined: <SendOutlined />,
ReconciliationOutlined: <ReconciliationOutlined />,
UserSwitchOutlined: <UserSwitchOutlined />,
FileTextOutlined: <FileTextOutlined />,
SafetyCertificateOutlined: <SafetyCertificateOutlined />,
WechatOutlined: <WechatOutlined />,
PayCircleOutlined: <PayCircleOutlined />,
// 财务管理(finance)一级目录,见 scripts/finance-menu-migration.sql
WalletOutlined: <WalletOutlined />,
};
const DEFAULT_MENU_ICON: ReactNode = <AppstoreOutlined />;
const GROUP_DEFAULT_ICON: ReactNode = <FolderOutlined />;
const warnedIcons = new Set<string>();
function resolveIcon(
name: string | null | undefined,
fallback: ReactNode = DEFAULT_MENU_ICON,
): ReactNode {
const hit = name ? iconMap[name] : undefined;
if (hit) return hit;
if (name && import.meta.env.DEV && !warnedIcons.has(name)) {
warnedIcons.add(name);
console.warn(
`[menu] 一级图标「${name}」未在 SideNav.tsx 的 iconMap 中登记,已回落兜底图标。` +
`请在 iconMap 补一行,或让后端改发已支持的值。`,
);
}
return fallback;
}
/**
* 折叠控制线稿:与 docs/sider-redesign.html 的 fold / unfold 路径逐条一致。
* 不用 antd MenuFoldOutlined / MenuUnfoldOutlined —— 那两个是「带方框 + 三条横线」的
* 汉堡图标,和侧栏的极简线稿语言不符,是原型与实现最直观的一处不一致。
*/
const FOLD_PATHS: Record<'fold' | 'unfold', string[]> = {
fold: ['M14 5l-7 7 7 7', 'M20 5v14'],
unfold: ['M10 5l7 7-7 7', 'M4 5v14'],
};
function FoldGlyph({ collapsed }: { collapsed: boolean }) {
return (
<svg
width="15"
height="15"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.7"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
{FOLD_PATHS[collapsed ? 'unfold' : 'fold'].map((d) => (
<path key={d} d={d} />
))}
</svg>
);
}
interface PopoverState {
group: MenuGroupDef;
top: number;
}
export default function SideNav({
groups,
selectedKey,
activeGroupName,
isGroupOpen,
onToggleGroup,
collapsed,
onToggleCollapsed,
onNavigate,
roleLabel,
}: SideNavProps) {
const navRef = useRef<HTMLElement>(null);
const [fade, setFade] = useState({ top: false, bottom: false });
const [popover, setPopover] = useState<PopoverState | null>(null);
const solo = groups.filter((g) => g.hideTitle).flatMap((g) => g.children);
const chapters = groups.filter((g) => !g.hideTitle);
/** 长菜单的上下渐隐:只在真的还有内容时出现,静默提示可滚动。 */
const syncFade = useCallback(() => {
const el = navRef.current;
if (!el) return;
const more = el.scrollHeight - el.clientHeight;
const top = el.scrollTop > 4;
const bottom = more > 4 && el.scrollTop < more - 4;
setFade((prev) => (prev.top === top && prev.bottom === bottom ? prev : { top, bottom }));
}, []);
useEffect(() => {
syncFade();
window.addEventListener('resize', syncFade);
return () => window.removeEventListener('resize', syncFade);
}, [syncFade, groups, collapsed, popover]);
/** 折叠态浮层:点外部或 Esc 关闭。浮层 portal 到 body,避开 .ant-layout-sider 的 overflow:hidden。 */
useEffect(() => {
if (!popover) return;
const onDocClick = (e: MouseEvent) => {
const target = e.target as HTMLElement | null;
if (!target?.closest('.uj-nav-popover') && !target?.closest('.uj-chap-head')) {
setPopover(null);
}
};
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') setPopover(null);
};
document.addEventListener('click', onDocClick);
document.addEventListener('keydown', onKey);
return () => {
document.removeEventListener('click', onDocClick);
document.removeEventListener('keydown', onKey);
};
}, [popover]);
// 展开侧栏时浮层失去意义,立即收掉,避免展开后出现一块悬空面板。
useEffect(() => {
if (!collapsed) setPopover(null);
}, [collapsed]);
const handleChapterClick = (group: MenuGroupDef, anchorTop: number) => {
if (collapsed) {
// 浮层跟随锚点,但按真实子项数估高后夹回视口内:
// 二手车管理 12 项在矮窗口里不夹会整块溢出屏幕底部。
const estimated = 44 + group.children.length * 32;
const top = Math.max(12, Math.min(anchorTop, window.innerHeight - estimated - 12));
setPopover((prev) => (prev?.group.group === group.group ? null : { group, top }));
return;
}
onToggleGroup(group.group);
};
return (
<>
<nav className="uj-nav" ref={navRef} onScroll={syncFade} aria-label="主导航">
<div className="uj-zone-label">工作区</div>
{solo.map((m) => {
const active = selectedKey === m.path;
return (
<button
key={m.path}
type="button"
className={`uj-solo${active ? ' is-active' : ''}`}
title={m.label}
aria-current={active ? 'page' : undefined}
onClick={() => onNavigate(m.path)}
>
<span className="uj-solo-ico">{resolveIcon(m.icon)}</span>
<span className="uj-solo-txt">{m.label}</span>
</button>
);
})}
<div className="uj-zone-sep" />
<div className="uj-zone-label">业务域</div>
{chapters.map((g) => {
const open = isGroupOpen(g.group);
const current = activeGroupName === g.group;
return (
<div
key={g.group}
className={`uj-chapter${open ? ' is-open' : ''}${current ? ' is-current' : ''}`}
>
<button
type="button"
className="uj-chap-head"
title={g.group}
aria-expanded={collapsed ? undefined : open}
onClick={(e) => handleChapterClick(g, e.currentTarget.getBoundingClientRect().top)}
>
<span className="uj-chap-ico">
{resolveIcon(
g.icon,
g.children[0] ? resolveIcon(g.children[0].icon) : GROUP_DEFAULT_ICON,
)}
</span>
<span className="uj-chap-label">{g.group}</span>
<span className="uj-chap-count">{g.children.length}</span>
<span className="uj-chap-chev">
<RightOutlined />
</span>
</button>
{!collapsed && open && (
<div className="uj-chap-body">
{g.children.map((m) => {
const active = selectedKey === m.path;
return (
<button
key={m.path}
type="button"
className={`uj-leaf${active ? ' is-active' : ''}`}
title={m.label}
aria-current={active ? 'page' : undefined}
onClick={() => onNavigate(m.path)}
>
<span className="uj-leaf-node" aria-hidden="true" />
<span className="uj-leaf-txt">{m.label}</span>
</button>
);
})}
</div>
)}
</div>
);
})}
</nav>
<div className={`uj-nav-fade is-top${fade.top ? ' is-on' : ''}`} aria-hidden="true" />
<div className={`uj-nav-fade is-bottom${fade.bottom ? ' is-on' : ''}`} aria-hidden="true" />
<div className="uj-sider-foot">
<button
type="button"
className="uj-fold-btn"
onClick={onToggleCollapsed}
aria-label={collapsed ? '展开导航' : '收起导航'}
title={collapsed ? '展开导航' : '收起导航'}
>
<FoldGlyph collapsed={collapsed} />
</button>
{!collapsed && (
<div className="uj-foot-meta">
<span className="n">{roleLabel}</span>
<span className="e">当前登录角色</span>
</div>
)}
</div>
{popover &&
createPortal(
<div className="uj-nav-popover" role="menu" style={{ top: popover.top }}>
<div className="uj-pop-title">{popover.group.group}</div>
{popover.group.children.map((m) => (
<button
key={m.path}
type="button"
role="menuitem"
className={`uj-pop-item${selectedKey === m.path ? ' is-active' : ''}`}
onClick={() => {
onNavigate(m.path);
setPopover(null);
}}
>
{m.label}
</button>
))}
</div>,
document.body,
)}
</>
);
}