File size: 1,607 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
import { ReactNode } from 'react';
import { Controller, FieldValues, RegisterOptions, useFormContext } from 'react-hook-form';
import { classNames } from '@/shared/lib/classNames/classNames';
import cls from './HCheckbox.module.scss';
import { Cell } from '@/shared/ui/FormComponents/fieldsUI/Cell/Cell';
import { Checkbox } from '@/shared/ui/FormComponents/fieldsUI/Checkbox/Checkbox';

interface HCheckboxProps {
    className?: string;
    name: string;
    label?: string;
    children?: ReactNode;
    noteText?: ReactNode;
    registerOptions?: Omit<RegisterOptions<FieldValues, string>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
}

export const HCheckbox = (props: HCheckboxProps) => {
    const { className, name, label, children, noteText, 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
                >
                    <Checkbox //
                        name={field.name}
                        onChange={field.onChange}
                        inputRef={field.ref}
                    >
                        {children}
                    </Checkbox>
                </Cell>
            )}
        />
    );
};