Skip to content

Commit 338f1e3

Browse files
committed
fixes #548
1 parent b5d34e8 commit 338f1e3

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

packages/material-react-table/src/components/menus/MRT_RowActionMenu.tsx

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type MouseEvent } from 'react';
1+
import { ReactNode, useMemo, type MouseEvent } from 'react';
22
import Menu, { type MenuProps } from '@mui/material/Menu';
33
import { MRT_ActionMenuItem } from './MRT_ActionMenuItem';
44
import {
@@ -40,6 +40,30 @@ export const MRT_RowActionMenu = <TData extends MRT_RowData>({
4040
} = table;
4141
const { density } = getState();
4242

43+
const menuItems = useMemo(() => {
44+
const items: ReactNode[] = [];
45+
const editItem = parseFromValuesOrFunc(enableEditing, row) &&
46+
['modal', 'row'].includes(editDisplayMode!) && (
47+
<MRT_ActionMenuItem
48+
icon={<EditIcon />}
49+
label={localization.edit}
50+
onClick={handleEdit}
51+
table={table}
52+
/>
53+
);
54+
if (editItem) items.push(editItem);
55+
const rowActionMenuItems = renderRowActionMenuItems?.({
56+
closeMenu: () => setAnchorEl(null),
57+
row,
58+
staticRowIndex,
59+
table,
60+
});
61+
if (rowActionMenuItems?.length) items.push(...rowActionMenuItems);
62+
return items;
63+
}, [renderRowActionMenuItems, row, staticRowIndex, table]);
64+
65+
if (!menuItems.length) return null;
66+
4367
return (
4468
<Menu
4569
MenuListProps={{
@@ -55,21 +79,7 @@ export const MRT_RowActionMenu = <TData extends MRT_RowData>({
5579
open={!!anchorEl}
5680
{...rest}
5781
>
58-
{parseFromValuesOrFunc(enableEditing, row) &&
59-
['modal', 'row'].includes(editDisplayMode!) && (
60-
<MRT_ActionMenuItem
61-
icon={<EditIcon />}
62-
label={localization.edit}
63-
onClick={handleEdit}
64-
table={table}
65-
/>
66-
)}
67-
{renderRowActionMenuItems?.({
68-
closeMenu: () => setAnchorEl(null),
69-
row,
70-
staticRowIndex,
71-
table,
72-
})}
82+
{menuItems}
7383
</Menu>
7484
);
7585
};

packages/material-react-table/stories/features/RowActions.stories.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,53 @@ export const RowActionsEnabledConditionally = () => {
127127
);
128128
};
129129

130+
export const RowActionsEnabledConditionallyPerRow = () => {
131+
const [enabled, setEnabled] = useState(false);
132+
return (
133+
<MaterialReactTable
134+
columns={columns}
135+
data={data}
136+
enableRowActions={enabled}
137+
renderRowActionMenuItems={({ closeMenu, row }) =>
138+
row.index % 2 === 0
139+
? []
140+
: [
141+
<MenuItem
142+
key={1}
143+
onClick={() => {
144+
console.info('View Profile', row);
145+
closeMenu();
146+
}}
147+
>
148+
<AccountCircleIcon /> View Profile
149+
</MenuItem>,
150+
<MenuItem
151+
key={2}
152+
onClick={() => {
153+
console.info('Remove', row);
154+
closeMenu();
155+
}}
156+
>
157+
<DeleteIcon /> Remove
158+
</MenuItem>,
159+
<MenuItem
160+
key={3}
161+
onClick={() => {
162+
console.info('Share', row);
163+
closeMenu();
164+
}}
165+
>
166+
<ShareIcon /> Share
167+
</MenuItem>,
168+
]
169+
}
170+
renderTopToolbarCustomActions={() => (
171+
<Button onClick={() => setEnabled(!enabled)}>Toggle Row Actions</Button>
172+
)}
173+
/>
174+
);
175+
};
176+
130177
export const RowActionsAndEditingEnabled = () => (
131178
<MaterialReactTable
132179
columns={columns}

0 commit comments

Comments
 (0)