import React, {ComponentPropsWithoutRef, forwardRef} from 'react'; import clsx from 'clsx'; import {mergeProps, useObjectRef} from '@react-aria/utils'; import {useController} from 'react-hook-form'; import {AutoFocusProps, useAutoFocus} from '../../focus/use-auto-focus'; type RadioSize = 'xs' | 'sm' | 'md' | 'lg' | undefined; export interface RadioProps extends AutoFocusProps, Omit, 'size'> { size?: RadioSize; value: string; invalid?: boolean; isFirst?: boolean; } export const Radio = forwardRef((props, ref) => { const {children, autoFocus, size, invalid, isFirst, ...domProps} = props; const inputRef = useObjectRef(ref); useAutoFocus({autoFocus}, inputRef); const sizeClassNames = getSizeClassNames(size); return ( ); }); export function FormRadio(props: RadioProps) { const { field: {onChange, onBlur, value, ref}, fieldState: {invalid}, } = useController({ name: props.name!, }); const formProps: Partial = { onChange, onBlur, checked: props.value === value, invalid: props.invalid || invalid, }; return ; } function getSizeClassNames(size?: RadioSize): { circle: string; label: string; } { switch (size) { case 'xs': return {circle: 'h-12 w-12', label: 'text-xs'}; case 'sm': return {circle: 'h-16 w-16', label: 'text-sm'}; case 'lg': return {circle: 'h-24 w-24', label: 'text-lg'}; default: return {circle: 'h-20 w-20', label: 'text-base'}; } }