-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDeleteAction.tsx
More file actions
64 lines (59 loc) · 1.79 KB
/
DeleteAction.tsx
File metadata and controls
64 lines (59 loc) · 1.79 KB
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
import { faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import React, { useState } from "react";
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";
interface DeleteActionProps {
title?: string;
text?: string;
deleteButtonText?: string;
cancelButtonText?: string;
iconOnly?: boolean;
onDelete: () => void | Promise<boolean>;
}
function DeleteAction({
title = "Delete Entry",
text = "Are you sure to Delete this Entry?",
deleteButtonText = "Delete",
cancelButtonText = "Cancel",
iconOnly = false,
onDelete,
}: DeleteActionProps) {
const [showModal, setShowModal] = useState(false);
const toggle = () => setShowModal(!showModal);
return (
<React.Fragment>
{iconOnly ? (
<FontAwesomeIcon icon={faTrash} style={{ marginRight: "5px", cursor: "pointer" }} onClick={toggle} />
) : (
<Button color="danger" close onClick={toggle}>
<FontAwesomeIcon icon={faTrash} />
</Button>
)}
<Modal isOpen={showModal} toggle={toggle}>
<ModalHeader toggle={toggle}>{title}</ModalHeader>
<ModalBody>{text}</ModalBody>
<ModalFooter>
<Button
color="danger"
onClick={async () => {
if (onDelete instanceof Promise) {
if (await onDelete()) {
toggle();
}
} else {
onDelete();
toggle();
}
}}
>
{deleteButtonText}
</Button>{" "}
<Button color="secondary" onClick={toggle}>
{cancelButtonText}
</Button>
</ModalFooter>
</Modal>
</React.Fragment>
);
}
export { DeleteAction, DeleteActionProps };