import * as React from 'react'; import { TabContent, TabTitle, TabTitleRow, TabViewStyled } from './TabView.styled'; export type TabViewProps = { children: React.ReactNode[]; initialTab?: number; }; type TabProps = { title: string; children: () => React.ReactNode; }; export const TabView = (props: TabViewProps) => { const tabNames = React.Children.map(props.children, (child) => { if (!React.isValidElement(child)) { return '(Oops)'; } return child.props.title; }) as string[]; if (tabNames.length === 0) { return null; } const [currentTab, setCurrentTab] = React.useState(props.initialTab ?? 0); return ( {tabNames.map((tabName, idx) => ( setCurrentTab(idx)} key={`tab${tabName}${idx}`} > {tabName} ))} {props.children[currentTab] || ( setCurrentTab(0)}> Tabs were misconfigured, resetting to zero. )} ); }; export const Tab = (props: TabProps) =>
{props.children()}
;