Files
portabase/src/components/common/button-with-loading.tsx
T

57 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-05-24 17:09:12 +02:00
"use client"
2025-11-01 19:38:05 +01:00
import { ButtonHTMLAttributes, ReactNode } from "react";
2025-05-16 15:54:17 +02:00
import { Loader2 } from "lucide-react";
2025-11-01 19:38:05 +01:00
import { Button } from "@/components/ui/button";
export type VariantButton = {
2025-05-16 15:54:17 +02:00
secondary: string;
default: string;
outline: string;
ghost: string;
link: string;
destructive: string;
};
2025-11-01 19:38:05 +01:00
export type SizeButton = {
2025-05-16 15:54:17 +02:00
default: string;
icon: string;
sm: string;
lg: string;
};
2025-11-01 19:38:05 +01:00
export type ButtonWithLoadingProps = {
children?: string | ReactNode;
icon?: ReactNode;
2025-05-16 15:54:17 +02:00
variant?: keyof VariantButton;
className?: string;
2026-02-10 11:44:45 +01:00
onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void;
2025-05-16 15:54:17 +02:00
isPending?: boolean;
2025-11-01 19:38:05 +01:00
size?: keyof SizeButton;
} & ButtonHTMLAttributes<HTMLButtonElement>;
2024-11-17 21:01:05 +01:00
export const ButtonWithLoading = ({
2025-11-01 19:38:05 +01:00
icon,
children,
variant = "default",
className,
onClick,
isPending,
size = "default",
...rest
}: ButtonWithLoadingProps) => {
2024-11-23 17:20:24 +01:00
return (
<Button
2026-02-10 11:44:45 +01:00
onClick={(e) => onClick?.(e)}
2025-11-01 19:38:05 +01:00
variant={variant}
2024-11-17 21:01:05 +01:00
className={className}
2025-11-01 19:38:05 +01:00
size={size}
{...rest}
>
2025-11-01 19:38:05 +01:00
{isPending && <Loader2 className="mr-2 animate-spin" size={16} />}
{children && children}
2025-05-16 15:54:17 +02:00
<>{icon ? icon : null}</>
</Button>
2025-05-16 15:54:17 +02:00
);
};