|
| 1 | +import { useCallback, useState } from 'react'; |
1 | 2 | import cx from 'classnames';
|
2 |
| -import { Button } from 'decentraland-ui2'; |
| 3 | +import { Button, Divider, IconButton, Menu, MenuItem } from 'decentraland-ui2'; |
| 4 | +import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown'; |
3 | 5 |
|
4 |
| -import type { Props } from './types'; |
| 6 | +import type { Props, ActionsProps } from './types'; |
5 | 7 |
|
6 | 8 | import './styles.css';
|
7 | 9 |
|
8 |
| -export function ButtonComponent({ children, className = '', onClick, ...props }: Props) { |
| 10 | +export function ButtonComponent({ |
| 11 | + children, |
| 12 | + className = '', |
| 13 | + onClick, |
| 14 | + actions = [], |
| 15 | + actionableIcon, |
| 16 | + ...props |
| 17 | +}: Props) { |
9 | 18 | return (
|
10 | 19 | <Button
|
11 | 20 | {...props}
|
12 | 21 | className={cx('Button', className)}
|
13 | 22 | onClick={onClick}
|
14 | 23 | >
|
15 | 24 | {children}
|
| 25 | + <Actions |
| 26 | + actions={actions} |
| 27 | + icon={actionableIcon} |
| 28 | + /> |
16 | 29 | </Button>
|
17 | 30 | );
|
18 | 31 | }
|
| 32 | + |
| 33 | +function Actions({ actions, icon }: ActionsProps) { |
| 34 | + const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); |
| 35 | + const open = Boolean(anchorEl); |
| 36 | + |
| 37 | + const handleOpen = useCallback((event: React.MouseEvent<HTMLElement, MouseEvent>) => { |
| 38 | + event.stopPropagation(); |
| 39 | + setAnchorEl(event.currentTarget); |
| 40 | + }, []); |
| 41 | + |
| 42 | + const handleClose = useCallback((event: React.MouseEvent<HTMLElement, MouseEvent>) => { |
| 43 | + event.stopPropagation(); |
| 44 | + setAnchorEl(null); |
| 45 | + }, []); |
| 46 | + |
| 47 | + const handleClick = useCallback( |
| 48 | + (fn: () => void) => (event: React.MouseEvent<HTMLElement, MouseEvent>) => { |
| 49 | + event.stopPropagation(); |
| 50 | + setAnchorEl(null); |
| 51 | + fn(); |
| 52 | + }, |
| 53 | + [], |
| 54 | + ); |
| 55 | + |
| 56 | + if (!actions.length) return null; |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className="Actions"> |
| 60 | + <Divider |
| 61 | + orientation="vertical" |
| 62 | + flexItem |
| 63 | + sx={{ mx: 0.5 }} |
| 64 | + /> |
| 65 | + <IconButton |
| 66 | + onClick={handleOpen} |
| 67 | + size="large" |
| 68 | + > |
| 69 | + {icon ?? <ArrowDropDownIcon />} |
| 70 | + </IconButton> |
| 71 | + <Menu |
| 72 | + id="ActionsMenu" |
| 73 | + anchorEl={anchorEl} |
| 74 | + open={open} |
| 75 | + onClose={handleClose} |
| 76 | + > |
| 77 | + {actions.map(({ label, onClick }, i) => ( |
| 78 | + <MenuItem |
| 79 | + key={i} |
| 80 | + onClick={handleClick(onClick)} |
| 81 | + > |
| 82 | + {label} |
| 83 | + </MenuItem> |
| 84 | + ))} |
| 85 | + </Menu> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +} |
0 commit comments