-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathindex.tsx
111 lines (100 loc) · 2.64 KB
/
index.tsx
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*--------------------------------------------------------------------------
* Copyright (c) 2019-2021, Postgres.ai, Nikolay Samokhvalov nik@postgres.ai
* All Rights Reserved. Proprietary and confidential.
* Unauthorized copying of this file, via any medium is strictly prohibited
*--------------------------------------------------------------------------
*/
import React, { useState } from 'react'
import { Menu, MenuItem, IconButton, makeStyles } from '@material-ui/core'
import { MoreVert } from '@material-ui/icons'
import clsx from 'clsx'
import { StubSpinner } from '@postgres.ai/shared/components/StubSpinnerFlex'
const DIRECTION_TO_ORIGIN = {
left: 'right',
right: 'left',
} as const
type Action = {
name: string
onClick: () => void
isDisabled?: boolean
}
export type Props = {
actions: Action[]
isDisabled?: boolean
isLoading?: boolean
direction?: 'left' | 'right'
}
const useStyles = makeStyles(
{
button: {
'&:disabled': {
cursor: 'not-allowed',
pointerEvents: 'all',
},
},
spinner: {
background: 'transparent',
},
hiddenIcon: {
visibility: 'hidden',
},
},
{ index: 1 },
)
export const RowMenu = (props: Props) => {
const {
actions,
isDisabled = false,
isLoading = false,
direction = 'right',
} = props
const classes = useStyles()
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null)
const isOpen = Boolean(anchorEl)
const openMenu: React.MouseEventHandler<HTMLButtonElement> = (e) =>
setAnchorEl(e.currentTarget)
const closeMenu = () => setAnchorEl(null)
return (
<>
<IconButton
onClick={openMenu}
disabled={isDisabled || isLoading}
className={classes.button}
>
<MoreVert className={clsx(isLoading && classes.hiddenIcon)} />
{isLoading && (
<StubSpinner size="sm" mode="absolute" className={classes.spinner} />
)}
</IconButton>
<Menu
anchorOrigin={{
vertical: 'top',
horizontal: DIRECTION_TO_ORIGIN[direction],
}}
transformOrigin={{
vertical: 'top',
horizontal: DIRECTION_TO_ORIGIN[direction],
}}
anchorEl={anchorEl}
keepMounted
open={isOpen}
onClose={closeMenu}
>
{actions.map((action, i) => {
return (
<MenuItem
key={i}
onClick={() => {
closeMenu()
action.onClick()
}}
disabled={action.isDisabled}
>
{action.name}
</MenuItem>
)
})}
</Menu>
</>
)
}