Skip to content

Commit c3697b5

Browse files
committed
Refactor
1 parent 61d8754 commit c3697b5

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

src/components/Product/ProductFilters.component.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Dispatch, SetStateAction } from 'react';
22
import { Product, ProductType } from '@/types/product';
3+
import Button from '@/components/UI/Button.component';
34

45
interface ProductFiltersProps {
56
selectedSizes: string[];
@@ -107,17 +108,14 @@ const ProductFilters = ({
107108
<h3 className="font-semibold mb-4">STØRRELSE</h3>
108109
<div className="grid grid-cols-3 gap-2">
109110
{sizes.map((size) => (
110-
<button
111+
<Button
111112
key={size}
112-
onClick={() => toggleSize(size)}
113-
className={`px-3 py-1 border rounded ${
114-
selectedSizes.includes(size)
115-
? 'bg-gray-900 text-white'
116-
: 'hover:bg-gray-100'
117-
}`}
113+
handleButtonClick={() => toggleSize(size)}
114+
variant="filter"
115+
selected={selectedSizes.includes(size)}
118116
>
119117
{size}
120-
</button>
118+
</Button>
121119
))}
122120
</div>
123121
</div>
@@ -142,12 +140,12 @@ const ProductFilters = ({
142140
</div>
143141
</div>
144142

145-
<button
146-
onClick={resetFilters}
147-
className="w-full mt-8 py-2 px-4 bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors"
143+
<Button
144+
handleButtonClick={resetFilters}
145+
variant="reset"
148146
>
149147
Resett filter
150-
</button>
148+
</Button>
151149
</div>
152150
</div>
153151
);

src/components/UI/Button.component.tsx

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,62 @@
11
import { ReactNode } from 'react';
22
import Link from 'next/link';
33

4-
type TButtonColors = 'red' | 'blue';
4+
type TButtonVariant = 'primary' | 'secondary' | 'hero' | 'filter' | 'reset';
55

66
interface IButtonProps {
77
handleButtonClick?: () => void;
88
buttonDisabled?: boolean;
9-
color?: TButtonColors;
10-
children: ReactNode;
9+
variant?: TButtonVariant;
10+
children?: ReactNode;
1111
fullWidth?: boolean;
12-
isHero?: boolean;
1312
href?: string;
13+
title?: string;
14+
selected?: boolean;
1415
}
1516

1617
/**
1718
* Renders a clickable button
1819
* @function Button
1920
* @param {void} handleButtonClick - Handle button click
2021
* @param {boolean?} buttonDisabled - Is button disabled?
21-
* @param {TButtonColors?} color - Color for button, either red or blue
22+
* @param {TButtonVariant?} variant - Button variant
2223
* @param {ReactNode} children - Children for button
2324
* @param {boolean?} fullWidth - Whether the button should be full-width on mobile
25+
* @param {boolean?} selected - Whether the button is in a selected state
2426
* @returns {JSX.Element} - Rendered component
2527
*/
2628
const Button = ({
2729
handleButtonClick,
2830
buttonDisabled,
29-
color = 'blue',
31+
variant = 'primary',
3032
children,
3133
fullWidth = false,
32-
isHero = false,
3334
href,
35+
title,
36+
selected = false,
3437
}: IButtonProps) => {
35-
const getColorClasses = (buttonColor: TButtonColors) => {
36-
if (buttonColor === 'blue') {
37-
return 'bg-blue-500 hover:bg-blue-600';
38+
const getVariantClasses = (variant: TButtonVariant = 'primary') => {
39+
switch (variant) {
40+
case 'hero':
41+
return 'inline-block px-8 py-4 text-sm tracking-wider uppercase bg-white text-gray-900 hover:bg-gray-400 hover:text-white hover:shadow-md';
42+
case 'filter':
43+
return selected
44+
? 'px-3 py-1 border rounded bg-gray-900 text-white'
45+
: 'px-3 py-1 border rounded hover:bg-gray-100 bg-white text-gray-900';
46+
case 'reset':
47+
return 'w-full mt-8 py-2 px-4 bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors';
48+
case 'secondary':
49+
return 'px-2 lg:px-4 py-2 font-bold border border-gray-400 border-solid rounded text-white bg-red-500 hover:bg-red-600';
50+
default: // primary
51+
return 'px-2 lg:px-4 py-2 font-bold border border-gray-400 border-solid rounded text-white bg-blue-500 hover:bg-blue-600';
3852
}
39-
return 'bg-red-500 hover:bg-red-600';
4053
};
4154

42-
const buttonClasses = isHero
43-
? 'inline-block px-8 py-4 text-sm tracking-wider uppercase bg-white text-gray-900 hover:bg-gray-400 hover:text-white hover:shadow-md'
44-
: `px-2 lg:px-4 py-2 font-bold border border-gray-400 border-solid rounded text-white ${getColorClasses(color)}`;
45-
46-
const classes = `${buttonClasses} ease-in-out transition-all duration-300 disabled:opacity-50 ${
55+
const classes = `${getVariantClasses(variant)} ease-in-out transition-all duration-300 disabled:opacity-50 ${
4756
fullWidth ? 'w-full md:w-auto' : ''
4857
}`;
4958

50-
if (href && isHero) {
59+
if (href && variant === 'hero') {
5160
return (
5261
<Link href={href} className={classes}>
5362
{children}
@@ -60,6 +69,7 @@ const Button = ({
6069
onClick={handleButtonClick}
6170
disabled={buttonDisabled}
6271
className={classes}
72+
title={title}
6373
>
6474
{children}
6575
</button>

0 commit comments

Comments
 (0)