"use client"; import * as React from "react"; import { cn } from "../../lib/utils"; interface ToggleGroupContextValue { type: "single" | "multiple"; value: string | string[]; onChange: (value: string) => void; size?: "default" | "sm" | "lg"; variant?: "default" | "outline"; disabled?: boolean; } const ToggleGroupContext = React.createContext(undefined); function useToggleGroupContext() { const context = React.useContext(ToggleGroupContext); if (!context) { throw new Error("useToggleGroupContext must be used within a ToggleGroup"); } return context; } interface ToggleGroupProps { type: "single" | "multiple"; value?: string | string[]; defaultValue?: string | string[]; onValueChange?: (value: string | string[]) => void; disabled?: boolean; variant?: "default" | "outline"; size?: "default" | "sm" | "lg"; className?: string; children?: React.ReactNode; } const ToggleGroup = React.forwardRef, keyof ToggleGroupProps>>( ({ className, type, value, defaultValue, onValueChange, variant = "default", size = "default", disabled = false, children, ...props }, ref) => { // Ensure value is always of the right type (string for single, string[] for multiple) const ensureCorrectValueType = (val: string | string[] | undefined): string | string[] => { if (val === undefined) return type === "single" ? "" : []; if (type === "single") { return Array.isArray(val) ? val[0] || "" : val; } else { return Array.isArray(val) ? val : val ? [val] : []; } }; const [stateValue, setStateValue] = React.useState( ensureCorrectValueType(defaultValue) ); // Determine if the component is controlled or uncontrolled const isControlled = value !== undefined; const currentValue = isControlled ? ensureCorrectValueType(value) : stateValue; const handleValueChange = React.useCallback((itemValue: string) => { if (disabled) return; const newValue = (() => { if (type === "single") { return itemValue; } // For multiple selection // Ensure currentValue is always treated as an array for "multiple" type const values = Array.isArray(currentValue) ? currentValue : [currentValue].filter(Boolean); return values.includes(itemValue) ? values.filter((v) => v !== itemValue) : [...values, itemValue]; })(); if (!isControlled) { setStateValue(newValue); } onValueChange?.(newValue); }, [type, currentValue, disabled, isControlled, onValueChange]); return (
{children}
); } ); ToggleGroup.displayName = "ToggleGroup"; interface ToggleGroupItemProps extends React.ButtonHTMLAttributes { value: string; disabled?: boolean; variant?: "default" | "outline"; size?: "default" | "sm" | "lg"; defaultPressed?: boolean; } const ToggleGroupItem = React.forwardRef( ({ className, children, value, variant, size, disabled: itemDisabled, defaultPressed, ...props }, ref) => { const { type, value: groupValue, onChange, size: groupSize, variant: groupVariant, disabled: groupDisabled } = useToggleGroupContext(); const isActive = type === "single" ? groupValue === value : Array.isArray(groupValue) ? groupValue.includes(value) : groupValue === value; const isDisabled = groupDisabled || itemDisabled; React.useEffect(() => { // Handle defaultPressed if provided and the toggle is not already active if (defaultPressed && !isActive && !isDisabled) { onChange(value); } }, []); const handleClick = () => { onChange(value); }; return ( ); } ); ToggleGroupItem.displayName = "ToggleGroupItem"; export { ToggleGroup, ToggleGroupItem };