aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/modals/BaseModal.tsx
blob: f9488ef6b80ecc03d809ca15f6e73d3b0d6a480a (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
49
50
51
import { useIsShowed, useModalControl } from "@/modules/redux/hooks/modal";
import clsx from "clsx";
import { FunctionComponent, useCallback, useState } from "react";
import { Modal } from "react-bootstrap";

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, closeable = true } = props;
  const [needExit, setExit] = useState(false);

  const { hide: hideModal } = useModalControl();
  const showIndex = useIsShowed(modalKey);
  const isShowed = showIndex !== -1;

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

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

  return (
    <Modal
      centered
      size={size}
      show={isShowed && !needExit}
      onHide={hide}
      onExited={exit}
      backdrop={closeable ? undefined : "static"}
      className={clsx(`index-${showIndex}`)}
      backdropClassName={clsx(`index-${showIndex}`)}
    >
      <Modal.Header closeButton={closeable}>{title}</Modal.Header>
      <Modal.Body>{children}</Modal.Body>
      <Modal.Footer hidden={footer === undefined}>{footer}</Modal.Footer>
    </Modal>
  );
};

export default BaseModal;