import * as RadioGroupPrimitive from "@radix-ui/react-radio-group" import * as React from "react" import { cn } from "../libs/utils" function RadioGroupItem({ className, ...props }: React.ComponentProps) { return ( ) } interface RadioGroupOption { id: string label: string description?: React.ReactNode } export abstract class RadioGroupAdapter { abstract idOf(item: T): string abstract labelOf(item: T): string descriptionOf(_item: T): React.ReactNode { return undefined } selectedClassName(_item: T): string { return 'border-accent-foreground' } } interface RadioGroupProps { options: T[] selected?: T onSelect: (option: T) => void adapter?: RadioGroupAdapter } function RadioGroup({ onSelect, selected, options, adapter }: RadioGroupProps) { const getId = (o: T) => adapter ? adapter.idOf(o) : (o as unknown as RadioGroupOption).id const getLabel = (o: T) => adapter ? adapter.labelOf(o) : (o as unknown as RadioGroupOption).label const getDescription = (o: T) => adapter ? adapter.descriptionOf(o) : (o as unknown as RadioGroupOption).description const getSelectedClass = (o: T) => adapter ? adapter.selectedClassName(o) : 'border-primary' return ( { const option = options.find((o) => getId(o) === id) if (option) onSelect(option) }} className="space-y-4" > {options.map((option) => { const isSelected = selected ? getId(selected) === getId(option) : false return ( {getLabel(option)} {getDescription(option) && ( {getDescription(option)} )} ) })} ) } /** @deprecated use RadioGroupAdapter */ export const RadioOptionAdapter = RadioGroupAdapter export { RadioGroup, RadioGroupItem } export type { RadioGroupOption, RadioGroupProps }