"use client" import * as React from "react" import { cn } from "@/lib/utils" interface TabsContextValue { value: string onValueChange: (value: string) => void } const TabsContext = React.createContext(undefined) interface TabsProps { defaultValue: string value?: string onValueChange?: (value: string) => void className?: string children: React.ReactNode } const Tabs = ({ defaultValue, value: controlledValue, onValueChange: controlledOnValueChange, className, children }: TabsProps) => { const [uncontrolledValue, setUncontrolledValue] = React.useState(defaultValue) const isControlled = controlledValue !== undefined const value = isControlled ? controlledValue : uncontrolledValue const onValueChange = isControlled ? controlledOnValueChange : setUncontrolledValue return ( {}) }}>
{children}
) } interface TabsListProps { className?: string children: React.ReactNode } const TabsList = React.forwardRef( ({ className, ...props }, ref) => (
) ) TabsList.displayName = "TabsList" interface TabsTriggerProps { value: string className?: string children: React.ReactNode } const TabsTrigger = React.forwardRef( ({ value, className, children, ...props }, ref) => { const context = React.useContext(TabsContext) if (!context) { throw new Error("TabsTrigger must be used within Tabs") } const isActive = context.value === value return ( ) } ) TabsTrigger.displayName = "TabsTrigger" interface TabsContentProps { value: string className?: string children: React.ReactNode } const TabsContent = React.forwardRef( ({ value, className, children, ...props }, ref) => { const context = React.useContext(TabsContext) if (!context) { throw new Error("TabsContent must be used within Tabs") } if (context.value !== value) { return null } return (
{children}
) } ) TabsContent.displayName = "TabsContent" export { Tabs, TabsList, TabsTrigger, TabsContent }