|
| 1 | +import React, { ReactNode } from "react"; |
| 2 | +import { |
| 3 | + Radio as AriaRadio, |
| 4 | + RadioGroup as AriaRadioGroup, |
| 5 | + FieldError, |
| 6 | + type FieldErrorProps, |
| 7 | + Label, |
| 8 | + type RadioGroupProps as AriaRadioGroupProps, |
| 9 | + type RadioProps as AriaRadioProps, |
| 10 | + type RadioRenderProps, |
| 11 | +} from "react-aria-components"; |
| 12 | +import { cn } from "../../utils"; |
| 13 | +import { RadioIndicator } from "./radio-indicator"; |
| 14 | + |
| 15 | +export { RadioIndicator }; |
| 16 | + |
| 17 | +interface CustomRadioItemProps extends Omit<AriaRadioProps, "className"> { |
| 18 | + className?: string; |
| 19 | +} |
| 20 | + |
| 21 | +/** A single radio whose content is fully owned by the caller. Use the render-prop |
| 22 | + * `children` to receive `{ isSelected, isHovered, isFocusVisible, ... }` and compose your |
| 23 | + * own UI (cards, labels, ...), placing a `<RadioIndicator>` where you want it. Renders a |
| 24 | + * `<label>`, so never nest interactive controls inside it — render adornments (a help |
| 25 | + * tooltip) and conditional content (a field) as siblings within the `<CustomRadio>`. */ |
| 26 | +export function CustomRadioItem({ |
| 27 | + className, |
| 28 | + children, |
| 29 | + ...props |
| 30 | +}: Readonly<CustomRadioItemProps>) { |
| 31 | + return ( |
| 32 | + <AriaRadio |
| 33 | + {...props} |
| 34 | + className={cn( |
| 35 | + "relative box-border block cursor-pointer", |
| 36 | + "text-klerosUIComponentsPrimaryText disabled:text-klerosUIComponentsStroke disabled:cursor-default", |
| 37 | + className, |
| 38 | + )} |
| 39 | + > |
| 40 | + {children} |
| 41 | + </AriaRadio> |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +export interface CustomRadioOption |
| 46 | + extends Omit<AriaRadioProps, "children" | "className"> { |
| 47 | + /** Custom content for this option. A function receives the react-aria render props. */ |
| 48 | + content: ReactNode | ((renderProps: RadioRenderProps) => ReactNode); |
| 49 | + className?: string; |
| 50 | +} |
| 51 | + |
| 52 | +export interface CustomRadioProps |
| 53 | + extends Omit<AriaRadioGroupProps, "children" | "className"> { |
| 54 | + /** Convenience API for simple option lists. For per-row adornments or content |
| 55 | + * interleaved between options, use `children` (compose `<CustomRadioItem>`s) instead. */ |
| 56 | + items?: CustomRadioOption[]; |
| 57 | + children?: ReactNode; |
| 58 | + /** Group label rendered above the options. */ |
| 59 | + groupLabel?: string; |
| 60 | + className?: string; |
| 61 | + /** Props for field error display. |
| 62 | + * [See FieldErrorProps](https://react-spectrum.adobe.com/react-aria/RadioGroup.html#fielderror) */ |
| 63 | + fieldErrorProps?: FieldErrorProps; |
| 64 | +} |
| 65 | + |
| 66 | +/** A radio group whose options render arbitrary content (cards, tooltip-wrapped labels, ...) |
| 67 | + * while keeping react-aria's `RadioGroup` semantics (single-select, roving tab-index, |
| 68 | + * keyboard navigation, `role="radiogroup"`). Pass `items` for simple lists, or `children` |
| 69 | + * (compose `<CustomRadioItem>`s, with adornments/fields as siblings) for richer layouts. |
| 70 | + * For the plain label-only case, use `Radio` (the `options`-based group) instead. |
| 71 | + * [Extends AriaRadioGroupProps](https://react-spectrum.adobe.com/react-aria/RadioGroup.html#radiogroup-1) */ |
| 72 | +function CustomRadio({ |
| 73 | + groupLabel, |
| 74 | + className, |
| 75 | + fieldErrorProps, |
| 76 | + items, |
| 77 | + children, |
| 78 | + ...props |
| 79 | +}: Readonly<CustomRadioProps>) { |
| 80 | + return ( |
| 81 | + <AriaRadioGroup |
| 82 | + {...props} |
| 83 | + className={cn( |
| 84 | + "relative flex flex-col gap-2", |
| 85 | + "orientation-horizontal:flex-row orientation-horizontal:items-center orientation-horizontal:gap-4", |
| 86 | + className, |
| 87 | + )} |
| 88 | + > |
| 89 | + {groupLabel && ( |
| 90 | + <Label className="text-klerosUIComponentsSecondaryText text-base"> |
| 91 | + {groupLabel} |
| 92 | + </Label> |
| 93 | + )} |
| 94 | + {children ?? |
| 95 | + items?.map(({ content, className: itemClassName, ...item }) => ( |
| 96 | + <CustomRadioItem |
| 97 | + key={String(item.value)} |
| 98 | + {...item} |
| 99 | + className={itemClassName} |
| 100 | + > |
| 101 | + {content} |
| 102 | + </CustomRadioItem> |
| 103 | + ))} |
| 104 | + <FieldError |
| 105 | + {...fieldErrorProps} |
| 106 | + className={cn( |
| 107 | + "text-klerosUIComponentsError self-end text-sm", |
| 108 | + fieldErrorProps?.className, |
| 109 | + )} |
| 110 | + /> |
| 111 | + </AriaRadioGroup> |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +export default CustomRadio; |
0 commit comments