import React, { createContext, useContext, useState, ReactNode } from 'react'; import { cn } from '../lib/utils'; interface TabsContextType { value: string; onValueChange: (value: string) => void; } const TabsContext = createContext(undefined); export interface TabsProps { defaultValue?: string; value?: string; onValueChange?: (value: string) => void; children: ReactNode; className?: string; } export function Tabs({ defaultValue, value: controlledValue, onValueChange, children, className }: TabsProps) { const [internalValue, setInternalValue] = useState(defaultValue || ''); const isControlled = controlledValue !== undefined; const currentValue = isControlled ? controlledValue : internalValue; const handleValueChange = (newValue: string) => { if (!isControlled) { setInternalValue(newValue); } onValueChange?.(newValue); }; return (
{children}
); } export interface TabsListProps { children: ReactNode; className?: string; } export function TabsList({ children, className }: TabsListProps) { return (
{children}
); } export interface TabsTriggerProps { value: string; children: ReactNode; className?: string; } export function TabsTrigger({ value, children, className }: TabsTriggerProps) { const context = useContext(TabsContext); if (!context) { throw new Error('TabsTrigger must be used within Tabs'); } const { value: currentValue, onValueChange } = context; const isActive = currentValue === value; return ( ); } export interface TabsContentProps { value: string; children: ReactNode; className?: string; } export function TabsContent({ value, children, className }: TabsContentProps) { const context = useContext(TabsContext); if (!context) { throw new Error('TabsContent must be used within Tabs'); } const { value: currentValue } = context; if (currentValue !== value) return null; return (
{children}
); }