2024-06-18 06:50:33 +02:00
|
|
|
import clsx from "clsx";
|
2024-09-17 07:23:05 +02:00
|
|
|
import { Children, isValidElement, memo } from "react";
|
2024-05-15 01:53:03 +02:00
|
|
|
import "./button.less";
|
|
|
|
|
2024-06-18 06:50:33 +02:00
|
|
|
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
2024-08-07 01:48:25 +02:00
|
|
|
forwardedRef?: React.RefObject<HTMLButtonElement>;
|
2024-05-15 01:53:03 +02:00
|
|
|
className?: string;
|
2024-07-18 08:41:33 +02:00
|
|
|
children?: React.ReactNode;
|
2024-05-15 01:53:03 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 07:23:05 +02:00
|
|
|
const Button = memo(({ className = "", children, disabled, ...props }: ButtonProps) => {
|
|
|
|
const hasIcon = Children.toArray(children).some(
|
|
|
|
(child) => isValidElement(child) && (child as React.ReactElement).type === "svg"
|
2024-06-18 06:50:33 +02:00
|
|
|
);
|
|
|
|
|
2024-09-17 07:23:05 +02:00
|
|
|
// Check if the className contains any of the categories: solid, outlined, or ghost
|
|
|
|
const containsButtonCategory = /(solid|outline|ghost)/.test(className);
|
|
|
|
// If no category is present, default to 'solid'
|
|
|
|
const categoryClassName = containsButtonCategory ? className : `solid ${className}`;
|
|
|
|
|
|
|
|
// Check if the className contains any of the color options: green, grey, red, or yellow
|
|
|
|
const containsColor = /(green|grey|red|yellow)/.test(categoryClassName);
|
|
|
|
// If no color is present, default to 'green'
|
|
|
|
const finalClassName = containsColor ? categoryClassName : `green ${categoryClassName}`;
|
|
|
|
|
2024-06-18 06:50:33 +02:00
|
|
|
return (
|
2024-09-13 07:22:31 +02:00
|
|
|
<button
|
|
|
|
tabIndex={disabled ? -1 : 0}
|
2024-09-17 07:23:05 +02:00
|
|
|
className={clsx("button", finalClassName, {
|
2024-09-13 07:22:31 +02:00
|
|
|
disabled,
|
|
|
|
hasIcon,
|
|
|
|
})}
|
|
|
|
disabled={disabled}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</button>
|
2024-06-18 06:50:33 +02:00
|
|
|
);
|
2024-06-26 18:31:43 +02:00
|
|
|
});
|
2024-05-15 01:53:03 +02:00
|
|
|
|
|
|
|
export { Button };
|