import classNames from 'classnames'; import { useRef } from 'react'; import { useStateWithDeps } from '../../hooks/useStateWithDeps'; import type { IconProps } from './Icon'; import { IconButton } from './IconButton'; import { HStack } from './Stacks'; interface Props { options: { value: T; label: string; icon: IconProps['icon'] }[]; onChange: (value: T) => void; value: T; name: string; } export function SegmentedControl({ value, onChange, options }: Props) { const [selectedValue, setSelectedValue] = useStateWithDeps(value, [value]); const containerRef = useRef(null); return ( { const selectedIndex = options.findIndex((o) => o.value === selectedValue); if (e.key === 'ArrowRight') { const newIndex = Math.abs((selectedIndex + 1) % options.length); setSelectedValue(options[newIndex]!.value); const child = containerRef.current?.children[newIndex] as HTMLButtonElement; child.focus(); } else if (e.key === 'ArrowLeft') { const newIndex = Math.abs((selectedIndex - 1) % options.length); setSelectedValue(options[newIndex]!.value); const child = containerRef.current?.children[newIndex] as HTMLButtonElement; child.focus(); } }} > {options.map((o, i) => { const isSelected = selectedValue === o.value; const isActive = value === o.value; return ( onChange(o.value)} /> ); })} ); }