import type { KeyboardEvent, ReactNode } from "react"; import { useRef } from "react"; import { cn, focusRing, segmentedClass, transitionUi } from "@/lib/ui/variants"; export interface SegmentOption { value: T; label: ReactNode; } interface SegmentedControlProps { options: Array>; value: T; onChange: (value: T) => void; ariaLabel: string; className?: string; } export function SegmentedControl({ options, value, onChange, ariaLabel, className, }: SegmentedControlProps) { const buttonsRef = useRef>([]); const focusOption = (index: number) => { const clamped = (index + options.length) % options.length; const target = options[clamped]; if (!target) return; onChange(target.value); buttonsRef.current[clamped]?.focus(); }; const handleKeyDown = (event: KeyboardEvent, index: number) => { switch (event.key) { case "ArrowRight": case "ArrowDown": event.preventDefault(); focusOption(index + 1); break; case "ArrowLeft": case "ArrowUp": event.preventDefault(); focusOption(index - 1); break; case "Home": event.preventDefault(); focusOption(0); break; case "End": event.preventDefault(); focusOption(options.length - 1); break; default: break; } }; return (
{options.map((option, index) => { const selected = option.value === value; return ( ); })}
); }