waveterm/frontend/app/element/button.tsx

42 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-06-18 06:50:33 +02:00
import clsx from "clsx";
import { Children, isValidElement, memo } from "react";
import "./button.less";
2024-06-18 06:50:33 +02:00
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
forwardedRef?: React.RefObject<HTMLButtonElement>;
className?: string;
2024-07-18 08:41:33 +02:00
children?: React.ReactNode;
}
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
);
// 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}
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
});
export { Button };