-
Notifications
You must be signed in to change notification settings - Fork 12
/
LayoutToggle.jsx
65 lines (60 loc) · 1.85 KB
/
LayoutToggle.jsx
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
import React from 'react';
import { Chip, MenuItem, Menu, ListItemIcon, Tooltip } from '@mui/material';
import {
TableChart as TableIcon,
ViewStream as RowsIcon,
ArrowDropDown as ArrowDropDownIcon
} from '@mui/icons-material';
import { find, map } from 'lodash';
import {
TABLE_LAYOUT_ID, LIST_LAYOUT_ID
} from '../../common/constants'
const OPTIONS = [
{id: TABLE_LAYOUT_ID, name: 'Table View', icon: <TableIcon fontSize='inherit' />},
{id: LIST_LAYOUT_ID, name: 'Row View', icon: <RowsIcon fontSize='inherit' />},
]
const LayoutToggle = ({ layoutId, onClick, size }) => {
const [anchorEl, setAnchorEl] = React.useState(null);
const selectedLayoutId = layoutId || 'table';
const selectedLayout = find(OPTIONS, {id: selectedLayoutId})
const toggleAnchor = event => setAnchorEl(anchorEl ? null : event.currentTarget)
const onSelect = id => {
onClick(id)
toggleAnchor()
}
return (
<React.Fragment>
<Tooltip arrow title='Switch view layouts'>
<Chip
variant="outlined"
icon={selectedLayout.icon}
color="primary"
label={selectedLayout.name}
onClick={toggleAnchor}
size={size || 'medium'}
deleteIcon={<ArrowDropDownIcon fontSize="inherit" />}
onDelete={toggleAnchor}
/>
</Tooltip>
<Menu
id="layouts-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={toggleAnchor}
>
{
map(OPTIONS, ({id, name, icon}) => (
<MenuItem key={id} value={id} onClick={() => onSelect(id)}>
<ListItemIcon style={{minWidth: 'auto', marginRight: '10px'}}>
{icon}
</ListItemIcon>
<span>{name}</span>
</MenuItem>
))
}
</Menu>
</React.Fragment>
);
}
export default LayoutToggle;