Files
buzz-sheet/components/admin/ui/delete-confirmation-dialog.tsx
T
2026-09-30 19:34:28 +07:00

77 lines
2.1 KiB
TypeScript

"use client";
import type { ComponentProps, ReactNode } from "react";
import { toast } from "sonner";
import { SimpleTooltip } from "@/components/tooltip";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
type DeleteConfirmationDialogProps = {
type?: string;
title?: ReactNode;
description: ReactNode;
onConfirm: () => void | Promise<void>;
confirmLabel?: ReactNode;
trigger?: ComponentProps<typeof AlertDialogTrigger>["render"];
children?: ReactNode;
open?: ComponentProps<typeof AlertDialog>["open"];
onOpenChange?: ComponentProps<typeof AlertDialog>["onOpenChange"];
showSuccessToast?: boolean;
triggerTooltip?: ReactNode;
};
export function DeleteConfirmationDialog({
type,
title,
description,
onConfirm,
confirmLabel = "ยืนยันลบ",
trigger,
children,
open,
onOpenChange,
showSuccessToast = true,
triggerTooltip,
}: DeleteConfirmationDialogProps) {
const handleConfirm = async () => {
await onConfirm();
if (showSuccessToast) toast.success("ลบสำเร็จ!");
};
return (
<AlertDialog open={open} onOpenChange={onOpenChange}>
{trigger ? (
triggerTooltip ? (
<SimpleTooltip text={triggerTooltip}>
<AlertDialogTrigger render={trigger}>{children}</AlertDialogTrigger>
</SimpleTooltip>
) : (
<AlertDialogTrigger render={trigger}>{children}</AlertDialogTrigger>
)
) : null}
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{type ? `Are you sure to delete ${type}?` : title}</AlertDialogTitle>
<AlertDialogDescription>{description}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>ยกเลิก</AlertDialogCancel>
<AlertDialogAction variant="destructive" onClick={handleConfirm}>
{confirmLabel}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}