material-react-table 2.9.2 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +162 -111
- package/dist/index.esm.js +694 -606
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +708 -618
- package/dist/index.js.map +1 -1
- package/locales/ar/index.esm.js +1 -0
- package/locales/ar/index.js +1 -0
- package/locales/az/index.esm.js +1 -0
- package/locales/az/index.js +1 -0
- package/locales/bg/index.esm.js +1 -0
- package/locales/bg/index.js +1 -0
- package/locales/cs/index.esm.js +1 -0
- package/locales/cs/index.js +1 -0
- package/locales/da/index.esm.js +1 -0
- package/locales/da/index.js +1 -0
- package/locales/de/index.esm.js +1 -0
- package/locales/de/index.js +1 -0
- package/locales/en/index.esm.js +1 -0
- package/locales/en/index.js +1 -0
- package/locales/es/index.esm.js +1 -0
- package/locales/es/index.js +1 -0
- package/locales/et/index.esm.js +1 -0
- package/locales/et/index.js +1 -0
- package/locales/fa/index.esm.js +1 -0
- package/locales/fa/index.js +1 -0
- package/locales/fi/index.esm.js +1 -0
- package/locales/fi/index.js +1 -0
- package/locales/fr/index.esm.js +1 -0
- package/locales/fr/index.js +1 -0
- package/locales/he/index.esm.js +1 -0
- package/locales/he/index.js +1 -0
- package/locales/hu/index.esm.js +1 -0
- package/locales/hu/index.js +1 -0
- package/locales/hy/index.esm.js +1 -0
- package/locales/hy/index.js +1 -0
- package/locales/id/index.esm.js +1 -0
- package/locales/id/index.js +1 -0
- package/locales/it/index.esm.js +1 -0
- package/locales/it/index.js +1 -0
- package/locales/ja/index.esm.js +1 -0
- package/locales/ja/index.js +1 -0
- package/locales/ko/index.esm.js +1 -0
- package/locales/ko/index.js +1 -0
- package/locales/nl/index.esm.js +1 -0
- package/locales/nl/index.js +1 -0
- package/locales/no/index.esm.js +1 -0
- package/locales/no/index.js +1 -0
- package/locales/np/index.esm.js +1 -0
- package/locales/np/index.js +1 -0
- package/locales/pl/index.esm.js +1 -0
- package/locales/pl/index.js +1 -0
- package/locales/pt/index.esm.js +1 -0
- package/locales/pt/index.js +1 -0
- package/locales/pt-BR/index.esm.js +1 -0
- package/locales/pt-BR/index.js +1 -0
- package/locales/ro/index.esm.js +1 -0
- package/locales/ro/index.js +1 -0
- package/locales/ru/index.esm.js +1 -0
- package/locales/ru/index.js +1 -0
- package/locales/sk/index.esm.js +1 -0
- package/locales/sk/index.js +1 -0
- package/locales/sr-Cyrl-RS/index.esm.js +1 -0
- package/locales/sr-Cyrl-RS/index.js +1 -0
- package/locales/sr-Latn-RS/index.esm.js +1 -0
- package/locales/sr-Latn-RS/index.js +1 -0
- package/locales/sv/index.esm.js +1 -0
- package/locales/sv/index.js +1 -0
- package/locales/tr/index.esm.js +1 -0
- package/locales/tr/index.js +1 -0
- package/locales/uk/index.esm.js +1 -0
- package/locales/uk/index.js +1 -0
- package/locales/vi/index.esm.js +1 -0
- package/locales/vi/index.js +1 -0
- package/locales/zh-Hans/index.esm.js +1 -0
- package/locales/zh-Hans/index.js +1 -0
- package/locales/zh-Hant/index.esm.js +1 -0
- package/locales/zh-Hant/index.js +1 -0
- package/package.json +4 -4
- package/src/components/body/MRT_TableBodyCell.tsx +41 -32
- package/src/components/body/MRT_TableBodyRow.tsx +0 -4
- package/src/components/buttons/MRT_CopyButton.tsx +1 -0
- package/src/components/head/MRT_TableHeadCell.tsx +6 -0
- package/src/components/head/MRT_TableHeadCellFilterLabel.tsx +1 -0
- package/src/components/head/MRT_TableHeadRow.tsx +1 -0
- package/src/components/menus/MRT_ActionMenuItem.tsx +60 -0
- package/src/components/menus/MRT_CellActionMenu.tsx +108 -0
- package/src/components/menus/MRT_ColumnActionMenu.tsx +89 -170
- package/src/components/menus/MRT_FilterOptionMenu.tsx +6 -14
- package/src/components/menus/MRT_RowActionMenu.tsx +7 -15
- package/src/components/menus/MRT_ShowHideColumnsMenu.tsx +3 -1
- package/src/components/table/MRT_TableContainer.tsx +4 -0
- package/src/hooks/display-columns/getMRT_RowActionsColumnDef.tsx +1 -8
- package/src/hooks/display-columns/getMRT_RowDragColumnDef.tsx +1 -8
- package/src/hooks/display-columns/getMRT_RowExpandColumnDef.tsx +1 -8
- package/src/hooks/display-columns/getMRT_RowNumbersColumnDef.tsx +1 -8
- package/src/hooks/display-columns/getMRT_RowPinningColumnDef.tsx +1 -8
- package/src/hooks/display-columns/getMRT_RowSelectColumnDef.tsx +1 -8
- package/src/hooks/display-columns/getMRT_RowSpacerColumnDef.tsx +1 -8
- package/src/hooks/useMRT_Effects.ts +10 -0
- package/src/hooks/useMRT_TableInstance.ts +112 -59
- package/src/icons.ts +2 -0
- package/src/index.ts +2 -0
- package/src/locales/ar.ts +1 -0
- package/src/locales/az.ts +1 -0
- package/src/locales/bg.ts +1 -0
- package/src/locales/cs.ts +1 -0
- package/src/locales/da.ts +1 -0
- package/src/locales/de.ts +1 -0
- package/src/locales/en.ts +1 -0
- package/src/locales/es.ts +1 -0
- package/src/locales/et.ts +1 -0
- package/src/locales/fa.ts +1 -0
- package/src/locales/fi.ts +1 -0
- package/src/locales/fr.ts +1 -0
- package/src/locales/he.ts +1 -0
- package/src/locales/hu.ts +1 -0
- package/src/locales/hy.ts +1 -0
- package/src/locales/id.ts +1 -0
- package/src/locales/it.ts +1 -0
- package/src/locales/ja.ts +1 -0
- package/src/locales/ko.ts +1 -0
- package/src/locales/nl.ts +1 -0
- package/src/locales/no.ts +1 -0
- package/src/locales/np.ts +1 -0
- package/src/locales/pl.ts +1 -0
- package/src/locales/pt-BR.ts +1 -0
- package/src/locales/pt.ts +1 -0
- package/src/locales/ro.ts +1 -0
- package/src/locales/ru.ts +1 -0
- package/src/locales/sk.ts +1 -0
- package/src/locales/sr-Cyrl-RS.ts +1 -0
- package/src/locales/sr-Latn-RS.ts +1 -0
- package/src/locales/sv.ts +1 -0
- package/src/locales/tr.ts +1 -0
- package/src/locales/uk.ts +1 -0
- package/src/locales/vi.ts +1 -0
- package/src/locales/zh-Hans.ts +1 -0
- package/src/locales/zh-Hant.ts +1 -0
- package/src/types.ts +41 -9
- package/src/utils/cell.utils.ts +50 -0
- package/src/utils/displayColumn.utils.ts +38 -20
- package/src/hooks/display-columns/getMRT_DisplayColumns.tsx +0 -26
- package/src/locales/am.ts +0 -94
package/locales/zh-Hans/index.js
CHANGED
package/locales/zh-Hant/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
2
|
+
"version": "2.10.0",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"name": "material-react-table",
|
|
5
5
|
"description": "A fully featured Material UI V5 implementation of TanStack React Table V8, written from the ground up in TypeScript.",
|
|
@@ -80,11 +80,11 @@
|
|
|
80
80
|
"@storybook/react": "^7.6.10",
|
|
81
81
|
"@storybook/react-vite": "^7.6.10",
|
|
82
82
|
"@storybook/testing-library": "^0.2.2",
|
|
83
|
-
"@types/node": "^20.11.
|
|
83
|
+
"@types/node": "^20.11.10",
|
|
84
84
|
"@types/react": "^18.2.48",
|
|
85
85
|
"@types/react-dom": "^18.2.18",
|
|
86
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
87
|
-
"@typescript-eslint/parser": "^6.
|
|
86
|
+
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
|
87
|
+
"@typescript-eslint/parser": "^6.20.0",
|
|
88
88
|
"@vitejs/plugin-react": "^4.2.1",
|
|
89
89
|
"eslint": "^8.56.0",
|
|
90
90
|
"eslint-plugin-mui-path-imports": "^0.0.15",
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
type MRT_RowData,
|
|
17
17
|
type MRT_TableInstance,
|
|
18
18
|
} from '../../types';
|
|
19
|
+
import { isCellEditable, openEditingCell } from '../../utils/cell.utils';
|
|
19
20
|
import { getIsFirstColumn, getIsLastColumn } from '../../utils/column.utils';
|
|
20
21
|
import { getCommonMRTCellStyles, getMRTTheme } from '../../utils/style.utils';
|
|
21
22
|
import { parseFromValuesOrFunc } from '../../utils/utils';
|
|
@@ -24,7 +25,6 @@ import { MRT_EditCellTextField } from '../inputs/MRT_EditCellTextField';
|
|
|
24
25
|
|
|
25
26
|
interface Props<TData extends MRT_RowData> extends TableCellProps {
|
|
26
27
|
cell: MRT_Cell<TData>;
|
|
27
|
-
measureElement?: (element: HTMLTableCellElement) => void;
|
|
28
28
|
numRows?: number;
|
|
29
29
|
rowRef: RefObject<HTMLTableRowElement>;
|
|
30
30
|
staticColumnIndex?: number;
|
|
@@ -34,7 +34,6 @@ interface Props<TData extends MRT_RowData> extends TableCellProps {
|
|
|
34
34
|
|
|
35
35
|
export const MRT_TableBodyCell = <TData extends MRT_RowData>({
|
|
36
36
|
cell,
|
|
37
|
-
measureElement,
|
|
38
37
|
numRows,
|
|
39
38
|
rowRef,
|
|
40
39
|
staticColumnIndex,
|
|
@@ -50,20 +49,19 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
|
|
|
50
49
|
columnResizeMode,
|
|
51
50
|
createDisplayMode,
|
|
52
51
|
editDisplayMode,
|
|
52
|
+
enableCellActions,
|
|
53
53
|
enableClickToCopy,
|
|
54
54
|
enableColumnOrdering,
|
|
55
55
|
enableColumnPinning,
|
|
56
|
-
enableEditing,
|
|
57
56
|
enableGrouping,
|
|
58
57
|
layoutMode,
|
|
59
58
|
muiSkeletonProps,
|
|
60
59
|
muiTableBodyCellProps,
|
|
61
60
|
},
|
|
62
|
-
refs: { editInputRefs },
|
|
63
|
-
setEditingCell,
|
|
64
61
|
setHoveredColumn,
|
|
65
62
|
} = table;
|
|
66
63
|
const {
|
|
64
|
+
actionCell,
|
|
67
65
|
columnSizingInfo,
|
|
68
66
|
creatingRow,
|
|
69
67
|
density,
|
|
@@ -168,10 +166,7 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
|
|
|
168
166
|
columnDef.columnDefType !== 'group' &&
|
|
169
167
|
column.getIsPinned();
|
|
170
168
|
|
|
171
|
-
const isEditable =
|
|
172
|
-
!cell.getIsPlaceholder() &&
|
|
173
|
-
parseFromValuesOrFunc(enableEditing, row) &&
|
|
174
|
-
parseFromValuesOrFunc(columnDef.enableEditing, row) !== false;
|
|
169
|
+
const isEditable = isCellEditable({ cell, table });
|
|
175
170
|
|
|
176
171
|
const isEditing =
|
|
177
172
|
isEditable &&
|
|
@@ -184,18 +179,24 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
|
|
|
184
179
|
const isCreating =
|
|
185
180
|
isEditable && createDisplayMode === 'row' && creatingRow?.id === row.id;
|
|
186
181
|
|
|
182
|
+
const showClickToCopyButton =
|
|
183
|
+
(parseFromValuesOrFunc(enableClickToCopy, cell) === true ||
|
|
184
|
+
parseFromValuesOrFunc(columnDef.enableClickToCopy, cell) === true) &&
|
|
185
|
+
!['context-menu', false].includes(
|
|
186
|
+
// @ts-ignore
|
|
187
|
+
parseFromValuesOrFunc(columnDef.enableClickToCopy, cell),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const isRightClickable = parseFromValuesOrFunc(enableCellActions, cell);
|
|
191
|
+
|
|
192
|
+
const cellValueProps = {
|
|
193
|
+
cell,
|
|
194
|
+
table,
|
|
195
|
+
};
|
|
196
|
+
|
|
187
197
|
const handleDoubleClick = (event: MouseEvent<HTMLTableCellElement>) => {
|
|
188
198
|
tableCellProps?.onDoubleClick?.(event);
|
|
189
|
-
|
|
190
|
-
setEditingCell(cell);
|
|
191
|
-
queueMicrotask(() => {
|
|
192
|
-
const textField = editInputRefs.current[column.id];
|
|
193
|
-
if (textField) {
|
|
194
|
-
textField.focus();
|
|
195
|
-
textField.select?.();
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
+
openEditingCell({ cell, table });
|
|
199
200
|
};
|
|
200
201
|
|
|
201
202
|
const handleDragEnter = (e: DragEvent<HTMLTableCellElement>) => {
|
|
@@ -210,9 +211,13 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
|
|
|
210
211
|
}
|
|
211
212
|
};
|
|
212
213
|
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
214
|
+
const handleContextMenu = (e: MouseEvent<HTMLTableCellElement>) => {
|
|
215
|
+
tableCellProps?.onContextMenu?.(e);
|
|
216
|
+
if (isRightClickable) {
|
|
217
|
+
e.preventDefault();
|
|
218
|
+
table.setActionCell(cell);
|
|
219
|
+
table.refs.actionCellRef.current = e.currentTarget;
|
|
220
|
+
}
|
|
216
221
|
};
|
|
217
222
|
|
|
218
223
|
return (
|
|
@@ -220,27 +225,31 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
|
|
|
220
225
|
align={theme.direction === 'rtl' ? 'right' : 'left'}
|
|
221
226
|
data-index={staticColumnIndex}
|
|
222
227
|
data-pinned={!!isColumnPinned || undefined}
|
|
223
|
-
ref={(node: HTMLTableCellElement) => {
|
|
224
|
-
if (node) {
|
|
225
|
-
measureElement?.(node);
|
|
226
|
-
}
|
|
227
|
-
}}
|
|
228
228
|
{...tableCellProps}
|
|
229
|
+
onContextMenu={handleContextMenu}
|
|
229
230
|
onDoubleClick={handleDoubleClick}
|
|
230
231
|
onDragEnter={handleDragEnter}
|
|
231
232
|
sx={(theme) => ({
|
|
232
233
|
'&:hover': {
|
|
233
234
|
outline:
|
|
235
|
+
actionCell?.id === cell.id ||
|
|
234
236
|
(editDisplayMode === 'cell' && isEditable) ||
|
|
235
237
|
(editDisplayMode === 'table' && (isCreating || isEditing))
|
|
236
238
|
? `1px solid ${theme.palette.grey[500]}`
|
|
237
239
|
: undefined,
|
|
238
|
-
outlineOffset: '-1px',
|
|
239
240
|
textOverflow: 'clip',
|
|
240
241
|
},
|
|
241
242
|
alignItems: layoutMode?.startsWith('grid') ? 'center' : undefined,
|
|
242
|
-
cursor:
|
|
243
|
-
|
|
243
|
+
cursor: isRightClickable
|
|
244
|
+
? 'context-menu'
|
|
245
|
+
: isEditable && editDisplayMode === 'cell'
|
|
246
|
+
? 'pointer'
|
|
247
|
+
: 'inherit',
|
|
248
|
+
outline:
|
|
249
|
+
actionCell?.id === cell.id
|
|
250
|
+
? `1px solid ${theme.palette.grey[500]}`
|
|
251
|
+
: undefined,
|
|
252
|
+
outlineOffset: '-1px',
|
|
244
253
|
overflow: 'hidden',
|
|
245
254
|
p:
|
|
246
255
|
density === 'compact'
|
|
@@ -254,6 +263,7 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
|
|
|
254
263
|
: columnDefType === 'display'
|
|
255
264
|
? '1rem 1.25rem'
|
|
256
265
|
: '1.5rem',
|
|
266
|
+
|
|
257
267
|
textOverflow: columnDefType !== 'display' ? 'ellipsis' : undefined,
|
|
258
268
|
whiteSpace:
|
|
259
269
|
row.getIsPinned() || density === 'compact' ? 'nowrap' : 'normal',
|
|
@@ -293,8 +303,7 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
|
|
|
293
303
|
})
|
|
294
304
|
) : isCreating || isEditing ? (
|
|
295
305
|
<MRT_EditCellTextField cell={cell} table={table} />
|
|
296
|
-
) :
|
|
297
|
-
columnDef.enableClickToCopy !== false ? (
|
|
306
|
+
) : showClickToCopyButton && columnDef.enableClickToCopy !== false ? (
|
|
298
307
|
<MRT_CopyButton cell={cell} table={table}>
|
|
299
308
|
<MRT_TableBodyCellValue {...cellValueProps} />
|
|
300
309
|
</MRT_CopyButton>
|
|
@@ -234,10 +234,6 @@ export const MRT_TableBodyRow = <TData extends MRT_RowData>({
|
|
|
234
234
|
}
|
|
235
235
|
const props = {
|
|
236
236
|
cell,
|
|
237
|
-
measureElement:
|
|
238
|
-
!isDraggingRow && !isHoveredRow
|
|
239
|
-
? columnVirtualizer?.measureElement
|
|
240
|
-
: undefined,
|
|
241
237
|
numRows,
|
|
242
238
|
rowRef,
|
|
243
239
|
staticColumnIndex,
|
|
@@ -10,6 +10,7 @@ import { MRT_TableHeadCellGrabHandle } from './MRT_TableHeadCellGrabHandle';
|
|
|
10
10
|
import { MRT_TableHeadCellResizeHandle } from './MRT_TableHeadCellResizeHandle';
|
|
11
11
|
import { MRT_TableHeadCellSortLabel } from './MRT_TableHeadCellSortLabel';
|
|
12
12
|
import {
|
|
13
|
+
type MRT_ColumnVirtualizer,
|
|
13
14
|
type MRT_Header,
|
|
14
15
|
type MRT_RowData,
|
|
15
16
|
type MRT_TableInstance,
|
|
@@ -18,12 +19,14 @@ import { getCommonMRTCellStyles, getMRTTheme } from '../../utils/style.utils';
|
|
|
18
19
|
import { parseFromValuesOrFunc } from '../../utils/utils';
|
|
19
20
|
|
|
20
21
|
interface Props<TData extends MRT_RowData> extends TableCellProps {
|
|
22
|
+
columnVirtualizer?: MRT_ColumnVirtualizer;
|
|
21
23
|
header: MRT_Header<TData>;
|
|
22
24
|
staticColumnIndex?: number;
|
|
23
25
|
table: MRT_TableInstance<TData>;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
export const MRT_TableHeadCell = <TData extends MRT_RowData>({
|
|
29
|
+
columnVirtualizer,
|
|
27
30
|
header,
|
|
28
31
|
staticColumnIndex,
|
|
29
32
|
table,
|
|
@@ -161,6 +164,9 @@ export const MRT_TableHeadCell = <TData extends MRT_RowData>({
|
|
|
161
164
|
ref={(node: HTMLTableCellElement) => {
|
|
162
165
|
if (node) {
|
|
163
166
|
tableHeadCellRefs.current[column.id] = node;
|
|
167
|
+
if (columnDefType !== 'group') {
|
|
168
|
+
columnVirtualizer?.measureElement?.(node);
|
|
169
|
+
}
|
|
164
170
|
}
|
|
165
171
|
}}
|
|
166
172
|
{...tableCellProps}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import Box from '@mui/material/Box';
|
|
3
|
+
import IconButton from '@mui/material/IconButton';
|
|
4
|
+
import ListItemIcon from '@mui/material/ListItemIcon';
|
|
5
|
+
import MenuItem, { type MenuItemProps } from '@mui/material/MenuItem';
|
|
6
|
+
import { type MRT_RowData, type MRT_TableInstance } from '../../types';
|
|
7
|
+
|
|
8
|
+
interface Props<TData extends MRT_RowData> extends MenuItemProps {
|
|
9
|
+
icon: ReactNode;
|
|
10
|
+
label: string;
|
|
11
|
+
onOpenSubMenu?: MenuItemProps['onClick'] | MenuItemProps['onMouseEnter'];
|
|
12
|
+
table: MRT_TableInstance<TData>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const MRT_ActionMenuItem = <TData extends MRT_RowData>({
|
|
16
|
+
icon,
|
|
17
|
+
label,
|
|
18
|
+
onOpenSubMenu,
|
|
19
|
+
table,
|
|
20
|
+
...rest
|
|
21
|
+
}: Props<TData>) => {
|
|
22
|
+
const {
|
|
23
|
+
options: {
|
|
24
|
+
icons: { ArrowRightIcon },
|
|
25
|
+
},
|
|
26
|
+
} = table;
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<MenuItem
|
|
30
|
+
sx={{
|
|
31
|
+
alignItems: 'center',
|
|
32
|
+
justifyContent: 'space-between',
|
|
33
|
+
minWidth: '120px',
|
|
34
|
+
my: 0,
|
|
35
|
+
py: '6px',
|
|
36
|
+
}}
|
|
37
|
+
{...rest}
|
|
38
|
+
>
|
|
39
|
+
<Box
|
|
40
|
+
sx={{
|
|
41
|
+
alignItems: 'center',
|
|
42
|
+
display: 'flex',
|
|
43
|
+
}}
|
|
44
|
+
>
|
|
45
|
+
<ListItemIcon>{icon}</ListItemIcon>
|
|
46
|
+
{label}
|
|
47
|
+
</Box>
|
|
48
|
+
{onOpenSubMenu && (
|
|
49
|
+
<IconButton
|
|
50
|
+
onClick={onOpenSubMenu as any}
|
|
51
|
+
onMouseEnter={onOpenSubMenu as any}
|
|
52
|
+
size="small"
|
|
53
|
+
sx={{ p: 0 }}
|
|
54
|
+
>
|
|
55
|
+
<ArrowRightIcon />
|
|
56
|
+
</IconButton>
|
|
57
|
+
)}
|
|
58
|
+
</MenuItem>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import Menu, { type MenuProps } from '@mui/material/Menu';
|
|
2
|
+
import { useTheme } from '@mui/material/styles';
|
|
3
|
+
import { MRT_ActionMenuItem } from './MRT_ActionMenuItem';
|
|
4
|
+
import { type MRT_RowData, type MRT_TableInstance } from '../../types';
|
|
5
|
+
import { openEditingCell } from '../../utils/cell.utils';
|
|
6
|
+
import { getMRTTheme } from '../../utils/style.utils';
|
|
7
|
+
import { parseFromValuesOrFunc } from '../../utils/utils';
|
|
8
|
+
|
|
9
|
+
interface Props<TData extends MRT_RowData> extends Partial<MenuProps> {
|
|
10
|
+
table: MRT_TableInstance<TData>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const MRT_CellActionMenu = <TData extends MRT_RowData>({
|
|
14
|
+
table,
|
|
15
|
+
...rest
|
|
16
|
+
}: Props<TData>) => {
|
|
17
|
+
const {
|
|
18
|
+
getState,
|
|
19
|
+
options: {
|
|
20
|
+
editDisplayMode,
|
|
21
|
+
enableClickToCopy,
|
|
22
|
+
enableEditing,
|
|
23
|
+
icons: { ContentCopy, EditIcon },
|
|
24
|
+
localization,
|
|
25
|
+
renderCellActionMenuItems,
|
|
26
|
+
},
|
|
27
|
+
refs: { actionCellRef },
|
|
28
|
+
} = table;
|
|
29
|
+
const { actionCell, density } = getState();
|
|
30
|
+
const cell = actionCell!;
|
|
31
|
+
const { row } = cell;
|
|
32
|
+
const { column } = cell;
|
|
33
|
+
const { columnDef } = column;
|
|
34
|
+
|
|
35
|
+
const theme = useTheme();
|
|
36
|
+
const { menuBackgroundColor } = getMRTTheme(table, theme);
|
|
37
|
+
|
|
38
|
+
const handleClose = (event?: any) => {
|
|
39
|
+
event?.stopPropagation();
|
|
40
|
+
table.setActionCell(null);
|
|
41
|
+
actionCellRef.current = null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const internalMenuItems = [
|
|
45
|
+
(parseFromValuesOrFunc(enableClickToCopy, cell) === 'context-menu' ||
|
|
46
|
+
parseFromValuesOrFunc(columnDef.enableClickToCopy, cell) ===
|
|
47
|
+
'context-menu') && (
|
|
48
|
+
<MRT_ActionMenuItem
|
|
49
|
+
key={'mrt-copy'}
|
|
50
|
+
icon={<ContentCopy />}
|
|
51
|
+
label={localization.copy}
|
|
52
|
+
onClick={(event) => {
|
|
53
|
+
event.stopPropagation();
|
|
54
|
+
navigator.clipboard.writeText(cell.getValue() as string);
|
|
55
|
+
handleClose();
|
|
56
|
+
}}
|
|
57
|
+
table={table}
|
|
58
|
+
/>
|
|
59
|
+
),
|
|
60
|
+
parseFromValuesOrFunc(enableEditing, row) && editDisplayMode === 'cell' && (
|
|
61
|
+
<MRT_ActionMenuItem
|
|
62
|
+
key={'mrt-edit'}
|
|
63
|
+
icon={<EditIcon />}
|
|
64
|
+
label={localization.edit}
|
|
65
|
+
onClick={() => {
|
|
66
|
+
openEditingCell({ cell, table });
|
|
67
|
+
handleClose();
|
|
68
|
+
}}
|
|
69
|
+
table={table}
|
|
70
|
+
/>
|
|
71
|
+
),
|
|
72
|
+
].filter(Boolean);
|
|
73
|
+
|
|
74
|
+
const renderActionProps = {
|
|
75
|
+
cell,
|
|
76
|
+
closeMenu: handleClose,
|
|
77
|
+
column,
|
|
78
|
+
internalMenuItems,
|
|
79
|
+
row,
|
|
80
|
+
table,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const menuItems =
|
|
84
|
+
columnDef.renderCellActionMenuItems?.(renderActionProps) ??
|
|
85
|
+
renderCellActionMenuItems?.(renderActionProps);
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
(!!menuItems?.length || !!internalMenuItems?.length) && (
|
|
89
|
+
<Menu
|
|
90
|
+
MenuListProps={{
|
|
91
|
+
dense: density === 'compact',
|
|
92
|
+
sx: {
|
|
93
|
+
backgroundColor: menuBackgroundColor,
|
|
94
|
+
},
|
|
95
|
+
}}
|
|
96
|
+
anchorEl={actionCellRef.current}
|
|
97
|
+
disableScrollLock
|
|
98
|
+
onClick={(event) => event.stopPropagation()}
|
|
99
|
+
onClose={handleClose}
|
|
100
|
+
open={!!cell}
|
|
101
|
+
transformOrigin={{ horizontal: -100, vertical: 8 }}
|
|
102
|
+
{...rest}
|
|
103
|
+
>
|
|
104
|
+
{menuItems ?? internalMenuItems}
|
|
105
|
+
</Menu>
|
|
106
|
+
)
|
|
107
|
+
);
|
|
108
|
+
};
|