import React from 'react'; import type { FieldValues, Path } from 'react-hook-form'; import { useController, useFormContext } from 'react-hook-form'; import type { FormFieldPropsBase } from './types'; import type { ExcludeUndefined } from 'types/utils'; import type { RadioGroupProps, RadioProps } from '@luxfi/ui/radio'; import { Radio, RadioGroup } from '@luxfi/ui/radio'; export interface FormFieldRadioProps< FormFields extends FieldValues, Name extends Path, > extends Pick, 'rules' | 'controllerProps'>, RadioGroupProps { name: Name; options: Array<{ value: ExcludeUndefined; label: string }>; itemProps?: RadioProps; } const FormFieldRadioContent = < FormFields extends FieldValues, Name extends Path, >({ name, options, itemProps, onValueChange, disabled, controllerProps, ...rest }: FormFieldRadioProps) => { const { control } = useFormContext(); const { field, formState } = useController({ control, name, ...controllerProps, }); const handleValueChange = React.useCallback( ({ value }: { value: string | null }) => { field.onChange(value); onValueChange?.({ value }); }, [ field, onValueChange ], ); return ( { options.map(({ value, label }) => ( { label } )) } ); }; export const FormFieldRadio = React.memo(FormFieldRadioContent) as typeof FormFieldRadioContent;