@activecollab/components
Version:
ActiveCollab Components
64 lines • 2.77 kB
JavaScript
import React, { forwardRef, useEffect } from "react";
import classnames from "classnames";
import { Button } from "../Button/Button";
import { Dialog } from "../Dialog";
import { Body2 } from "../Typography/Variants/Body2";
export const ConfirmDialog = /*#__PURE__*/forwardRef((_ref, ref) => {
let _ref$open = _ref.open,
open = _ref$open === void 0 ? false : _ref$open,
onCancel = _ref.onCancel,
onConfirm = _ref.onConfirm,
className = _ref.className,
_ref$dialogTitle = _ref.dialogTitle,
dialogTitle = _ref$dialogTitle === void 0 ? "Discard changes?" : _ref$dialogTitle,
_ref$dialogContent = _ref.dialogContent,
dialogContent = _ref$dialogContent === void 0 ? "All unsaved changes will be lost." : _ref$dialogContent,
_ref$confirmBtnText = _ref.confirmBtnText,
confirmBtnText = _ref$confirmBtnText === void 0 ? "OK" : _ref$confirmBtnText,
_ref$cancelBtnText = _ref.cancelBtnText,
cancelBtnText = _ref$cancelBtnText === void 0 ? "Cancel" : _ref$cancelBtnText,
_ref$shouldShowCancel = _ref.shouldShowCancelButton,
shouldShowCancelButton = _ref$shouldShowCancel === void 0 ? true : _ref$shouldShowCancel,
_ref$isLoading = _ref.isLoading,
isLoading = _ref$isLoading === void 0 ? false : _ref$isLoading;
useEffect(() => {
const handleKeyDown = event => {
if (event.key === "Enter" && onConfirm) {
event.preventDefault();
onConfirm();
}
};
if (open) {
window.addEventListener("keydown", handleKeyDown);
}
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [open, onConfirm]);
return /*#__PURE__*/React.createElement(Dialog, {
ref: ref,
open: open,
onClose: onCancel,
className: classnames("c-confirm-dialog", className),
disableCloseOnEsc: isLoading
}, /*#__PURE__*/React.createElement(Dialog.Title, null, dialogTitle), /*#__PURE__*/React.createElement(Dialog.ContentDivider, null), /*#__PURE__*/React.createElement(Dialog.Content, null, /*#__PURE__*/React.createElement(Body2, {
lineHeight: "loose",
color: "secondary",
whitespace: "pre-line"
}, dialogContent)), /*#__PURE__*/React.createElement(Dialog.ContentDivider, null), /*#__PURE__*/React.createElement(Dialog.Actions, null, /*#__PURE__*/React.createElement(Button, {
variant: "primary",
style: {
marginRight: "12px"
},
onClick: onConfirm,
disabled: isLoading
}, confirmBtnText), shouldShowCancelButton ? /*#__PURE__*/React.createElement(Button, {
variant: "secondary",
"data-action": "cancel",
onClick: onCancel,
type: "button",
disabled: isLoading
}, cancelBtnText) : null));
});
ConfirmDialog.displayName = "ConfirmDialog";
//# sourceMappingURL=ConfirmDialog.js.map