import { useRef, useState } from "react"; import "./Tabs.css"; interface Tab { id: string; label: string; content: React.ReactNode; } interface TabsProps { tabs: Tab[]; defaultTab?: string; onChange?: (id: string) => void; className?: string; } const Tabs = ({ tabs, defaultTab, onChange, className }: TabsProps) => { const [activeTab, setActiveTab] = useState(defaultTab ?? tabs[0]?.id); const tabRefs = useRef>({}); const handleSelect = (id: string) => { setActiveTab(id); onChange?.(id); }; const handleKeyDown = ( e: React.KeyboardEvent, index: number, ) => { let nextIndex: number | null = null; if (e.key === "ArrowRight") { nextIndex = (index + 1) % tabs.length; } else if (e.key === "ArrowLeft") { nextIndex = (index - 1 + tabs.length) % tabs.length; } if (nextIndex !== null) { e.preventDefault(); const nextId = tabs[nextIndex].id; handleSelect(nextId); tabRefs.current[nextId]?.focus(); } }; const wrapperClass = `tabs${className ? ` ${className}` : ""}`; return (
{tabs.map(({ id, label }, index) => ( ))}
{tabs.map(({ id, content }) => ( ))}
); }; export default Tabs;