summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/modals/BaseModal.tsx
blob: 89021effba67e4337c285b34c0a38cc4e2ac1d83 (plain)
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
import React, { FunctionComponent, useCallback, useState } from "react";
import { Modal } from "react-bootstrap";
import { useModalInformation } from "./hooks";

export interface BaseModalProps {
  modalKey: string;
  size?: "sm" | "lg" | "xl";
  closeable?: boolean;
  title?: string;
  footer?: JSX.Element;
}

export const BaseModal: FunctionComponent<BaseModalProps> = (props) => {
  const { size, modalKey, title, children, footer } = props;
  const [needExit, setExit] = useState(false);

  const { isShow, closeModal } = useModalInformation(modalKey);

  const closeable = props.closeable !== false;

  const hide = useCallback(() => {
    setExit(true);
  }, []);

  const exit = useCallback(() => {
    if (isShow) {
      closeModal(modalKey);
    }
    setExit(false);
  }, [closeModal, modalKey, isShow]);

  return (
    <Modal
      centered
      size={size}
      show={isShow && !needExit}
      onHide={hide}
      onExited={exit}
      backdrop={closeable ? undefined : "static"}
    >
      <Modal.Header closeButton={closeable}>{title}</Modal.Header>
      <Modal.Body>{children}</Modal.Body>
      <Modal.Footer hidden={footer === undefined}>{footer}</Modal.Footer>
    </Modal>
  );
};

export default BaseModal;