From bb43c4312e82a161b146f83b7d6d601452f6e924 Mon Sep 17 00:00:00 2001 From: Li1304553726 <1304553726@qq.com> Date: Wed, 19 Nov 2025 11:45:03 +0800 Subject: [PATCH] 1 --- app/components/news/header/TopNav.tsx | 74 +++++++++++++++------------ 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/app/components/news/header/TopNav.tsx b/app/components/news/header/TopNav.tsx index f5dfd1f..b40a7ad 100644 --- a/app/components/news/header/TopNav.tsx +++ b/app/components/news/header/TopNav.tsx @@ -1,16 +1,11 @@ import { Search } from 'lucide-react'; -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; interface MenuItem { label: string; key: string; } -// 定义 TopNavProps 接口,描述组件的 props 类型 -// menuItems? 菜单项数组 -// activeKey: 当前激活的菜单项 -// onSearch: 搜索回调函数 -// onItemClick: 菜单点击回调函数 interface TopNavProps { menuItems?: MenuItem[]; activeKey?: string; @@ -18,11 +13,6 @@ interface TopNavProps { onItemClick?: (key: string) => void; } -//定义 TopNav 组件,类型为 React 函数组件,接收 TopNavProps 类型的 props -//解构并设置 menuItems 默认值,如果父组件没有传入则使用默认的6个菜单项 -// activeKey: 当前激活的菜单项 -// onSearch: 搜索回调函数 -// onItemClick: 菜单点击回调函数 export function TopNav({ menuItems = [ { label: '首页', key: 'home' }, @@ -32,35 +22,37 @@ export function TopNav({ { label: '联系热线', key: 'hotline' }, { label: '综合服务', key: 'service' }, ], - activeKey: externalActiveKey, // 从外部传入的 activeKey + activeKey: externalActiveKey, onSearch, onItemClick, }: TopNavProps){ - // 使用外部传入的 activeKey,如果没有则使用内部状态 - // 创建内部状态 internalActiveKey,默认值为 'home',用于内部管理激活状态 const [internalActiveKey, setInternalActiveKey] = useState('home'); - // 如果外部传入了 activeKey 则使用外部的,否则使用内部状态(支持受控和非受控模式) const currentActiveKey = externalActiveKey !== undefined ? externalActiveKey : internalActiveKey; - // 创建搜索关键词状态,初始为空字符串 const [searchKeyword, setSearchKeyword] = useState(''); - // 处理搜索提交事件, 阻止默认表单提交行为,调用搜索回调函数 + const [activeIndex, setActiveIndex] = useState(0); + const [prevIndex, setPrevIndex] = useState(0); + + // 监听激活项变化,更新索引和动画方向 + useEffect(() => { + const currentIndex = menuItems.findIndex(item => item.key === currentActiveKey); + setPrevIndex(activeIndex); + setActiveIndex(currentIndex); + }, [currentActiveKey, menuItems, activeIndex]); + const handleSearchSubmit = (e: React.FormEvent) => { e.preventDefault(); - onSearch?.(searchKeyword); // .? 可选链操作符,确保 onSearch 存在时才调用 + onSearch?.(searchKeyword); }; - // 定义菜单项点击处理函数,如果外部没有传入 activeKey 则更新内部状态,调用点击回调函数 const handleItemClick = (item: MenuItem) => { - // 更新内部状态(如果使用内部状态) if (externalActiveKey === undefined) { setInternalActiveKey(item.key); } - // 调用外部回调函数 - onItemClick?.(item.key); // .? 可选链操作符,确保 onItemClick 存在时才调用 + onItemClick?.(item.key); }; return ( -
+
{/* 搜索框与导航菜单组合 */}
{/* 搜索框 */} @@ -71,26 +63,42 @@ export function TopNav({ value={searchKeyword} onChange={(e) => setSearchKeyword(e.target.value)} placeholder="搜索..." - className="pl-10 pr-4 py-2 text-sm rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent w-64 transition-all duration-200 hover:shadow-sm" + className="pl-10 pr-4 py-2 text-sm border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent w-64 transition-all duration-200 hover:shadow-sm" />
- + {/* 导航菜单 */} -
); -}; +}; \ No newline at end of file