Skip to content

Commit 2898032

Browse files
feat: add prop for passing action list for ellipses menu
Signed-off-by: Rudraksh Tyagi <rudraksh875@gmail.com>
1 parent 09366aa commit 2898032

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

src/custom/ResponsiveDataTable.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
11
import { Theme, ThemeProvider, createTheme } from '@mui/material';
22
import MUIDataTable from 'mui-datatables';
33
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+
};
444

545
const dataTableTheme = (theme: Theme) =>
646
createTheme({
@@ -129,6 +169,12 @@ export interface Column {
129169
display?: boolean;
130170
sortDescFirst?: boolean;
131171
customBodyRender?: (value: string | number | boolean | object) => JSX.Element;
172+
actionsList?: {
173+
title: string;
174+
icon: JSX.Element;
175+
onClick: () => void;
176+
disabled?: boolean;
177+
}[];
132178
};
133179
}
134180

@@ -165,6 +211,16 @@ const ResponsiveDataTable = ({
165211
return new Intl.DateTimeFormat('un-US', dateOptions).format(date);
166212
};
167213

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+
168224
const updatedOptions = {
169225
...options,
170226
print: false,

0 commit comments

Comments
 (0)