import { FormControlLabel, FormGroup, FormLabel, Radio as MUIRadio, RadioGroup } from '@mui/material'; import { type FormikValues, useFormikContext } from 'formik'; import { equals, includes, path, split, type } from 'ramda'; import { useTranslation } from 'react-i18next'; import { useMemoComponent } from '../..'; import { getNormalizedId } from '../../utils'; import type { InputPropsWithoutGroup } from './models'; const Radio = ({ dataTestId, fieldName, label, radio, getDisabled, change, additionalMemoProps }: InputPropsWithoutGroup): JSX.Element => { const { t } = useTranslation(); const { values, setFieldValue, setFieldTouched, setValues, setTouched } = useFormikContext(); const changeRadio = ( _: React.ChangeEvent, value: string ): void => { if (includes(value, ['true', 'false'])) { if (change) { change({ setFieldTouched, setFieldValue, setTouched, setValues, value: equals(value, 'true'), values }); return; } setFieldValue(fieldName, equals(value, 'true')); return; } if (change) { change({ setFieldTouched, setFieldValue, setValues, value, values } as Parameters>[0]); return; } setFieldValue(fieldName, value); }; const fieldNamePath = split('.', fieldName); const value = path(fieldNamePath, values); const disabled = getDisabled?.(values) || false; return useMemoComponent({ Component: ( {t(label)} {radio?.options?.map(({ value: optionValue, label: optionLabel }) => ( } key={ equals(type(optionLabel), 'String') ? (optionLabel as string) : `${optionValue}` } label={ equals(type(optionLabel), 'String') ? (t(optionLabel as string) as string) : optionLabel } value={optionValue} /> ))} ), memoProps: [value, disabled, additionalMemoProps] }); }; export default Radio;