Uecko_ERP/packages/rdx-ui/src/components/custom-dialog.tsx

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-05-05 17:11:44 +00:00
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@repo/shadcn-ui/components";
import { useRef } from "react";
2025-05-05 17:11:44 +00:00
interface CustomDialogProps {
2025-09-23 08:59:15 +00:00
open: boolean;
onConfirm: (ok: boolean) => void;
title?: string;
description?: string;
cancelLabel?: string;
confirmLabel?: string;
2025-05-05 17:11:44 +00:00
}
export const CustomDialog = ({
2025-09-23 08:59:15 +00:00
open,
2025-05-05 17:11:44 +00:00
onConfirm,
title,
description,
cancelLabel,
confirmLabel,
}: CustomDialogProps) => {
const closedByActionRef = useRef(false);
2025-10-23 17:29:52 +00:00
const handleClose = (ok: boolean) => {
closedByActionRef.current = true;
2025-10-23 17:29:52 +00:00
onConfirm(ok);
};
2025-05-05 17:11:44 +00:00
return (
2025-10-23 17:29:52 +00:00
<AlertDialog
onOpenChange={(nextOpen) => {
if (nextOpen) {
closedByActionRef.current = false;
return;
}
if (!closedByActionRef.current) {
onConfirm(false);
}
2025-10-23 17:29:52 +00:00
}}
2025-11-19 16:05:01 +00:00
open={open}
2025-10-23 17:29:52 +00:00
>
<AlertDialogContent className="max-w-md rounded-2xl border border-border shadow-lg">
<AlertDialogHeader className="space-y-2">
<AlertDialogTitle className="text-lg font-semibold">{title}</AlertDialogTitle>
2025-11-19 16:05:01 +00:00
<AlertDialogDescription aria-live="assertive" className="text-sm text-muted-foreground">
2025-10-23 17:29:52 +00:00
{description}
</AlertDialogDescription>
2025-05-05 17:11:44 +00:00
</AlertDialogHeader>
2025-10-23 17:29:52 +00:00
<AlertDialogFooter className="mt-12">
<AlertDialogCancel autoFocus className="min-w-[120px]" onClick={() => handleClose(false)}>
2025-11-19 16:05:01 +00:00
{cancelLabel}
</AlertDialogCancel>
<AlertDialogAction
className="min-w-[120px] bg-destructive text-white hover:bg-destructive/90"
onClick={() => handleClose(true)}
>
2025-10-23 17:29:52 +00:00
{confirmLabel}
</AlertDialogAction>
2025-05-05 17:11:44 +00:00
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
2025-11-19 16:05:01 +00:00
};