UNPKG

@activecollab/components

Version:

ActiveCollab Components

89 lines 3.88 kB
import React, { forwardRef, useCallback, useEffect, useState } from "react"; import classnames from "classnames"; import { Button } from "../Button/Button"; import { Checkbox } from "../Checkbox"; import { Dialog } from "../Dialog"; import { StyledConfirmation } from "./Styles"; import { Body2 } from "../Typography/Variants/Body2"; const CONFIRMATION_CHECKBOX_ID = "confirm-dialog-confirmation"; 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, confirmationLabel = _ref.confirmationLabel; const _useState = useState(false), confirmed = _useState[0], setConfirmed = _useState[1]; const isConfirmBlocked = Boolean(confirmationLabel) && !confirmed; const handleToggleConfirmed = useCallback(event => setConfirmed(event.target.checked), []); useEffect(() => { if (!open) { setConfirmed(false); } }, [open]); useEffect(() => { const handleKeyDown = event => { if (event.key === "Enter" && onConfirm && !isConfirmBlocked) { event.preventDefault(); onConfirm(); } }; if (open) { window.addEventListener("keydown", handleKeyDown); } return () => { window.removeEventListener("keydown", handleKeyDown); }; }, [open, onConfirm, isConfirmBlocked]); 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), confirmationLabel ? /*#__PURE__*/React.createElement(StyledConfirmation, { className: "c-confirm-dialog--confirmation" }, /*#__PURE__*/React.createElement(Checkbox, { id: CONFIRMATION_CHECKBOX_ID, checked: confirmed, onChange: handleToggleConfirmed, disabled: isLoading }), /*#__PURE__*/React.createElement("label", { htmlFor: CONFIRMATION_CHECKBOX_ID }, /*#__PURE__*/React.createElement(Body2, { color: "secondary" }, confirmationLabel))) : null), /*#__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 || isConfirmBlocked }, 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