Skip to content

Commit 8a7521c

Browse files
committed
Reusable checkbox
1 parent c3697b5 commit 8a7521c

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/components/Product/ProductFilters.component.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Dispatch, SetStateAction } from 'react';
1+
import { Dispatch, SetStateAction, ChangeEvent } from 'react';
22
import { Product, ProductType } from '@/types/product';
33
import Button from '@/components/UI/Button.component';
4+
import Checkbox from '@/components/UI/Checkbox.component';
45

56
interface ProductFiltersProps {
67
selectedSizes: string[];
@@ -71,15 +72,13 @@ const ProductFilters = ({
7172
<h3 className="font-semibold mb-4">PRODUKT TYPE</h3>
7273
<div className="space-y-2">
7374
{productTypes.map((type) => (
74-
<label key={type.id} className="flex items-center">
75-
<input
76-
type="checkbox"
77-
className="form-checkbox"
78-
checked={type.checked}
79-
onChange={() => toggleProductType(type.id)}
80-
/>
81-
<span className="ml-2">{type.name}</span>
82-
</label>
75+
<Checkbox
76+
key={type.id}
77+
id={type.id}
78+
label={type.name}
79+
checked={type.checked}
80+
onChange={() => toggleProductType(type.id)}
81+
/>
8382
))}
8483
</div>
8584
</div>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ChangeEvent } from 'react';
2+
3+
interface ICheckboxProps {
4+
id: string;
5+
label: string;
6+
checked: boolean;
7+
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
8+
}
9+
10+
/**
11+
* A reusable checkbox component with a label
12+
* @function Checkbox
13+
* @param {string} id - Unique identifier for the checkbox
14+
* @param {string} label - Label text to display next to the checkbox
15+
* @param {boolean} checked - Whether the checkbox is checked
16+
* @param {function} onChange - Handler for when the checkbox state changes
17+
* @returns {JSX.Element} - Rendered component
18+
*/
19+
const Checkbox = ({ id, label, checked, onChange }: ICheckboxProps) => {
20+
return (
21+
<label htmlFor={id} className="flex items-center py-2 cursor-pointer">
22+
<input
23+
id={id}
24+
type="checkbox"
25+
className="form-checkbox h-5 w-5 cursor-pointer"
26+
checked={checked}
27+
onChange={onChange}
28+
/>
29+
<span className="ml-3 text-base">{label}</span>
30+
</label>
31+
);
32+
};
33+
34+
export default Checkbox;

0 commit comments

Comments
 (0)