material-react-table 0.20.0 → 0.22.1
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/README.md +90 -18
- package/dist/MaterialReactTable.d.ts +160 -153
- package/dist/body/MRT_TableBody.d.ts +3 -2
- package/dist/body/MRT_TableBodyCell.d.ts +3 -2
- package/dist/body/MRT_TableBodyRow.d.ts +3 -2
- package/dist/buttons/MRT_GrabHandleButton.d.ts +3 -2
- package/dist/filtersFns.d.ts +22 -22
- package/dist/head/MRT_TableHead.d.ts +4 -2
- package/dist/head/MRT_TableHeadCell.d.ts +4 -6
- package/dist/head/MRT_TableHeadRow.d.ts +4 -2
- package/dist/material-react-table.cjs.development.js +197 -4479
- package/dist/material-react-table.cjs.development.js.map +1 -1
- package/dist/material-react-table.cjs.production.min.js +1 -1
- package/dist/material-react-table.cjs.production.min.js.map +1 -1
- package/dist/material-react-table.esm.js +149 -148
- package/dist/material-react-table.esm.js.map +1 -1
- package/dist/menus/MRT_ShowHideColumnsMenuItems.d.ts +3 -1
- package/dist/sortingFns.d.ts +2 -2
- package/dist/table/MRT_TableRoot.d.ts +1 -1
- package/dist/utils.d.ts +7 -7
- package/package.json +7 -9
- package/src/MaterialReactTable.tsx +191 -176
- package/src/body/MRT_TableBody.tsx +17 -12
- package/src/body/MRT_TableBodyCell.tsx +15 -17
- package/src/body/MRT_TableBodyRow.tsx +10 -3
- package/src/buttons/MRT_GrabHandleButton.tsx +12 -5
- package/src/filtersFns.ts +22 -22
- package/src/head/MRT_TableHead.tsx +11 -3
- package/src/head/MRT_TableHeadCell.tsx +49 -20
- package/src/head/MRT_TableHeadRow.tsx +25 -20
- package/src/menus/MRT_ShowHideColumnsMenu.tsx +6 -1
- package/src/menus/MRT_ShowHideColumnsMenuItems.tsx +54 -27
- package/src/sortingFns.ts +6 -6
- package/src/table/MRT_Table.tsx +17 -4
- package/src/table/MRT_TableContainer.tsx +1 -1
- package/src/table/MRT_TablePaper.tsx +21 -25
- package/src/table/MRT_TableRoot.tsx +9 -10
- package/src/utils.ts +28 -22
- package/dist/head/MRT_DraggableTableHeadCell.d.ts +0 -8
- package/src/head/MRT_DraggableTableHeadCell.tsx +0 -43
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
|
|
1
|
+
import React, {
|
|
2
|
+
Dispatch,
|
|
3
|
+
DragEvent,
|
|
4
|
+
FC,
|
|
5
|
+
SetStateAction,
|
|
6
|
+
useState,
|
|
7
|
+
} from 'react';
|
|
3
8
|
import {
|
|
4
9
|
Box,
|
|
5
10
|
FormControlLabel,
|
|
@@ -16,12 +21,16 @@ import type { MRT_Column, MRT_TableInstance } from '..';
|
|
|
16
21
|
interface Props {
|
|
17
22
|
allColumns: MRT_Column[];
|
|
18
23
|
column: MRT_Column;
|
|
24
|
+
currentHoveredColumn: MRT_Column | null;
|
|
19
25
|
isSubMenu?: boolean;
|
|
26
|
+
setCurrentHoveredColumn: Dispatch<SetStateAction<MRT_Column | null>>;
|
|
20
27
|
table: MRT_TableInstance;
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
|
|
24
31
|
allColumns,
|
|
32
|
+
currentHoveredColumn,
|
|
33
|
+
setCurrentHoveredColumn,
|
|
25
34
|
column,
|
|
26
35
|
isSubMenu,
|
|
27
36
|
table,
|
|
@@ -40,22 +49,6 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
|
|
|
40
49
|
const { columnDef } = column;
|
|
41
50
|
const { columnDefType } = columnDef;
|
|
42
51
|
|
|
43
|
-
const [, dropRef] = useDrop({
|
|
44
|
-
accept: 'column',
|
|
45
|
-
drop: (movingColumn: MRT_Column) => {
|
|
46
|
-
const newColumnOrder = reorderColumn(movingColumn, column, columnOrder);
|
|
47
|
-
setColumnOrder(newColumnOrder);
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const [, dragRef, previewRef] = useDrag({
|
|
52
|
-
collect: (monitor) => ({
|
|
53
|
-
isDragging: monitor.isDragging(),
|
|
54
|
-
}),
|
|
55
|
-
item: () => column,
|
|
56
|
-
type: 'column',
|
|
57
|
-
});
|
|
58
|
-
|
|
59
52
|
const switchChecked =
|
|
60
53
|
(columnDefType !== 'group' && column.getIsVisible()) ||
|
|
61
54
|
(columnDefType === 'group' &&
|
|
@@ -71,41 +64,73 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
|
|
|
71
64
|
}
|
|
72
65
|
};
|
|
73
66
|
|
|
67
|
+
const menuItemRef = React.useRef<HTMLElement>(null);
|
|
68
|
+
|
|
69
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
70
|
+
|
|
71
|
+
const handleDragStart = (e: DragEvent<HTMLButtonElement>) => {
|
|
72
|
+
setIsDragging(true);
|
|
73
|
+
e.dataTransfer.setDragImage(menuItemRef.current as HTMLElement, 0, 0);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const handleDragEnd = (_e: DragEvent<HTMLButtonElement>) => {
|
|
77
|
+
setIsDragging(false);
|
|
78
|
+
setCurrentHoveredColumn(null);
|
|
79
|
+
if (currentHoveredColumn) {
|
|
80
|
+
setColumnOrder(reorderColumn(column, currentHoveredColumn, columnOrder));
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const handleDragEnter = (_e: DragEvent) => {
|
|
85
|
+
if (!isDragging) {
|
|
86
|
+
setCurrentHoveredColumn(column);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
74
90
|
return (
|
|
75
91
|
<>
|
|
76
92
|
<MenuItem
|
|
77
|
-
|
|
78
|
-
|
|
93
|
+
disableRipple={enableColumnOrdering}
|
|
94
|
+
ref={menuItemRef as any}
|
|
95
|
+
onDragEnter={handleDragEnter}
|
|
96
|
+
sx={(theme) => ({
|
|
79
97
|
alignItems: 'center',
|
|
80
98
|
justifyContent: 'flex-start',
|
|
81
99
|
my: 0,
|
|
100
|
+
opacity: isDragging ? 0.5 : 1,
|
|
101
|
+
outline: isDragging
|
|
102
|
+
? `1px dashed ${theme.palette.divider}`
|
|
103
|
+
: currentHoveredColumn?.id === column.id
|
|
104
|
+
? `2px dashed ${theme.palette.primary.main}`
|
|
105
|
+
: 'none',
|
|
82
106
|
pl: `${(column.depth + 0.5) * 2}rem`,
|
|
83
107
|
py: '6px',
|
|
84
|
-
}}
|
|
108
|
+
})}
|
|
85
109
|
>
|
|
86
110
|
<Box
|
|
87
|
-
ref={previewRef}
|
|
88
111
|
sx={{
|
|
89
112
|
display: 'flex',
|
|
90
113
|
flexWrap: 'nowrap',
|
|
91
114
|
gap: '8px',
|
|
92
115
|
}}
|
|
93
116
|
>
|
|
94
|
-
{
|
|
117
|
+
{!isSubMenu &&
|
|
118
|
+
columnDefType !== 'group' &&
|
|
95
119
|
enableColumnOrdering &&
|
|
96
120
|
!allColumns.some(
|
|
97
121
|
(col) => col.columnDef.columnDefType === 'group',
|
|
98
122
|
) &&
|
|
99
123
|
(columnDef.enableColumnOrdering !== false ? (
|
|
100
124
|
<MRT_GrabHandleButton
|
|
101
|
-
|
|
125
|
+
handleDragEnd={handleDragEnd}
|
|
126
|
+
handleDragStart={handleDragStart}
|
|
102
127
|
table={table}
|
|
103
128
|
/>
|
|
104
129
|
) : (
|
|
105
130
|
<Box sx={{ width: '28px' }} />
|
|
106
131
|
))}
|
|
107
|
-
{
|
|
108
|
-
|
|
132
|
+
{!isSubMenu &&
|
|
133
|
+
enablePinning &&
|
|
109
134
|
(column.getCanPin() ? (
|
|
110
135
|
<MRT_ColumnPinningButtons column={column} table={table} />
|
|
111
136
|
) : (
|
|
@@ -150,9 +175,11 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
|
|
|
150
175
|
{column.columns?.map((c: MRT_Column, i) => (
|
|
151
176
|
<MRT_ShowHideColumnsMenuItems
|
|
152
177
|
allColumns={allColumns}
|
|
153
|
-
key={`${i}-${c.id}`}
|
|
154
178
|
column={c}
|
|
179
|
+
currentHoveredColumn={currentHoveredColumn}
|
|
155
180
|
isSubMenu={isSubMenu}
|
|
181
|
+
key={`${i}-${c.id}`}
|
|
182
|
+
setCurrentHoveredColumn={setCurrentHoveredColumn}
|
|
156
183
|
table={table}
|
|
157
184
|
/>
|
|
158
185
|
))}
|
package/src/sortingFns.ts
CHANGED
|
@@ -2,9 +2,9 @@ import { compareItems, RankingInfo } from '@tanstack/match-sorter-utils';
|
|
|
2
2
|
import { Row, sortingFns } from '@tanstack/react-table';
|
|
3
3
|
import { MRT_Row } from '.';
|
|
4
4
|
|
|
5
|
-
const fuzzy = <
|
|
6
|
-
rowA: Row<
|
|
7
|
-
rowB: Row<
|
|
5
|
+
const fuzzy = <TData extends Record<string, any> = {}>(
|
|
6
|
+
rowA: Row<TData>,
|
|
7
|
+
rowB: Row<TData>,
|
|
8
8
|
columnId: string,
|
|
9
9
|
) => {
|
|
10
10
|
let dir = 0;
|
|
@@ -25,9 +25,9 @@ export const MRT_SortingFns = {
|
|
|
25
25
|
fuzzy,
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
export const rankGlobalFuzzy = <
|
|
29
|
-
rowA: MRT_Row<
|
|
30
|
-
rowB: MRT_Row<
|
|
28
|
+
export const rankGlobalFuzzy = <TData extends Record<string, any> = {}>(
|
|
29
|
+
rowA: MRT_Row<TData>,
|
|
30
|
+
rowB: MRT_Row<TData>,
|
|
31
31
|
) =>
|
|
32
32
|
Math.max(...Object.values(rowB.columnFiltersMeta).map((v: any) => v.rank)) -
|
|
33
33
|
Math.max(...Object.values(rowA.columnFiltersMeta).map((v: any) => v.rank));
|
package/src/table/MRT_Table.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import React, { FC, RefObject } from 'react';
|
|
1
|
+
import React, { FC, RefObject, useState } from 'react';
|
|
2
2
|
import { Table } from '@mui/material';
|
|
3
3
|
import { MRT_TableHead } from '../head/MRT_TableHead';
|
|
4
4
|
import { MRT_TableBody } from '../body/MRT_TableBody';
|
|
5
5
|
import { MRT_TableFooter } from '../footer/MRT_TableFooter';
|
|
6
|
-
import { MRT_TableInstance } from '..';
|
|
6
|
+
import { MRT_Column, MRT_TableInstance } from '..';
|
|
7
7
|
|
|
8
8
|
interface Props {
|
|
9
9
|
tableContainerRef: RefObject<HTMLDivElement>;
|
|
@@ -29,6 +29,9 @@ export const MRT_Table: FC<Props> = ({ tableContainerRef, table }) => {
|
|
|
29
29
|
? muiTableProps({ table })
|
|
30
30
|
: muiTableProps;
|
|
31
31
|
|
|
32
|
+
const [currentHoveredColumn, setCurrentHoveredColumn] =
|
|
33
|
+
useState<MRT_Column | null>(null);
|
|
34
|
+
|
|
32
35
|
return (
|
|
33
36
|
<Table
|
|
34
37
|
stickyHeader={
|
|
@@ -41,8 +44,18 @@ export const MRT_Table: FC<Props> = ({ tableContainerRef, table }) => {
|
|
|
41
44
|
...tableProps?.sx,
|
|
42
45
|
}}
|
|
43
46
|
>
|
|
44
|
-
{enableTableHead &&
|
|
45
|
-
|
|
47
|
+
{enableTableHead && (
|
|
48
|
+
<MRT_TableHead
|
|
49
|
+
currentHoveredColumn={currentHoveredColumn}
|
|
50
|
+
setCurrentHoveredColumn={setCurrentHoveredColumn}
|
|
51
|
+
table={table}
|
|
52
|
+
/>
|
|
53
|
+
)}
|
|
54
|
+
<MRT_TableBody
|
|
55
|
+
setCurrentHoveredColumn={setCurrentHoveredColumn}
|
|
56
|
+
tableContainerRef={tableContainerRef}
|
|
57
|
+
table={table}
|
|
58
|
+
/>
|
|
46
59
|
{enableTableFooter && <MRT_TableFooter table={table} />}
|
|
47
60
|
</Table>
|
|
48
61
|
);
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import React, { FC, useEffect } from 'react';
|
|
2
2
|
import { Paper } from '@mui/material';
|
|
3
|
-
import { DndProvider } from 'react-dnd';
|
|
4
|
-
import { HTML5Backend } from 'react-dnd-html5-backend';
|
|
5
3
|
import { MRT_ToolbarTop } from '../toolbar/MRT_ToolbarTop';
|
|
6
4
|
import { MRT_ToolbarBottom } from '../toolbar/MRT_ToolbarBottom';
|
|
7
5
|
import { MRT_TableContainer } from './MRT_TableContainer';
|
|
@@ -34,28 +32,26 @@ export const MRT_TablePaper: FC<Props> = ({ table }) => {
|
|
|
34
32
|
: muiTablePaperProps;
|
|
35
33
|
|
|
36
34
|
return (
|
|
37
|
-
<
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
style
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
</Paper>
|
|
59
|
-
</DndProvider>
|
|
35
|
+
<Paper
|
|
36
|
+
elevation={2}
|
|
37
|
+
{...tablePaperProps}
|
|
38
|
+
sx={{
|
|
39
|
+
transition: 'all 0.2s ease-in-out',
|
|
40
|
+
...tablePaperProps?.sx,
|
|
41
|
+
}}
|
|
42
|
+
style={{
|
|
43
|
+
...tablePaperProps?.style,
|
|
44
|
+
height: isFullScreen ? '100vh' : undefined,
|
|
45
|
+
margin: isFullScreen ? '0' : undefined,
|
|
46
|
+
maxHeight: isFullScreen ? '100vh' : undefined,
|
|
47
|
+
maxWidth: isFullScreen ? '100vw' : undefined,
|
|
48
|
+
padding: isFullScreen ? '0' : undefined,
|
|
49
|
+
width: isFullScreen ? '100vw' : undefined,
|
|
50
|
+
}}
|
|
51
|
+
>
|
|
52
|
+
{enableToolbarTop && <MRT_ToolbarTop table={table} />}
|
|
53
|
+
<MRT_TableContainer table={table} />
|
|
54
|
+
{enableToolbarBottom && <MRT_ToolbarBottom table={table} />}
|
|
55
|
+
</Paper>
|
|
60
56
|
);
|
|
61
57
|
};
|
|
@@ -32,8 +32,8 @@ import {
|
|
|
32
32
|
} from '../utils';
|
|
33
33
|
import { MRT_FilterFns } from '../filtersFns';
|
|
34
34
|
|
|
35
|
-
export const MRT_TableRoot = <
|
|
36
|
-
props: MaterialReactTableProps<
|
|
35
|
+
export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
|
|
36
|
+
props: MaterialReactTableProps<TData>,
|
|
37
37
|
) => {
|
|
38
38
|
const [tableId, setIdPrefix] = useState(props.tableId);
|
|
39
39
|
useEffect(
|
|
@@ -42,7 +42,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
|
42
42
|
[props.tableId],
|
|
43
43
|
);
|
|
44
44
|
|
|
45
|
-
const initialState: Partial<MRT_TableState<
|
|
45
|
+
const initialState: Partial<MRT_TableState<TData>> = useMemo(() => {
|
|
46
46
|
const initState = props.initialState ?? {};
|
|
47
47
|
initState.columnOrder =
|
|
48
48
|
initState.columnOrder ?? getDefaultColumnOrderIds(props);
|
|
@@ -53,10 +53,9 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
|
53
53
|
initialState.columnOrder ?? [],
|
|
54
54
|
);
|
|
55
55
|
const [currentEditingCell, setCurrentEditingCell] =
|
|
56
|
-
useState<MRT_Cell<
|
|
57
|
-
const [currentEditingRow, setCurrentEditingRow] =
|
|
58
|
-
initialState?.currentEditingRow ?? null
|
|
59
|
-
);
|
|
56
|
+
useState<MRT_Cell<TData> | null>(initialState?.currentEditingCell ?? null);
|
|
57
|
+
const [currentEditingRow, setCurrentEditingRow] =
|
|
58
|
+
useState<MRT_Row<TData> | null>(initialState?.currentEditingRow ?? null);
|
|
60
59
|
const [density, setDensity] = useState(
|
|
61
60
|
initialState?.density ?? 'comfortable',
|
|
62
61
|
);
|
|
@@ -78,7 +77,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
|
78
77
|
}>(() =>
|
|
79
78
|
Object.assign(
|
|
80
79
|
{},
|
|
81
|
-
...getAllLeafColumnDefs(props.columns as MRT_ColumnDef<
|
|
80
|
+
...getAllLeafColumnDefs(props.columns as MRT_ColumnDef<TData>[]).map(
|
|
82
81
|
(col) => ({
|
|
83
82
|
[col.id?.toString() ?? col.accessorKey?.toString() ?? '']:
|
|
84
83
|
col.filterFn instanceof Function
|
|
@@ -158,7 +157,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
|
158
157
|
muiTableHeadCellProps: props.muiTableHeadCellProps,
|
|
159
158
|
size: 60,
|
|
160
159
|
},
|
|
161
|
-
] as MRT_ColumnDef<
|
|
160
|
+
] as MRT_ColumnDef<TData>[]
|
|
162
161
|
).filter(Boolean),
|
|
163
162
|
[
|
|
164
163
|
columnOrder,
|
|
@@ -184,7 +183,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
|
|
|
184
183
|
[currentFilterFns, displayColumns, props.columns],
|
|
185
184
|
);
|
|
186
185
|
|
|
187
|
-
const data:
|
|
186
|
+
const data: TData[] = useMemo(
|
|
188
187
|
() =>
|
|
189
188
|
(props.state?.isLoading || props.state?.showSkeletons) &&
|
|
190
189
|
!props.data.length
|
package/src/utils.ts
CHANGED
|
@@ -9,21 +9,21 @@ import {
|
|
|
9
9
|
import { MRT_FilterFns } from './filtersFns';
|
|
10
10
|
import { MRT_SortingFns } from './sortingFns';
|
|
11
11
|
|
|
12
|
-
const getColumnId = <
|
|
13
|
-
columnDef: MRT_ColumnDef<
|
|
12
|
+
const getColumnId = <TData extends Record<string, any> = {}>(
|
|
13
|
+
columnDef: MRT_ColumnDef<TData>,
|
|
14
14
|
): string =>
|
|
15
15
|
columnDef.id ?? columnDef.accessorKey?.toString?.() ?? columnDef.header;
|
|
16
16
|
|
|
17
|
-
export const getAllLeafColumnDefs = <
|
|
18
|
-
columns: MRT_ColumnDef<
|
|
19
|
-
): MRT_ColumnDef<
|
|
20
|
-
let lowestLevelColumns: MRT_ColumnDef<
|
|
21
|
-
let currentCols: MRT_ColumnDef<
|
|
17
|
+
export const getAllLeafColumnDefs = <TData extends Record<string, any> = {}>(
|
|
18
|
+
columns: MRT_ColumnDef<TData>[],
|
|
19
|
+
): MRT_ColumnDef<TData>[] => {
|
|
20
|
+
let lowestLevelColumns: MRT_ColumnDef<TData>[] = columns;
|
|
21
|
+
let currentCols: MRT_ColumnDef<TData>[] | undefined = columns;
|
|
22
22
|
while (!!currentCols?.length && currentCols.some((col) => col.columns)) {
|
|
23
|
-
const nextCols: MRT_ColumnDef<
|
|
23
|
+
const nextCols: MRT_ColumnDef<TData>[] = currentCols
|
|
24
24
|
.filter((col) => !!col.columns)
|
|
25
25
|
.map((col) => col.columns)
|
|
26
|
-
.flat() as MRT_ColumnDef<
|
|
26
|
+
.flat() as MRT_ColumnDef<TData>[];
|
|
27
27
|
if (nextCols.every((col) => !col?.columns)) {
|
|
28
28
|
lowestLevelColumns = [...lowestLevelColumns, ...nextCols];
|
|
29
29
|
}
|
|
@@ -32,10 +32,10 @@ export const getAllLeafColumnDefs = <D extends Record<string, any> = {}>(
|
|
|
32
32
|
return lowestLevelColumns.filter((col) => !col.columns);
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
export const prepareColumns = <
|
|
36
|
-
columnDefs: MRT_ColumnDef<
|
|
35
|
+
export const prepareColumns = <TData extends Record<string, any> = {}>(
|
|
36
|
+
columnDefs: MRT_ColumnDef<TData>[],
|
|
37
37
|
currentFilterFns: { [key: string]: MRT_FilterOption },
|
|
38
|
-
): MRT_DefinedColumnDef<
|
|
38
|
+
): MRT_DefinedColumnDef<TData>[] =>
|
|
39
39
|
columnDefs.map((columnDef) => {
|
|
40
40
|
if (!columnDef.id) columnDef.id = getColumnId(columnDef);
|
|
41
41
|
if (process.env.NODE_ENV !== 'production' && !columnDef.id) {
|
|
@@ -58,11 +58,11 @@ export const prepareColumns = <D extends Record<string, any> = {}>(
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
return columnDef;
|
|
61
|
-
}) as MRT_DefinedColumnDef<
|
|
61
|
+
}) as MRT_DefinedColumnDef<TData>[];
|
|
62
62
|
|
|
63
|
-
export const reorderColumn = <
|
|
64
|
-
movingColumn: MRT_Column<
|
|
65
|
-
receivingColumn: MRT_Column<
|
|
63
|
+
export const reorderColumn = <TData extends Record<string, any> = {}>(
|
|
64
|
+
movingColumn: MRT_Column<TData>,
|
|
65
|
+
receivingColumn: MRT_Column<TData>,
|
|
66
66
|
columnOrder: ColumnOrderState,
|
|
67
67
|
): ColumnOrderState => {
|
|
68
68
|
if (movingColumn.getCanPin()) {
|
|
@@ -76,8 +76,10 @@ export const reorderColumn = <D extends Record<string, any> = {}>(
|
|
|
76
76
|
return [...columnOrder];
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
export const getLeadingDisplayColumnIds = <
|
|
80
|
-
|
|
79
|
+
export const getLeadingDisplayColumnIds = <
|
|
80
|
+
TData extends Record<string, any> = {},
|
|
81
|
+
>(
|
|
82
|
+
props: MaterialReactTableProps<TData>,
|
|
81
83
|
) =>
|
|
82
84
|
[
|
|
83
85
|
((props.positionActionsColumn === 'first' && props.enableRowActions) ||
|
|
@@ -88,16 +90,20 @@ export const getLeadingDisplayColumnIds = <D extends Record<string, any> = {}>(
|
|
|
88
90
|
props.enableRowNumbers && 'mrt-row-numbers',
|
|
89
91
|
].filter(Boolean) as string[];
|
|
90
92
|
|
|
91
|
-
export const getTrailingDisplayColumnIds = <
|
|
92
|
-
|
|
93
|
+
export const getTrailingDisplayColumnIds = <
|
|
94
|
+
TData extends Record<string, any> = {},
|
|
95
|
+
>(
|
|
96
|
+
props: MaterialReactTableProps<TData>,
|
|
93
97
|
) => [
|
|
94
98
|
((props.positionActionsColumn === 'last' && props.enableRowActions) ||
|
|
95
99
|
(props.enableEditing && props.editingMode === 'row')) &&
|
|
96
100
|
'mrt-row-actions',
|
|
97
101
|
];
|
|
98
102
|
|
|
99
|
-
export const getDefaultColumnOrderIds = <
|
|
100
|
-
|
|
103
|
+
export const getDefaultColumnOrderIds = <
|
|
104
|
+
TData extends Record<string, any> = {},
|
|
105
|
+
>(
|
|
106
|
+
props: MaterialReactTableProps<TData>,
|
|
101
107
|
) =>
|
|
102
108
|
[
|
|
103
109
|
...getLeadingDisplayColumnIds(props),
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import React, { FC } from 'react';
|
|
2
|
-
import { useDrag, useDrop } from 'react-dnd';
|
|
3
|
-
import { MRT_TableHeadCell } from './MRT_TableHeadCell';
|
|
4
|
-
import type { MRT_Column, MRT_Header, MRT_TableInstance } from '..';
|
|
5
|
-
import { reorderColumn } from '../utils';
|
|
6
|
-
|
|
7
|
-
interface Props {
|
|
8
|
-
header: MRT_Header;
|
|
9
|
-
table: MRT_TableInstance;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const MRT_DraggableTableHeadCell: FC<Props> = ({ header, table }) => {
|
|
13
|
-
const { getState, setColumnOrder } = table;
|
|
14
|
-
const { columnOrder } = getState();
|
|
15
|
-
const { column } = header;
|
|
16
|
-
|
|
17
|
-
const [, dropRef] = useDrop({
|
|
18
|
-
accept: 'column',
|
|
19
|
-
drop: (movingColumn: MRT_Column) => {
|
|
20
|
-
const newColumnOrder = reorderColumn(movingColumn, column, columnOrder);
|
|
21
|
-
setColumnOrder(newColumnOrder);
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const [{ isDragging }, dragRef, previewRef] = useDrag({
|
|
26
|
-
collect: (monitor) => ({
|
|
27
|
-
isDragging: monitor.isDragging(),
|
|
28
|
-
}),
|
|
29
|
-
item: () => column,
|
|
30
|
-
type: 'column',
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
return (
|
|
34
|
-
<MRT_TableHeadCell
|
|
35
|
-
dragRef={dragRef}
|
|
36
|
-
dropRef={dropRef}
|
|
37
|
-
header={header}
|
|
38
|
-
isDragging={isDragging}
|
|
39
|
-
previewRef={previewRef}
|
|
40
|
-
table={table}
|
|
41
|
-
/>
|
|
42
|
-
);
|
|
43
|
-
};
|