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 ? (
-
+
+
+
) : (
/* 根据侧边栏状态显示不同按钮 */
-
{isCollapsed ? (
-
+
) : (
-
+
)}
-
+
)}
@@ -119,52 +114,93 @@ export function LeftSidebar({
{menus.map((menu) => (
{/* 一级菜单 */}
-
toggleMenu(menu.id)}
- className={cn(
- "w-full flex items-center justify-between px-3 py-2 rounded-md transition-colors text-sm",
- "hover:bg-gray-100 hover:text-gray-900",
- isCollapsed ? "justify-center px-2 py-3" : "px-3 py-2"
- )}
- title={isCollapsed ? menu.label : undefined}
- >
-
- {menu.icon && (
-
- {menu.icon}
-
- )}
- {!isCollapsed && (
- {menu.label}
- )}
-
- {!isCollapsed && menu.children && (
- expandedMenus.has(menu.id) ? (
-
- ) : (
-
- )
- )}
-
-
- {/* 二级菜单 */}
- {!isCollapsed && menu.children && expandedMenus.has(menu.id) && (
-
- {menu.children.map((child) => (
- child.path && onNavigate(child.path)}
+ {menu.children ? (
+ {
+ if (open) {
+ setExpandedMenus(prev => new Set(prev).add(menu.id));
+ } else {
+ setExpandedMenus(prev => {
+ const newSet = new Set(prev);
+ newSet.delete(menu.id);
+ return newSet;
+ });
+ }
+ }}
+ >
+
+
- {child.label}
-
- ))}
-
+
+ {menu.icon && (
+
+ {menu.icon}
+
+ )}
+ {!isCollapsed && (
+ {menu.label}
+ )}
+
+ {menu.children && (
+ isCollapsed ? null : (
+
+ )
+ )}
+
+
+
+ {/* 二级菜单 */}
+ {!isCollapsed && (
+
+ {menu.children.map((child) => (
+ child.path && onNavigate(child.path)}
+ >
+ {child.label}
+
+ ))}
+
+ )}
+
+
+ ) : (
+
+
+ {menu.icon && (
+
+ {menu.icon}
+
+ )}
+ {!isCollapsed && (
+ {menu.label}
+ )}
+
+
)}
))}
@@ -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 (
+ <>
+ {/* 移动端菜单按钮 */}
+
+ setShowMobileSidebar(true)}
+ >
+
+
+
- {/* 主内容区域 - 去掉顶部白色横条 */}
-
-
+ {/* 移动端侧边栏 */}
+
+
+ {/* 这里应该渲染LeftSidebar内容,但需要通过props传递 */}
+
+
+
+
+ {/* 主内容区域 */}
+
{children}
+ >
+ );
+ }
+
+ return (
+
);
}
\ 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