From 4f3beb2568647e82a03e3255287693e0c7f9db5e Mon Sep 17 00:00:00 2001 From: peng Date: Thu, 23 Oct 2025 15:13:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=AE=A1=E7=90=86=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=89=8D=E7=AB=AF=20=E8=8F=9C=E5=8D=95=E7=AE=AD?= =?UTF-8?q?=E5=A4=B4=E6=98=BE=E7=A4=BAfix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/layouts/SideBar/SideBarOld.tsx | 42 +++- .../SideBar/components/LeftSidebar.tsx | 190 +++++++++++------- .../SideBar/components/MainContent.tsx | 49 +++-- .../layouts/SideBar/components/lib/utils.ts | 6 - 4 files changed, 180 insertions(+), 107 deletions(-) delete mode 100644 crop-x/src/components/layouts/SideBar/components/lib/utils.ts diff --git a/crop-x/src/components/layouts/SideBar/SideBarOld.tsx b/crop-x/src/components/layouts/SideBar/SideBarOld.tsx index e123baa..d3730ea 100644 --- a/crop-x/src/components/layouts/SideBar/SideBarOld.tsx +++ b/crop-x/src/components/layouts/SideBar/SideBarOld.tsx @@ -4,6 +4,7 @@ import { useState, useEffect } from 'react'; import { useRouter, usePathname } from 'next/navigation'; import { LeftSidebar } from './components/LeftSidebar'; import { MainContent } from './components/MainContent'; +import { cn } from '@/lib/utils'; // 菜单项数据结构定义 interface NavItem { @@ -230,18 +231,23 @@ export function SideBarOld({ router.push(path); }; return ( -
+
{/* 左侧导航栏 - 独立滚动 */} -
- setIsCollapsed(!isCollapsed)} - /> -
+ {!isMobile && ( +
+ setIsCollapsed(!isCollapsed)} + /> +
+ )} {/* 右侧主内容 - 独立滚动 */}
@@ -253,6 +259,20 @@ export function SideBarOld({ {children}
+ + {/* 移动端侧边栏 */} + {isMobile && ( +
+ {}} + /> +
+ )}
); } \ No newline at end of file diff --git a/crop-x/src/components/layouts/SideBar/components/LeftSidebar.tsx b/crop-x/src/components/layouts/SideBar/components/LeftSidebar.tsx index cfc14f7..b8530b1 100644 --- a/crop-x/src/components/layouts/SideBar/components/LeftSidebar.tsx +++ b/crop-x/src/components/layouts/SideBar/components/LeftSidebar.tsx @@ -3,6 +3,13 @@ import { useState, useEffect } from 'react'; import { ChevronDown, ChevronLeft, ChevronRight, Menu, X } from 'lucide-react'; import { cn } from '@/lib/utils'; +import { Button } from '@/components/ui/button'; +import { Badge } from '@/components/ui/badge'; +import { + Collapsible, + CollapsibleContent, + CollapsibleTrigger, +} from '@/components/ui/collapsible'; interface MenuItem { id: string; @@ -32,35 +39,19 @@ export function LeftSidebar({ isCollapsed = false, onToggleCollapse }: LeftSidebarProps) { - // 根据activePath自动展开包含该路径的菜单 + // 初始状态下所有菜单都折叠 const getInitialExpandedMenus = () => { - const expanded = new Set(); - menus.forEach(menu => { - if (menu.children?.some(child => child.path === activePath)) { - expanded.add(menu.id); - } - }); - // 如果没有匹配的,默认展开第一个 - if (expanded.size === 0 && menus.length > 0) { - expanded.add(menus[0].id); - } - return expanded; + return new Set(); }; const [expandedMenus, setExpandedMenus] = useState>(getInitialExpandedMenus()); - // 当activePath或menus变化时,自动展开对应的菜单 + // 不自动展开菜单,由用户手动控制 + + // 当侧边栏状态改变时,折叠所有菜单 useEffect(() => { - menus.forEach(menu => { - if (menu.children?.some(child => child.path === activePath)) { - setExpandedMenus(prev => { - const newSet = new Set(prev); - newSet.add(menu.id); - return newSet; - }); - } - }); - }, [activePath, menus]); + setExpandedMenus(new Set()); + }, [isCollapsed]); const toggleMenu = (menuId: string) => { setExpandedMenus(prev => { @@ -77,35 +68,39 @@ export function LeftSidebar({ return (
{/* 头部 - 缩小高度 */} -
+

导航菜单

{isMobile ? ( - + ) : ( /* 根据侧边栏状态显示不同按钮 */ - + )}
@@ -119,52 +114,93 @@ export function LeftSidebar({ {menus.map((menu) => (
{/* 一级菜单 */} - - - {/* 二级菜单 */} - {!isCollapsed && menu.children && expandedMenus.has(menu.id) && ( -
- {menu.children.map((child) => ( - - ))} -
+
+ {menu.icon && ( + + {menu.icon} + + )} + {!isCollapsed && ( + {menu.label} + )} +
+ {menu.children && ( + isCollapsed ? null : ( + + ) + )} + + + + {/* 二级菜单 */} + {!isCollapsed && ( +
+ {menu.children.map((child) => ( + + ))} +
+ )} +
+ + ) : ( + )}
))} @@ -172,9 +208,9 @@ export function LeftSidebar({
{/* 底部 */} -
+
{isCollapsed ? "管理" : "管理系统"} diff --git a/crop-x/src/components/layouts/SideBar/components/MainContent.tsx b/crop-x/src/components/layouts/SideBar/components/MainContent.tsx index a4efaf6..38ef028 100644 --- a/crop-x/src/components/layouts/SideBar/components/MainContent.tsx +++ b/crop-x/src/components/layouts/SideBar/components/MainContent.tsx @@ -3,6 +3,8 @@ import { useState } from 'react'; import { ChevronLeft, ChevronRight, X } from 'lucide-react'; import { cn } from '@/lib/utils'; +import { Button } from '@/components/ui/button'; +import { Sheet, SheetContent, SheetTrigger } from '@/components/ui/sheet'; interface MainContentProps { children: React.ReactNode; @@ -27,22 +29,43 @@ export function MainContent({ } }; - return ( - <> - {/* 移动端侧边栏遮罩 */} - {isMobile && showMobileSidebar && ( -
setShowMobileSidebar(false)} - /> - )} + if (isMobile) { + return ( + <> + {/* 移动端菜单按钮 */} +
+ +
- {/* 主内容区域 - 去掉顶部白色横条 */} -
-
+ {/* 移动端侧边栏 */} + + + {/* 这里应该渲染LeftSidebar内容,但需要通过props传递 */} +
+

侧边栏内容

+
+
+
+ + {/* 主内容区域 */} +
{children}
+ + ); + } + + return ( +
+
+ {children}
- +
); } \ No newline at end of file diff --git a/crop-x/src/components/layouts/SideBar/components/lib/utils.ts b/crop-x/src/components/layouts/SideBar/components/lib/utils.ts deleted file mode 100644 index 1a860ee..0000000 --- a/crop-x/src/components/layouts/SideBar/components/lib/utils.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { type ClassValue, clsx } from "clsx" -import { twMerge } from "tailwind-merge" - -export function cn(...inputs: ClassValue[]) { - return twMerge(clsx(inputs)) -} \ No newline at end of file