File size: 1,769 Bytes
41a71fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { ReactNode } from 'react';
import { Controller, FieldValues, RegisterOptions, useFormContext } from 'react-hook-form';
import { classNames } from '@/shared/lib/classNames/classNames';
import { Cell } from '@/shared/ui/FormComponents/fieldsUI/Cell/Cell';
import { IRadioButtons, RadioButtons } from '@/shared/ui/FormComponents/fieldsUI/RadioButtons/RadioButtons';
import cls from './HRadioButtons.module.scss';

interface HRadioButtonsProps {
    className?: string;
    name: string;
    title?: string;
    label?: string;
    noteText?: ReactNode;
    radioData: IRadioButtons[];
    theme?: string;
    registerOptions?: Omit<RegisterOptions<FieldValues, string>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
}

export const HRadioButtons = (props: HRadioButtonsProps) => {
    const { className, name, title, label, noteText, radioData, theme, registerOptions } = props;
    const { control } = useFormContext();

    return (
        <Controller
            name={name}
            control={control}
            rules={registerOptions}
            render={({ field, fieldState }) => (
                <Cell
                    className={classNames(cls.HRadioButtons, {}, [className])}
                    label={label}
                    noteText={noteText}
                    fieldError={fieldState.error}
                    withoutBorder
                >
                    <RadioButtons
                        name={field.name}
                        title={title}
                        onChange={field.onChange}
                        currentValue={field.value}
                        data={radioData}
                        theme={theme}
                    />
                </Cell>
            )}
        />
    );
};