Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for custom action in action component #701

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 56 additions & 13 deletions src/custom/ResponsiveDataTable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Theme, ThemeProvider, createTheme, styled, useTheme } from '@mui/material';
import MUIDataTable from 'mui-datatables';
import React, { useCallback } from 'react';
import { ListItemIcon, ListItemText, Menu, MenuItem } from '../base';
import { Collapse, ListItemIcon, ListItemText, Menu, MenuItem } from '../base';
import { ShareIcon } from '../icons';
import { EllipsisIcon } from '../icons/Ellipsis';
import TooltipIcon from './TooltipIcon';

Expand All @@ -18,14 +19,28 @@ export const DataTableEllipsisMenu: React.FC<{
actionsList: NonNullable<Column['options']>['actionsList'];
}> = ({ actionsList }) => {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const [isSocialShareOpen, setIsSocialShareOpen] = React.useState(false);
const theme = useTheme();

const handleClick = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
setIsSocialShareOpen(false);
};

const theme = useTheme();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleActionClick = (action: any) => {
if (action.type === 'share-social') {
setIsSocialShareOpen(!isSocialShareOpen);
} else {
if (action.onClick) {
action.onClick();
}
handleClose();
}
};

return (
<>
Expand All @@ -43,17 +58,43 @@ export const DataTableEllipsisMenu: React.FC<{
>
{actionsList &&
actionsList.map((action, index) => (
<IconWrapper key={index} disabled={action.disabled}>
<MenuItem
sx={{ width: '-webkit-fill-available' }}
key={index}
onClick={action.onClick}
disabled={action.disabled}
>
<ListItemIcon>{action.icon}</ListItemIcon>
<ListItemText>{action.title}</ListItemText>
</MenuItem>
</IconWrapper>
<React.Fragment key={index}>
{action.type === 'share-social' ? (
<>
<MenuItem
sx={{
width: '-webkit-fill-available',
background: theme.palette.background.surfaces
}}
onClick={() => handleActionClick(action)}
disabled={action.disabled}
>
<ListItemIcon>
<ShareIcon width={24} height={24} />
</ListItemIcon>
<ListItemText>{action.title}</ListItemText>
</MenuItem>
<Collapse in={isSocialShareOpen}>{action.customComponent}</Collapse>
</>
) : (
<>
<IconWrapper key={index} disabled={action.disabled}>
<MenuItem
sx={{
width: '-webkit-fill-available',
background: theme.palette.background.surfaces
}}
key={index}
onClick={() => handleActionClick(action)}
disabled={action.disabled}
>
<ListItemIcon>{action.icon}</ListItemIcon>
<ListItemText>{action.title}</ListItemText>
</MenuItem>
</IconWrapper>
</>
)}
</React.Fragment>
))}
</Menu>
</>
Expand Down Expand Up @@ -201,6 +242,8 @@ export interface Column {
icon: JSX.Element;
onClick: () => void;
disabled?: boolean;
customComponent?: JSX.Element;
type?: string;
}[];
};
}
Expand Down
Loading