|
1 | 1 | import { Theme, ThemeProvider, createTheme } from '@mui/material'; |
2 | 2 | import MUIDataTable from 'mui-datatables'; |
3 | 3 | import React, { useCallback } from 'react'; |
| 4 | +import { IconButton, Menu, MenuItem } from '../base'; |
| 5 | +import { DropDownIcon } from '../icons'; |
| 6 | + |
| 7 | +const DataTableEllipsisMenu: React.FC<{ |
| 8 | + actionsList: NonNullable<Column['options']>['actionsList']; |
| 9 | +}> = ({ actionsList }) => { |
| 10 | + const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null); |
| 11 | + const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { |
| 12 | + setAnchorEl(event.currentTarget); |
| 13 | + }; |
| 14 | + const handleClose = () => { |
| 15 | + setAnchorEl(null); |
| 16 | + }; |
| 17 | + return ( |
| 18 | + <> |
| 19 | + <IconButton onClick={handleClick}> |
| 20 | + <DropDownIcon /> |
| 21 | + </IconButton> |
| 22 | + <Menu |
| 23 | + anchorEl={anchorEl} |
| 24 | + open={Boolean(anchorEl)} |
| 25 | + onClose={handleClose} |
| 26 | + PaperProps={{ |
| 27 | + style: { |
| 28 | + maxHeight: 48 * 4.5, |
| 29 | + width: '20ch' |
| 30 | + } |
| 31 | + }} |
| 32 | + > |
| 33 | + {actionsList && |
| 34 | + actionsList.map((action, index) => ( |
| 35 | + <MenuItem key={index} onClick={action.onClick} disabled={action.disabled}> |
| 36 | + {action.icon} |
| 37 | + {action.title} |
| 38 | + </MenuItem> |
| 39 | + ))} |
| 40 | + </Menu> |
| 41 | + </> |
| 42 | + ); |
| 43 | +}; |
4 | 44 |
|
5 | 45 | const dataTableTheme = (theme: Theme) => |
6 | 46 | createTheme({ |
@@ -129,6 +169,12 @@ export interface Column { |
129 | 169 | display?: boolean; |
130 | 170 | sortDescFirst?: boolean; |
131 | 171 | customBodyRender?: (value: string | number | boolean | object) => JSX.Element; |
| 172 | + actionsList?: { |
| 173 | + title: string; |
| 174 | + icon: JSX.Element; |
| 175 | + onClick: () => void; |
| 176 | + disabled?: boolean; |
| 177 | + }[]; |
132 | 178 | }; |
133 | 179 | } |
134 | 180 |
|
@@ -165,6 +211,16 @@ const ResponsiveDataTable = ({ |
165 | 211 | return new Intl.DateTimeFormat('un-US', dateOptions).format(date); |
166 | 212 | }; |
167 | 213 |
|
| 214 | + columns.forEach((col) => { |
| 215 | + console.log('col', col); |
| 216 | + if (col.options?.actionsList) { |
| 217 | + console.log('col.options.actionsList', col.options.actionsList); |
| 218 | + col.options.customBodyRender = function CustomBody() { |
| 219 | + return <DataTableEllipsisMenu actionsList={col.options?.actionsList} />; |
| 220 | + }; |
| 221 | + } |
| 222 | + }); |
| 223 | + |
168 | 224 | const updatedOptions = { |
169 | 225 | ...options, |
170 | 226 | print: false, |
|
0 commit comments