forked from gitroomhq/postiz-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.tsx
49 lines (45 loc) Β· 1.55 KB
/
checkbox.tsx
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
'use client';
import { FC, forwardRef, useCallback, useState } from 'react';
import clsx from 'clsx';
import Image from 'next/image';
import { useFormContext, useWatch } from 'react-hook-form';
export const Checkbox = forwardRef<null, {
checked?: boolean;
disableForm?: boolean;
name?: string;
className?: string;
onChange?: (event: { target: { name?: string, value: boolean } }) => void;
variant?: 'default' | 'hollow';
}>((props, ref: any) => {
const { checked, className, disableForm, variant } = props;
const form = useFormContext();
const register = disableForm ? {} : form.register(props.name!);
const watch = disableForm ? undefined : useWatch({
name: props.name!,
});
const [currentStatus, setCurrentStatus] = useState(watch || checked);
const changeStatus = useCallback(() => {
setCurrentStatus(!currentStatus);
props?.onChange?.({ target: { name: props.name!, value: !currentStatus } });
if (!disableForm) {
// @ts-ignore
register?.onChange?.({ target: { name: props.name!, value: !currentStatus } });
}
}, [currentStatus]);
return (
<div
ref={ref}
{...register}
onClick={changeStatus}
className={clsx(
'cursor-pointer rounded-[4px] select-none w-[24px] h-[24px] justify-center items-center flex',
variant === 'default' || !variant ? ('bg-forth') : ('border-customColor1 border-2 bg-customColor2'),
className
)}
>
{currentStatus && (
<Image src="/form/checked.svg" alt="Checked" width={20} height={20} />
)}
</div>
);
});