Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from "react";

type ButtonVariant = "primary" | "white" | "disabled";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disabled는 버튼의 상태를 나타내는 속성으로 봐도 좋을 것 같습니다 !
그래서 속성값으로 넣어도 좋을 것 같아요 !

export type ButtonVariant = 'primary' | 'white';

interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  variant?: ButtonVariant;
  textSize?: Record<ButtonTextSize, string>;
  fullWidth?: boolean;
  disabled?: boolean; // disabled 속성 분리
}

type ButtonTextSize = "lg" | "md" | "sm";

interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: ButtonVariant;
textSize?: ButtonTextSize;
fullWidth?: boolean;
}

const textSizeClassMap: Record<ButtonTextSize, string> = {
lg: "text-base font-bold leading-5",
md: "text-sm font-bold leading-none",
sm: "text-xs font-normal leading-4",
};

export default function Button({
variant = "primary",
textSize = "md",
fullWidth = false,
className,
children,
...props
}: ButtonProps) {
const baseClasses = "rounded-md";

let variantClass = "";
if (variant === "primary") {
variantClass =
"bg-[#EA3C12] text-white hover:bg-[#ca3f2a] active:bg-[#aa3523]";
} else if (variant === "white") {
variantClass =
"bg-white text-[#EA3C12] border border-[#EA3C12] hover:bg-[#fff5f3] active:bg-[#ffe5e0]";
} else if (variant === "disabled") {
variantClass = "bg-gray-40 text-white cursor-not-allowed";
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variantClass의 조건부 스타일 적용도
textSizeClassMap처럼 별도로 분리하시지 않으신 이유가 있을까요? 🤔
분리하면 반복적인 If 구문을 제거할 수 있을 것 같아요! 😆

Suggested change
if (variant === "primary") {
variantClass =
"bg-[#EA3C12] text-white hover:bg-[#ca3f2a] active:bg-[#aa3523]";
} else if (variant === "white") {
variantClass =
"bg-white text-[#EA3C12] border border-[#EA3C12] hover:bg-[#fff5f3] active:bg-[#ffe5e0]";
} else if (variant === "disabled") {
variantClass = "bg-gray-40 text-white cursor-not-allowed";
}
const variantClassMap: Record<ButtonVariant, string> = {
primary: "bg-[#EA3C12] text-white hover:bg-[#ca3f2a] active:bg-[#aa3523]",
white:
"bg-white text-[#EA3C12] border border-[#EA3C12] hover:bg-[#fff5f3] active:bg-[#ffe5e0]",
disabled: "bg-gray-400 text-white cursor-not-allowed",
};
...
const finalClassName = [
  ...
  variantClassMap[variant]
 ...
] ...


const fullWidthClass = fullWidth ? "w-full" : "";
const textSizeClass = textSizeClassMap[textSize];

const finalClassName = [
baseClasses,
variantClass,
textSizeClass,
fullWidthClass,
className,
]
.filter(Boolean)
.join(" ");

return (
<button
className={finalClassName}
disabled={variant === "disabled" || props.disabled}
{...props}
>
{children}
</button>
);
}