Creation dialog with redux-form validation
[arvados-workbench2.git] / src / components / context-menu / context-menu.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4 import * as React from "react";
5 import { Popover, List, ListItem, ListItemIcon, ListItemText, Divider } from "@material-ui/core";
6 import { DefaultTransformOrigin } from "../popover/helpers";
7
8 export interface ContextMenuAction {
9     name: string;
10     icon: string;
11     openCreateDialog?: boolean;
12 }
13
14 export type ContextMenuActionGroup = ContextMenuAction[];
15
16 export interface ContextMenuProps<T> {
17     anchorEl?: HTMLElement;
18     actions: ContextMenuActionGroup[];
19     onActionClick: (action: ContextMenuAction) => void;
20     onClose: () => void;
21 }
22
23 export default class ContextMenu<T> extends React.PureComponent<ContextMenuProps<T>> {
24     render() {
25         const { anchorEl, actions, onClose, onActionClick } = this.props;
26         return <Popover
27             anchorEl={anchorEl}
28             open={!!anchorEl}
29             onClose={onClose}
30             transformOrigin={DefaultTransformOrigin}
31             anchorOrigin={DefaultTransformOrigin}
32             onContextMenu={this.handleContextMenu}>
33             <List dense>
34                 {actions.map((group, groupIndex) =>
35                     <React.Fragment key={groupIndex}>
36                         {group.map((action, actionIndex) =>
37                             <ListItem
38                                 button
39                                 key={actionIndex}
40                                 onClick={() => onActionClick(action)}>
41                                 <ListItemIcon>
42                                     <i className={action.icon} />
43                                 </ListItemIcon>
44                                 <ListItemText>
45                                     {action.name}
46                                 </ListItemText>
47                             </ListItem>)}
48                         {groupIndex < actions.length - 1 && <Divider />}
49                     </React.Fragment>)}
50             </List>
51         </Popover>;
52     }
53
54     handleContextMenu = (event: React.MouseEvent<HTMLElement>) => {
55         event.preventDefault();
56         this.props.onClose();
57     }
58 }