ConfirmationModal.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React from 'react';
  2. import { Button } from 'components/common/Button/Button';
  3. import { ConfirmationModalWrapper } from './ConfirmationModal.styled';
  4. export interface ConfirmationModalProps {
  5. isOpen?: boolean;
  6. title?: React.ReactNode;
  7. onConfirm(): void;
  8. onCancel(): void;
  9. isConfirming?: boolean;
  10. submitBtnText?: string;
  11. }
  12. const ConfirmationModal: React.FC<ConfirmationModalProps> = ({
  13. isOpen,
  14. children,
  15. title = 'Confirm the action',
  16. onCancel,
  17. onConfirm,
  18. isConfirming = false,
  19. submitBtnText = 'Submit',
  20. }) => {
  21. const cancelHandler = React.useCallback(() => {
  22. if (!isConfirming) {
  23. onCancel();
  24. }
  25. }, [isConfirming, onCancel]);
  26. return isOpen ? (
  27. <ConfirmationModalWrapper>
  28. <div onClick={cancelHandler} aria-hidden="true" />
  29. <div>
  30. <header>
  31. <p>{title}</p>
  32. </header>
  33. <section>{children}</section>
  34. <footer>
  35. <Button
  36. buttonType="secondary"
  37. buttonSize="M"
  38. onClick={cancelHandler}
  39. type="button"
  40. disabled={isConfirming}
  41. >
  42. Cancel
  43. </Button>
  44. <Button
  45. buttonType="primary"
  46. buttonSize="M"
  47. onClick={onConfirm}
  48. type="button"
  49. disabled={isConfirming}
  50. >
  51. {submitBtnText}
  52. </Button>
  53. </footer>
  54. </div>
  55. </ConfirmationModalWrapper>
  56. ) : null;
  57. };
  58. export default ConfirmationModal;