news/app/components/news/header/TopNav.tsx

111 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-11-18 15:26:36 +08:00
import { Search } from 'lucide-react';
2025-11-20 10:50:06 +08:00
import React, { useState, useEffect } from 'react';
2025-11-19 15:53:36 +08:00
import { Button } from '@/ui/button';
2025-11-18 15:26:36 +08:00
interface MenuItem {
label: string;
key: string;
sectionId:string;
2025-11-18 15:26:36 +08:00
}
interface TopNavProps {
menuItems?: MenuItem[];
activeKey?: string;
onSearch?: (keyword: string) => void;
onItemClick?: (key: string) => void;
}
export function TopNav({
menuItems = [
{ label: '首页', key: 'home', sectionId:'home' },
{ label: '烽火动态', key: 'news', sectionId:'fireNews' },
{ label: '烽火铸魂', key: 'soul', sectionId:'soul' },
{ label: '烽火训练', key: 'training', sectionId:'training' },
{ label: '联系热线', key: 'hotline', sectionId:'hotline' },
{ label: '综合服务', key: 'service', sectionId:'service' },
2025-11-18 15:26:36 +08:00
],
2025-11-19 11:45:03 +08:00
activeKey: externalActiveKey,
2025-11-18 15:26:36 +08:00
onSearch,
onItemClick,
2025-11-20 10:50:06 +08:00
}: TopNavProps) {
2025-11-18 15:26:36 +08:00
const [internalActiveKey, setInternalActiveKey] = useState('home');
const currentActiveKey = externalActiveKey !== undefined ? externalActiveKey : internalActiveKey;
const [searchKeyword, setSearchKeyword] = useState('');
2025-11-20 10:50:06 +08:00
2025-11-19 11:45:03 +08:00
2025-11-18 15:26:36 +08:00
const handleSearchSubmit = (e: React.FormEvent) => {
e.preventDefault();
2025-11-19 11:45:03 +08:00
onSearch?.(searchKeyword);
2025-11-18 15:26:36 +08:00
};
const handleItemClick = (item: MenuItem) => {
if (externalActiveKey === undefined) {
setInternalActiveKey(item.key);
}
// 如果有对应的 sectionId则滚动到该区域
2025-11-20 11:34:36 +08:00
// if (item.sectionId) {
// const element = document.getElementById(item.sectionId);
// if (element) {
// element.scrollIntoView({
// behavior: 'smooth', // 平滑滚动
// block: 'start' // 滚动到顶部对齐
// });
// }
// }
2025-11-19 11:45:03 +08:00
onItemClick?.(item.key);
2025-11-18 15:26:36 +08:00
};
return (
2025-11-19 15:53:36 +08:00
// 将组件宽度调整为1514px并保持居中
2025-11-20 16:41:36 +08:00
<div className="h-full w-full mx-auto flex items-center px-8 bg-white border-t-16 border-b-16 border-[#1f79bf]">
2025-11-18 15:26:36 +08:00
{/* 搜索框与导航菜单组合 */}
2025-11-20 17:52:29 +08:00
<div className="flex items-center justify-start gap-30 ml-45 ">
2025-11-20 10:50:06 +08:00
{/* 导航菜单 */}
2025-11-20 16:41:36 +08:00
<ul className="flex space-x-20 relative mr-10">
2025-11-18 15:26:36 +08:00
{menuItems.map((item) => {
2025-11-19 15:53:36 +08:00
const isActive = currentActiveKey === item.key;
2025-11-18 15:26:36 +08:00
return (
2025-11-19 15:53:36 +08:00
<li key={item.key} className="relative">
2025-11-18 15:26:36 +08:00
<button
onClick={() => handleItemClick(item)}
2025-11-20 10:50:06 +08:00
className={`px-4 py-2 text-2xl transition-all duration-500 relative z-10 ${isActive ? 'text-white font-bold' : 'text-gray-600 hover:font-bold cursor-pointer'
}`}
2025-11-18 15:26:36 +08:00
>
2025-11-19 15:53:36 +08:00
{/* 激活状态背景层 */}
{isActive && (
<span className="absolute inset-0 bg-gradient-to-br from-blue-500 to-blue-700 transform skew-x-12 rounded-none z-0"></span>
)}
<span className="relative z-20">{item.label}</span>
2025-11-18 15:26:36 +08:00
</button>
</li>
);
})}
</ul>
2025-11-20 16:41:36 +08:00
{/* 搜索框 */}
<div className="relative overflow-hidden bg-gray-100 ">
<Button variant="default" className="absolute rounded-none right-0 top-1/2 transform -translate-y-1/2 bg-[#1f79bf] hover:bg-[#1a68a0] text-white border-0 "
onClick={handleSearchSubmit}
>
<Search className="w-5 h-5" />
</Button>
<input type="text"
placeholder='请输入关键词'
value={searchKeyword}
onChange={(e) => setSearchKeyword(e.target.value)}
className="pl-5 pr-4 py-2 text-sm h-full w-10 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" />
</div>
2025-11-19 15:53:36 +08:00
2025-11-18 15:26:36 +08:00
</div>
</div>
);
2025-11-19 15:53:36 +08:00
};