-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCategoryButton.tsx
60 lines (52 loc) · 1.49 KB
/
CategoryButton.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
50
51
52
53
54
55
56
57
58
59
60
"use client";
import React, { useRef, useEffect } from "react";
import { FieldValues, UseFormWatch } from "react-hook-form";
import { cn } from "@/utils/helper";
import { Category } from "@/types";
interface CategoryButtonProps extends Category {
onClick: (fieldName: string, value: string) => void;
watch: UseFormWatch<FieldValues>;
}
const CategoryButton: React.FC<CategoryButtonProps> = ({
icon: Icon,
label,
color,
onClick,
watch,
}) => {
const isSelected = watch("category") === label;
const buttonRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
if (!buttonRef.current) return;
const timer = setTimeout(() => {
if(isSelected){
buttonRef.current?.focus();
}
}, 300);
return () => clearTimeout(timer);
}, [isSelected]);
const handleChange = () => {
if(isSelected) return;
onClick("category", label);
};
return (
<div className="col-span-1">
<button
ref={buttonRef}
type="button"
onClick={handleChange}
className={cn(
`rounded-xl border-2 p-2 flex flex-col gap-3 hover:border-black cursor-pointer transition duration-200 w-full`,
isSelected ? "border-black" : "border-neutral-200"
)}
onFocus={handleChange}
>
<Icon size={30} color={color} />
<div className="font-semibold">
{label}
</div>
</button>
</div>
);
};
export default CategoryButton;