Skip to content

Commit a785a8a

Browse files
Merge pull request #20 from weDevsOfficial/feat/confirm-modal-button-loading
feat: ConfirmModal onConfirm promise support
2 parents 4f63f81 + d1c9094 commit a785a8a

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@
8585
"vite-plugin-dts": "^3.4.0"
8686
},
8787
"packageManager": "yarn@4.1.0"
88-
}
88+
}

src/Components/Modal/ConfirmModal.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Modal, ModalActions, ModalBody, ModalHeader } from './Modal';
22
import { Button } from '@/Components/Button';
33
import { ExclamationTriangleIcon } from '@heroicons/react/24/outline';
4+
import { useState } from 'react';
45

56
interface ConfirmModalProps {
67
isOpen: boolean;
@@ -9,7 +10,7 @@ interface ConfirmModalProps {
910
buttonLabel?: string;
1011
buttonVariant?: 'primary' | 'secondary' | 'danger';
1112
onClose: () => void;
12-
onConfirm: () => void;
13+
onConfirm: () => Promise<void> | void;
1314
}
1415

1516
const ConfirmModal = ({
@@ -21,6 +22,17 @@ const ConfirmModal = ({
2122
onClose,
2223
onConfirm,
2324
}: ConfirmModalProps) => {
25+
const [isLoading, setIsLoading] = useState(false);
26+
27+
const handleConfirm = async () => {
28+
setIsLoading(true);
29+
try {
30+
await onConfirm();
31+
} finally {
32+
setIsLoading(false);
33+
}
34+
};
35+
2436
return (
2537
<Modal isOpen={isOpen} onClose={() => onClose()}>
2638
<div className="flex">
@@ -37,12 +49,10 @@ const ConfirmModal = ({
3749
<ModalActions>
3850
<Button
3951
variant={buttonVariant}
40-
onClick={() => {
41-
if (onConfirm) {
42-
onConfirm();
43-
}
44-
}}
52+
onClick={handleConfirm}
4553
className="ml-3"
54+
disabled={isLoading}
55+
loading={isLoading}
4656
>
4757
{buttonLabel}
4858
</Button>

0 commit comments

Comments
 (0)