1 import type { ReactNode } from 'react';
3 import { type FieldProps } from 'formik';
5 import { InputFieldTwo, Radio } from '@proton/components';
6 import type { InputFieldProps } from '@proton/components/components/v2/field/InputField';
7 import clsx from '@proton/utils/clsx';
9 import { useFieldControl } from '../../../hooks/useFieldControl';
11 export type RadioValue = string | number | readonly string[];
13 type RadioGroupProps<T extends RadioValue> = {
17 onChange?: (value: T) => void;
18 options: { label: ReactNode; value: T; disabled?: boolean }[];
21 const RadioGroup = <T extends RadioValue>({ name, options, value, className, onChange }: RadioGroupProps<T>) => {
22 const handleChange = (value: T) => () => onChange?.(value);
26 {options.map((option, i) => (
30 onChange={handleChange(option.value)}
31 checked={value === option.value}
33 className={clsx(['mr-8', 'mb-2', 'flex', className])}
34 disabled={option.disabled}
43 type RadioGroupFieldProps<T extends RadioValue> = FieldProps & InputFieldProps<typeof RadioGroup<T>>;
45 export const RadioGroupField = <T extends RadioValue>({
51 }: RadioGroupFieldProps<T>) => {
52 const { error } = useFieldControl({ field, form, meta });
55 <InputFieldTwo<typeof RadioGroup<T>>
57 assistContainerClassName="empty:hidden"
61 onChange={async (value: T) => {
62 await form.setFieldValue(field.name, value);