"use client"; /** * A small segmented choice (zoom, chronicle grouping, life-event precision): * an ARIA radiogroup with one tab stop and arrow keys, through the shared * roving helper. Neutral palette; the chosen segment is raised, not coloured. * * Inside a form sheet it renders as toggle buttons (`aria-pressed`) instead: * the sheets' discard guard reads any checked radio as the person's input, * and a precision that starts on "Day" is a default, not an answer. */ import { Fragment, type ReactElement, type ReactNode } from "react"; import { useRovingRadioGroup } from "@/hooks/use-roving-radio-group"; import { cn } from "@/lib/utils"; export interface SegmentedOption { value: T; label: string; } export function Segmented({ options, value, onChange, label, slot, className, stretch = false, asToggleButtons = false, wrapOption, }: { options: ReadonlyArray>; value: T; onChange: (next: T) => void; label: string; slot: string; className?: string; stretch?: boolean; asToggleButtons?: boolean; /** * Wraps one segment's button, for a popover anchored to it (the range * segment). Returns the button itself for every other segment. */ wrapOption?: (value: T, button: ReactElement) => ReactNode; }) { const selectedIndex = options.findIndex((o) => o.value === value); const { getRadioProps } = useRovingRadioGroup({ count: options.length, selectedIndex, onSelect: (index) => onChange(options[index].value), }); return (
{options.map((option, index) => { const checked = option.value === value; const button = ( ); return wrapOption ? ( {wrapOption(option.value, button)} ) : ( button ); })}
); }