Skip to content

Commit

Permalink
fix(layout): 修复 Layout 菜单submenu 和 grounp 混用的时候收起错误
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Nov 1, 2023
1 parent 446a346 commit d99a14d
Show file tree
Hide file tree
Showing 3 changed files with 598 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/layout/src/components/SiderMenu/BaseMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,18 @@ class MenuUtil {
getNavMenuItems = (
menusData: MenuDataItem[] = [],
level: number,
noGroupLevel: number,
): ItemType[] =>
menusData
.map((item) => this.getSubMenuOrItem(item, level))
.map((item) => this.getSubMenuOrItem(item, level, noGroupLevel))
.filter((item) => item)
.flat(1);

/** Get SubMenu or Item */
getSubMenuOrItem = (
item: MenuDataItem,
level: number,
noGroupLevel: number,
): ItemType | ItemType[] => {
const {
subMenuItemRender,
Expand Down Expand Up @@ -236,7 +238,7 @@ class MenuUtil {
this.props?.hashId,
{
[`${baseClassName}-item-title-collapsed`]: collapsed,
[`${baseClassName}-item-title-collapsed-level-${level}`]:
[`${baseClassName}-item-title-collapsed-level-${noGroupLevel}`]:
collapsed,
[`${baseClassName}-group-item-title`]: menuType === 'group',
[`${baseClassName}-item-collapsed-show-title`]:
Expand Down Expand Up @@ -284,11 +286,12 @@ class MenuUtil {
this.props.collapsed &&
!menu.collapsedShowGroupTitle
) {
return this.getNavMenuItems(children, level);
return this.getNavMenuItems(children, level + 1, level);
}

const childrenList = this.getNavMenuItems(
children,
level + 1,
isGroup && level === 0 && this.props.collapsed ? level : level + 1,
);

Expand Down Expand Up @@ -329,7 +332,7 @@ class MenuUtil {
disabled: item.disabled,
key: item.key! || item.path!,
onClick: item.onTitleClick,
label: this.getMenuItemPath(item, level),
label: this.getMenuItemPath(item, level, noGroupLevel),
};
};

Expand All @@ -350,7 +353,11 @@ class MenuUtil {
*
* @memberof SiderMenu
*/
getMenuItemPath = (item: MenuDataItem, level: number) => {
getMenuItemPath = (
item: MenuDataItem,
level: number,
noGroupLevel: number,
) => {
const itemPath = this.conversionPath(item.path || '/');
const {
location = { pathname: '/' },
Expand Down Expand Up @@ -386,7 +393,8 @@ class MenuUtil {
this.props?.hashId,
{
[`${baseClassName}-item-title-collapsed`]: collapsed,
[`${baseClassName}-item-title-collapsed-level-${level}`]: collapsed,
[`${baseClassName}-item-title-collapsed-level-${noGroupLevel}`]:
collapsed,
[`${baseClassName}-item-collapsed-show-title`]:
menu?.collapsedShowTitle && collapsed,
},
Expand Down Expand Up @@ -429,7 +437,7 @@ class MenuUtil {
this.props?.hashId,
{
[`${baseClassName}-item-title-collapsed`]: collapsed,
[`${baseClassName}-item-title-collapsed-level-${level}`]:
[`${baseClassName}-item-title-collapsed-level-${noGroupLevel}`]:
collapsed,
[`${baseClassName}-item-link`]: true,
[`${baseClassName}-item-collapsed-show-title`]:
Expand Down Expand Up @@ -703,7 +711,7 @@ const BaseMenu: React.FC<BaseMenuProps & PrivateSiderMenuProps> = (props) => {
[`${baseClassName}-horizontal`]: mode === 'horizontal',
[`${baseClassName}-collapsed`]: props.collapsed,
})}
items={menuUtils.getNavMenuItems(finallyData, 0)}
items={menuUtils.getNavMenuItems(finallyData, 0, 0)}
onOpenChange={(_openKeys) => {
if (!props.collapsed) {
setOpenKeys(_openKeys);
Expand Down
234 changes: 234 additions & 0 deletions packages/layout/src/demos/morse_debug.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
import {
GithubFilled,
InfoCircleFilled,
PlusCircleFilled,
QuestionCircleFilled,
SearchOutlined,
} from '@ant-design/icons';
import { PageContainer, ProCard, ProLayout } from '@ant-design/pro-components';
import { Button, Input } from 'antd';
import { useState } from 'react';

export default () => {
const [pathname, setPathname] = useState('/list/sub-page/sub-sub-page1');

return (
<ProLayout
collapsed
siderMenuType="group"
route={{
routes: [
{
path: '/all',
name: '总览',
parentId: '14',
children: [
{ path: '/all/workbench/index', name: '首页', parentId: '16' },
],
},
{
path: '/sa',
name: '隐私计算',
parentId: '14',
children: [
{
path: '/sa/experiment',
name: '项目空间',
parentId: '19',
children: [
{
path: '/sa/experiment/list',
name: '项目列表',
parentId: '20',
},
],
},
{
path: '/sa/offlinetask',
name: '任务管理中心',
parentId: '19',
children: [
{
path: '/sa/offlinetask/tasklist',
name: '研发任务',
parentId: '27',
},
{
path: '/sa/offlinetask/dispatchtask',
name: '调度任务',
parentId: '27',
},
],
},
{
path: '/sa/onlinetask',
name: '服务管控中心',
parentId: '19',
children: [
{
path: '/sa/onlinetask/anonymousquery',
name: '在线匿名查询服务',
parentId: '34',
},
],
},
],
},
{
path: '/others',
name: '其他',
parentId: '14',
children: [
{
path: '/others/data',
name: '数据管理',
parentId: '39',
children: [
{
path: '/others/data/list',
name: '所有数据',
parentId: '40',
},
{
path: '/others/data/datadictionary',
name: '数据字典',
parentId: '40',
},
{
path: '/others/data/outputConfiguration',
name: '输出配置',
parentId: '40',
},
],
},
{
path: '/others/monitormarket',
name: '监控大盘',
parentId: '39',
children: [
{
path: '/others/monitormarket/model',
name: '模型监控',
parentId: '58',
},
{
path: '/others/monitormarket/alliance',
name: '联盟监控',
parentId: '58',
},
{
path: '/others/monitormarket/resource',
name: '资源监控',
parentId: '58',
},
],
},
],
},
],
}}
location={{
pathname,
}}
avatarProps={{
src: 'https://gw.alipayobjects.com/zos/antfincdn/efFD%24IOql2/weixintupian_20170331104822.jpg',
size: 'small',
title: '七妮妮',
}}
actionsRender={(props) => {
if (props.isMobile) return [];
return [
props.layout !== 'side' ? (
<div
key="SearchOutlined"
aria-hidden
style={{
display: 'flex',
alignItems: 'center',
marginInlineEnd: 24,
}}
onMouseDown={(e) => {
e.stopPropagation();
e.preventDefault();
}}
>
<Input
style={{
borderRadius: 4,
marginInlineEnd: 12,
backgroundColor: 'rgba(0,0,0,0.03)',
}}
prefix={
<SearchOutlined
style={{
color: 'rgba(0, 0, 0, 0.15)',
}}
/>
}
placeholder="搜索方案"
bordered={false}
/>
<PlusCircleFilled
style={{
color: 'var(--ant-primary-color)',
fontSize: 24,
}}
/>
</div>
) : undefined,
<InfoCircleFilled key="InfoCircleFilled" />,
<QuestionCircleFilled key="QuestionCircleFilled" />,
<GithubFilled key="GithubFilled" />,
];
}}
menuFooterRender={(props) => {
if (props?.collapsed) return undefined;
return (
<p
style={{
textAlign: 'center',
paddingBlockStart: 12,
}}
>
Power by Ant Design
</p>
);
}}
onMenuHeaderClick={(e) => console.log(e)}
menuItemRender={(item, dom) => (
<a
onClick={() => {
setPathname(item.path || '/welcome');
}}
>
{dom}
</a>
)}
>
<PageContainer
extra={[
<Button key="3">操作</Button>,
<Button key="2">操作</Button>,
<Button key="1" type="primary">
主操作
</Button>,
]}
footer={[
<Button key="3">重置</Button>,
<Button key="2" type="primary">
提交
</Button>,
]}
>
<ProCard
style={{
height: '200vh',
minHeight: 800,
}}
>
<div />
</ProCard>
</PageContainer>
</ProLayout>
);
};

0 comments on commit d99a14d

Please sign in to comment.