import clsx from 'clsx'; import { type ReactNode, createContext, forwardRef, useContext } from 'react'; export interface TabsContextValue { activeKey: string onChange: (key: string) => void } const TabsContext = createContext({ activeKey: '', onChange: () => {}, }); export interface TabsProps { activeKey: string onChange: (key: string) => void children: ReactNode className?: string } export function Tabs ({ activeKey, onChange, children, className }: TabsProps) { return (
{children}
); } export interface TabListProps { children: ReactNode className?: string } export function TabList ({ children, className }: TabListProps) { return (
{children}
); } export interface TabProps extends React.ButtonHTMLAttributes { value: string className?: string children: ReactNode isSelected?: boolean } export const Tab = forwardRef( ({ className, isSelected, value, ...props }, ref) => { const { onChange } = useContext(TabsContext); const handleClick = (e: React.MouseEvent) => { onChange(value); props.onClick?.(e); }; return (
); } ); Tab.displayName = 'Tab'; export interface TabPanelProps { value: string children: ReactNode className?: string } export function TabPanel ({ value, children, className }: TabPanelProps) { const { activeKey } = useContext(TabsContext); if (value !== activeKey) return null; return
{children}
; }