import { Loader2 } from "lucide-react"; import { Button } from "./ui/button"; import React from "react"; import { cn } from "@/lib/utils"; import { ButtonProps } from "./ui/button"; interface ActionButtonProps extends ButtonProps { isLoading?: boolean; isSuccess?: boolean; error?: any; className?: string; children?: React.ReactNode; // Allow custom subcomponents as children } const ActionButtonDefault = React.forwardRef< HTMLSpanElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => ( {children ?? "Submit"} )); const ActionButtonLoading = React.forwardRef< HTMLSpanElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => ( {children ?? ( <> Loading... )} )); const ActionButtonSuccess = React.forwardRef< HTMLSpanElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => ( {children ?? <>Success!} )); const ActionButtonError = React.forwardRef< HTMLSpanElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => ( {children ?? <>Error occurred.} )); const ActionButton = React.forwardRef( ( { variant, isLoading, isSuccess, error, className, children, ...props }, ref, ) => { let buttonContent = null; React.Children.forEach(children, (child) => { if (React.isValidElement(child)) { if (child.type === ActionButtonLoading && isLoading) { buttonContent = child; } else if (child.type === ActionButtonSuccess && isSuccess) { buttonContent = child; } else if (child.type === ActionButtonError && error) { buttonContent = child; } else if ( child.type === ActionButtonDefault && !isLoading && !isSuccess && !error ) { buttonContent = child; } } }); return ( ); }, ); export { ActionButton, ActionButtonLoading, ActionButtonError, ActionButtonSuccess, ActionButtonDefault, };