import React, { useEffect, useRef, useState } from 'react'; import { cx } from '../../cx'; import './Tabs.css'; export type TabProps = { children: React.ReactNode; title: string; }; const getTabId = (index: number, suffix?: string) => [`tab-${index}`, suffix].filter(Boolean).join('-'); export function Tabs({ children }) { const firstRender = useRef(true); const [currentTab, setCurrentTab] = useState(0); const tabsRefs = useRef([]); useEffect(() => { if (!firstRender.current && tabsRefs.current) { tabsRefs.current[currentTab].focus(); } }, [currentTab]); useEffect(() => { firstRender.current = false; }, []); const onKeyDown = ({ key }: React.KeyboardEvent) => { if (key === 'ArrowLeft') { setCurrentTab(Math.max(0, currentTab - 1)); } else if (key === 'ArrowRight') { setCurrentTab( Math.min(currentTab + 1, React.Children.count(children) - 1) ); } }; return (
{React.Children.map>( children, (child, index) => { const isSelected = currentTab === index; return ( ); } )}
{React.Children.map(children, (child, index) => ( ))}
); } export function Tab({ children }: TabProps) { return <>{children}; }