aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/views/common/Menu.tsx
blob: a16199a5daf00304e35e8feea2c720a977323e65 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React from "react";
import classnames from "classnames";
import { OverlayTrigger, OverlayTriggerProps, Popover } from "react-bootstrap";
import { useTranslation } from "react-i18next";

import { BootstrapThemeColor, convertI18nText, I18nText } from "@/common";

export type MenuItem =
  | {
      type: "divider";
    }
  | {
      type: "button";
      text: I18nText;
      iconClassName?: string;
      color?: BootstrapThemeColor;
      onClick: () => void;
    };

export type MenuItems = MenuItem[];

export interface MenuProps {
  items: MenuItems;
  className?: string;
  onItemClicked?: () => void;
}

const Menu: React.FC<MenuProps> = ({ items, className, onItemClicked }) => {
  const { t } = useTranslation();

  return (
    <div className={classnames("cru-menu", className)}>
      {items.map((item, index) => {
        if (item.type === "divider") {
          return <div key={index} className="cru-menu-divider" />;
        } else {
          return (
            <div
              key={index}
              className={classnames(
                "cru-menu-item",
                `color-${item.color ?? "primary"}`
              )}
              onClick={() => {
                item.onClick();
                onItemClicked?.();
              }}
            >
              {item.iconClassName != null ? (
                <i className={classnames(item.iconClassName, "cru-menu-item-icon")} />
              ) : null}
              {convertI18nText(item.text, t)}
            </div>
          );
        }
      })}
    </div>
  );
};

export default Menu;

export interface PopupMenuProps {
  items: MenuItems;
  children: OverlayTriggerProps["children"];
}

export const PopupMenu: React.FC<PopupMenuProps> = ({ items, children }) => {
  const [show, setShow] = React.useState<boolean>(false);
  const toggle = (): void => setShow(!show);

  return (
    <OverlayTrigger
      trigger="click"
      rootClose
      overlay={
        <Popover id="menu-popover">
          <Menu items={items} onItemClicked={() => setShow(false)} />
        </Popover>
      }
      show={show}
      onToggle={toggle}
    >
      {children}
    </OverlayTrigger>
  );
};