-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathconfirmation-dialog.tsx
88 lines (80 loc) · 2.08 KB
/
confirmation-dialog.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { CircleAlert, Info } from 'lucide-react';
import * as React from 'react';
import { useEffect } from 'react';
import { Button } from '@/components/ui/button';
import { useDisclosure } from '@/hooks/use-disclosure';
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '../dialog';
export type ConfirmationDialogProps = {
triggerButton: React.ReactElement;
confirmButton: React.ReactElement;
title: string;
body?: string;
cancelButtonText?: string;
icon?: 'danger' | 'info';
isDone?: boolean;
};
export const ConfirmationDialog = ({
triggerButton,
confirmButton,
title,
body = '',
cancelButtonText = 'Cancel',
icon = 'danger',
isDone = false,
}: ConfirmationDialogProps) => {
const { close, open, isOpen } = useDisclosure();
const cancelButtonRef = React.useRef(null);
useEffect(() => {
if (isDone) {
close();
}
}, [isDone, close]);
return (
<Dialog
open={isOpen}
onOpenChange={(isOpen) => {
if (!isOpen) {
close();
} else {
open();
}
}}
>
<DialogTrigger asChild>{triggerButton}</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader className="flex">
<DialogTitle className="flex items-center gap-2">
{' '}
{icon === 'danger' && (
<CircleAlert className="size-6 text-red-600" aria-hidden="true" />
)}
{icon === 'info' && (
<Info className="size-6 text-blue-600" aria-hidden="true" />
)}
{title}
</DialogTitle>
</DialogHeader>
<div className="mt-3 text-center sm:ml-4 sm:mt-0 sm:text-left">
{body && (
<div className="mt-2">
<p>{body}</p>
</div>
)}
</div>
<DialogFooter>
{confirmButton}
<Button ref={cancelButtonRef} variant="outline" onClick={close}>
{cancelButtonText}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};