import { Search } from 'lucide-react'; import React, { useState, useEffect } from 'react'; import { Button } from '@/ui/button'; interface MenuItem { label: string; key: string; sectionId:string; } 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' }, ], activeKey: externalActiveKey, onSearch, onItemClick, }: TopNavProps) { const [internalActiveKey, setInternalActiveKey] = useState('home'); const currentActiveKey = externalActiveKey !== undefined ? externalActiveKey : internalActiveKey; const [searchKeyword, setSearchKeyword] = useState(''); const handleSearchSubmit = (e: React.FormEvent) => { e.preventDefault(); onSearch?.(searchKeyword); }; const handleItemClick = (item: MenuItem) => { if (externalActiveKey === undefined) { setInternalActiveKey(item.key); } // 如果有对应的 sectionId,则滚动到该区域 // if (item.sectionId) { // const element = document.getElementById(item.sectionId); // if (element) { // element.scrollIntoView({ // behavior: 'smooth', // 平滑滚动 // block: 'start' // 滚动到顶部对齐 // }); // } // } onItemClick?.(item.key); }; return ( // 将组件宽度调整为1514px,并保持居中
{/* 搜索框与导航菜单组合 */}
{/* 导航菜单 */} {/* 搜索框 */}
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" />
); };