import clsx from 'clsx' import { ReactNode } from 'react' import { FieldValues, Path, PathValue, UnpackNestedValue, UseFormSetValue, UseFormWatch, } from 'react-hook-form' export type RadioInputOption = { value: Value } & ({ label: string } | { display: ReactNode }) export type RadioInputNoFormProps = { options: RadioInputOption[] selected?: Value onChange: (value: Value) => void className?: string disabled?: boolean loading?: boolean } export const RadioInputNoForm = ({ options, selected, onChange, className, disabled, loading, }: RadioInputNoFormProps) => (
{(loading ? [...new Array(3)].map(() => ({ // value won't be used when loading // eslint-disable-next-line i18next/no-literal-string value: '' as any, display:
, })) : options ).map(({ value, ...labelOrDisplay }, index) => ( onChange(value)} selected={!loading && selected === value} {...labelOrDisplay} /> ))}
) export type RadioInputProps< FV extends FieldValues, FieldName extends Path, > = Omit< RadioInputNoFormProps>>, 'selected' | 'onChange' > & Partial< Pick< RadioInputNoFormProps>>, 'onChange' > > & { fieldName: FieldName watch: UseFormWatch setValue: UseFormSetValue } export const RadioInput = >({ fieldName, watch, setValue, onChange, ...props }: RadioInputProps) => { const selected = watch(fieldName) return ( { setValue(fieldName, value) onChange?.(value) }} selected={selected} {...props} /> ) } export type RadioButtonProps = { selected: boolean onClick?: () => void label?: string background?: boolean className?: string display?: ReactNode disabled?: boolean } export const RadioButton = ({ selected, onClick, label, background, className, display, disabled, }: RadioButtonProps) => (
{!!label &&

{label}

} {display}
)