material-react-table 0.8.14 → 0.8.15

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/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { ColumnDef, Table } from '@tanstack/react-table';
2
- import { MRT_ColumnDef, MRT_FilterFn } from '.';
1
+ import { ColumnDef, ColumnOrderState, Table, Updater } from '@tanstack/react-table';
2
+ import { MRT_Column, MRT_ColumnDef, MRT_FilterFn } from '.';
3
3
  export declare const getAllLeafColumnDefs: (columns: MRT_ColumnDef[]) => MRT_ColumnDef[];
4
4
  export declare const createGroup: <D extends Record<string, any> = {}>(table: Table<D>, column: MRT_ColumnDef<D>, currentFilterFns: {
5
5
  [key: string]: MRT_FilterFn<{}>;
@@ -10,3 +10,4 @@ export declare const createDataColumn: <D extends Record<string, any> = {}>(tabl
10
10
  export declare const createDisplayColumn: <D extends Record<string, any> = {}>(table: Table<D>, column: Pick<MRT_ColumnDef<D>, "accessorFN" | "footer" | "columns" | "filterFn" | "id" | "accessorKey" | "accessorFn" | "cell" | "meta" | "enableHiding" | "enablePinning" | "enableColumnFilter" | "enableGlobalFilter" | "sortingFn" | "sortDescFirst" | "enableSorting" | "enableMultiSort" | "invertSorting" | "sortUndefined" | "aggregationFn" | "aggregatedCell" | "enableGrouping" | "enableResizing" | "size" | "minSize" | "maxSize" | "Edit" | "Filter" | "Footer" | "Header" | "Cell" | "enableClickToCopy" | "enableColumnActions" | "enableColumnOrdering" | "enableEditing" | "enabledColumnFilterOptions" | "filterSelectOptions" | "muiTableBodyCellCopyButtonProps" | "muiTableBodyCellEditTextFieldProps" | "muiTableBodyCellProps" | "muiTableFooterCellProps" | "muiTableHeadCellColumnActionsButtonProps" | "muiTableHeadCellFilterTextFieldProps" | "muiTableHeadCellProps" | "onCellEditBlur" | "onCellEditChange" | "onColumnFilterValueChange"> & {
11
11
  header?: string | undefined;
12
12
  }) => ColumnDef<D>;
13
+ export declare const reorderColumn: (movingColumn: MRT_Column, receivingColumn: MRT_Column, columnOrder: ColumnOrderState, setColumnOrder: (updater: Updater<ColumnOrderState>) => void) => void;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.8.14",
2
+ "version": "0.8.15",
3
3
  "license": "MIT",
4
4
  "name": "material-react-table",
5
5
  "description": "A fully featured Material UI implementation of TanStack React Table, inspired by material-table and the MUI X DataGrid, written from the ground up in TypeScript.",
@@ -80,11 +80,14 @@ export type MRT_TableInstance<D extends Record<string, any> = {}> = Omit<
80
80
  | 'getAllColumns'
81
81
  | 'getAllFlatColumns'
82
82
  | 'getAllLeafColumns'
83
+ | 'getCenterLeafColumns'
83
84
  | 'getColumn'
84
85
  | 'getExpandedRowModel'
85
86
  | 'getFlatHeaders'
87
+ | 'getLeftLeafColumns'
86
88
  | 'getPaginationRowModel'
87
89
  | 'getPrePaginationRowModel'
90
+ | 'getRightLeafColumns'
88
91
  | 'getRowModel'
89
92
  | 'getSelectedRowModel'
90
93
  | 'getState'
@@ -93,11 +96,14 @@ export type MRT_TableInstance<D extends Record<string, any> = {}> = Omit<
93
96
  getAllColumns: () => MRT_Column<D>[];
94
97
  getAllFlatColumns: () => MRT_Column<D>[];
95
98
  getAllLeafColumns: () => MRT_Column<D>[];
99
+ getCenterLeafColumns: () => MRT_Column<D>[];
96
100
  getColumn: (columnId: string) => MRT_Column<D>;
97
101
  getExpandedRowModel: () => MRT_RowModel<D>;
98
102
  getFlatHeaders: () => MRT_Header<D>[];
103
+ getLeftLeafColumns: () => MRT_Column<D>[];
99
104
  getPaginationRowModel: () => MRT_RowModel<D>;
100
105
  getPrePaginationRowModel: () => MRT_RowModel<D>;
106
+ getRightLeafColumns: () => MRT_Column<D>[];
101
107
  getRowModel: () => MRT_RowModel<D>;
102
108
  getSelectedRowModel: () => MRT_RowModel<D>;
103
109
  getState: () => MRT_TableState<D>;
@@ -1,8 +1,10 @@
1
1
  import React, { FC, MouseEvent, useMemo } from 'react';
2
+ import { useDrop } from 'react-dnd';
2
3
  import { alpha, darken, lighten, Skeleton, TableCell } from '@mui/material';
3
4
  import { MRT_EditCellTextField } from '../inputs/MRT_EditCellTextField';
4
- import type { MRT_Cell, MRT_TableInstance } from '..';
5
5
  import { MRT_CopyButton } from '../buttons/MRT_CopyButton';
6
+ import { reorderColumn } from '../utils';
7
+ import type { MRT_Cell, MRT_Column, MRT_TableInstance } from '..';
6
8
 
7
9
  interface Props {
8
10
  cell: MRT_Cell;
@@ -20,16 +22,19 @@ export const MRT_TableBodyCell: FC<Props> = ({
20
22
  options: {
21
23
  editingMode,
22
24
  enableClickToCopy,
25
+ enableColumnOrdering,
23
26
  enableEditing,
24
27
  idPrefix,
25
28
  muiTableBodyCellProps,
26
29
  muiTableBodyCellSkeletonProps,
27
30
  onCellClick,
28
31
  },
32
+ setColumnOrder,
29
33
  setCurrentEditingCell,
30
34
  } = tableInstance;
31
35
 
32
36
  const {
37
+ columnOrder,
33
38
  currentEditingCell,
34
39
  currentEditingRow,
35
40
  isDensePadding,
@@ -41,6 +46,12 @@ export const MRT_TableBodyCell: FC<Props> = ({
41
46
 
42
47
  const { columnDef, columnDefType } = column;
43
48
 
49
+ const [, dropRef] = useDrop({
50
+ accept: 'column',
51
+ drop: (movingColumn: MRT_Column) =>
52
+ reorderColumn(movingColumn, column, columnOrder, setColumnOrder),
53
+ });
54
+
44
55
  const mTableCellBodyProps =
45
56
  muiTableBodyCellProps instanceof Function
46
57
  ? muiTableBodyCellProps({ cell, tableInstance })
@@ -121,6 +132,9 @@ export const MRT_TableBodyCell: FC<Props> = ({
121
132
  }
122
133
  onDoubleClick={handleDoubleClick}
123
134
  {...tableCellProps}
135
+ ref={
136
+ columnDefType === 'data' && enableColumnOrdering ? dropRef : undefined
137
+ }
124
138
  sx={(theme) => ({
125
139
  backgroundColor: column.getIsPinned()
126
140
  ? alpha(lighten(theme.palette.background.default, 0.04), 0.95)
@@ -23,7 +23,7 @@ export const MRT_ColumnPinningButtons: FC<Props> = ({
23
23
  };
24
24
 
25
25
  return (
26
- <Box sx={{ mr: '4px' }}>
26
+ <Box sx={{ minWidth: '70px', textAlign: 'center' }}>
27
27
  {column.getIsPinned() ? (
28
28
  <Tooltip arrow title={localization.unpin}>
29
29
  <IconButton onClick={() => handlePinColumn(false)} size="small">
@@ -59,12 +59,12 @@ export const MRT_TableFooterCell: FC<Props> = ({ footer, tableInstance }) => {
59
59
  <>
60
60
  {footer.isPlaceholder
61
61
  ? null
62
- : columnDef.Footer instanceof Function
63
- ? columnDef.Footer?.({
64
- footer,
65
- tableInstance,
66
- })
67
- : columnDef.Footer ??
62
+ : (columnDef.Footer instanceof Function
63
+ ? columnDef.Footer?.({
64
+ footer,
65
+ tableInstance,
66
+ })
67
+ : columnDef.Footer) ??
68
68
  columnDef.footer ??
69
69
  footer.renderFooter() ??
70
70
  null}
@@ -1,7 +1,8 @@
1
1
  import React, { FC } from 'react';
2
2
  import { useDrag, useDrop } from 'react-dnd';
3
3
  import { MRT_TableHeadCell } from './MRT_TableHeadCell';
4
- import type { MRT_Header, MRT_TableInstance } from '..';
4
+ import type { MRT_Column, MRT_Header, MRT_TableInstance } from '..';
5
+ import { reorderColumn } from '../utils';
5
6
 
6
7
  interface Props {
7
8
  header: MRT_Header;
@@ -20,29 +21,20 @@ export const MRT_DraggableTableHeadCell: FC<Props> = ({
20
21
 
21
22
  const { columnOrder } = getState();
22
23
 
23
- const reorder = (movingHeader: MRT_Header, receivingHeader: MRT_Header) => {
24
- if (movingHeader.column.getCanPin()) {
25
- movingHeader.column.pin(receivingHeader.column.getIsPinned());
26
- }
27
- columnOrder.splice(
28
- receivingHeader.index,
29
- 0,
30
- columnOrder.splice(movingHeader.index, 1)[0],
31
- );
32
- setColumnOrder([...columnOrder]);
33
- };
24
+ const { column } = header;
34
25
 
35
26
  const [, dropRef] = useDrop({
36
- accept: 'header',
37
- drop: (movingHeader: MRT_Header) => reorder(movingHeader, header),
27
+ accept: 'column',
28
+ drop: (movingColumn: MRT_Column) =>
29
+ reorderColumn(movingColumn, column, columnOrder, setColumnOrder),
38
30
  });
39
31
 
40
32
  const [{ isDragging }, dragRef, previewRef] = useDrag({
41
33
  collect: (monitor) => ({
42
34
  isDragging: monitor.isDragging(),
43
35
  }),
44
- item: () => header,
45
- type: 'header',
36
+ item: () => column,
37
+ type: 'column',
46
38
  });
47
39
 
48
40
  return (
@@ -58,14 +58,14 @@ export const MRT_TableHeadCell: FC<Props> = ({
58
58
  ...mcTableHeadCellProps,
59
59
  };
60
60
 
61
- const headerElement = (
62
- columnDef?.Header instanceof Function
63
- ? columnDef?.Header?.({
64
- header,
65
- tableInstance,
66
- })
67
- : columnDef?.Header ?? header.renderHeader()
68
- ) as ReactNode;
61
+ const headerElement = ((columnDef?.Header instanceof Function
62
+ ? columnDef?.Header?.({
63
+ header,
64
+ tableInstance,
65
+ })
66
+ : columnDef?.Header) ??
67
+ header.renderHeader() ??
68
+ columnDef.header) as ReactNode;
69
69
 
70
70
  const getIsLastLeftPinnedColumn = () => {
71
71
  return (
@@ -92,7 +92,9 @@ export const MRT_TableHeadCell: FC<Props> = ({
92
92
  align={columnDefType === 'group' ? 'center' : 'left'}
93
93
  colSpan={header.colSpan}
94
94
  {...tableCellProps}
95
- ref={columnDefType === 'data' ? dropRef : undefined}
95
+ ref={
96
+ columnDefType === 'data' && enableColumnOrdering ? dropRef : undefined
97
+ }
96
98
  sx={(theme: Theme) => ({
97
99
  backgroundColor:
98
100
  column.getIsPinned() && columnDefType !== 'group'
@@ -123,7 +125,7 @@ export const MRT_TableHeadCell: FC<Props> = ({
123
125
  column.getIsPinned() && columnDefType !== 'group'
124
126
  ? 'sticky'
125
127
  : undefined,
126
- pt: columnDefType !== 'data' ? 0 : isDensePadding ? '0.25' : '.5rem',
128
+ pt: columnDefType !== 'data' ? 0 : isDensePadding ? '0.25' : '.75rem',
127
129
  right:
128
130
  column.getIsPinned() === 'right' ? `${getTotalRight()}px` : undefined,
129
131
  transition: `all ${enableColumnResizing ? 0 : '0.2s'} ease-in-out`,
@@ -19,9 +19,12 @@ export const MRT_ShowHideColumnsMenu: FC<Props> = ({
19
19
  const {
20
20
  getAllColumns,
21
21
  getAllLeafColumns,
22
+ getCenterLeafColumns,
22
23
  getIsAllColumnsVisible,
23
24
  getIsSomeColumnsPinned,
24
25
  getIsSomeColumnsVisible,
26
+ getLeftLeafColumns,
27
+ getRightLeafColumns,
25
28
  getState,
26
29
  toggleAllColumnsVisible,
27
30
  options: { localization, enablePinning, enableColumnOrdering },
@@ -41,13 +44,23 @@ export const MRT_ShowHideColumnsMenu: FC<Props> = ({
41
44
  columnOrder.length > 0 &&
42
45
  !columns.some((col) => col.columnDefType === 'group')
43
46
  ) {
44
- return (
45
- columnOrder.map((colId) => columns.find((col) => col.id === colId)) ??
46
- columns
47
- );
47
+ return [
48
+ ...getLeftLeafColumns(),
49
+ ...[...new Set(columnOrder)].map((colId) =>
50
+ getCenterLeafColumns().find((col) => col?.id === colId),
51
+ ),
52
+ ...getRightLeafColumns(),
53
+ ].filter(Boolean);
48
54
  }
49
55
  return columns;
50
- }, [getAllColumns(), columnOrder, columnPinning]) as MRT_Column[];
56
+ }, [
57
+ columnOrder,
58
+ columnPinning,
59
+ getAllColumns(),
60
+ getCenterLeafColumns(),
61
+ getLeftLeafColumns(),
62
+ getRightLeafColumns(),
63
+ ]) as MRT_Column[];
51
64
 
52
65
  return (
53
66
  <Menu
@@ -3,6 +3,7 @@ import { useDrag, useDrop } from 'react-dnd';
3
3
  import { Box, FormControlLabel, MenuItem, Switch } from '@mui/material';
4
4
  import { MRT_ColumnPinningButtons } from '../buttons/MRT_ColumnPinningButtons';
5
5
  import { MRT_GrabHandleButton } from '../buttons/MRT_GrabHandleButton';
6
+ import { reorderColumn } from '../utils';
6
7
  import type { MRT_Column, MRT_TableInstance } from '..';
7
8
 
8
9
  interface Props {
@@ -28,21 +29,10 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
28
29
 
29
30
  const { columnDef, columnDefType } = column;
30
31
 
31
- const reorder = (movingColumn: MRT_Column, receivingColumn: MRT_Column) => {
32
- if (movingColumn.getCanPin()) {
33
- movingColumn.pin(receivingColumn.getIsPinned());
34
- }
35
- columnOrder.splice(
36
- columnOrder.indexOf(receivingColumn.id),
37
- 0,
38
- columnOrder.splice(columnOrder.indexOf(movingColumn.id), 1)[0],
39
- );
40
- setColumnOrder([...columnOrder]);
41
- };
42
-
43
32
  const [, dropRef] = useDrop({
44
33
  accept: 'column',
45
- drop: (movingColumn: MRT_Column) => reorder(movingColumn, column),
34
+ drop: (movingColumn: MRT_Column) =>
35
+ reorderColumn(movingColumn, column, columnOrder, setColumnOrder),
46
36
  });
47
37
 
48
38
  const [, dragRef, previewRef] = useDrag({
@@ -85,7 +75,14 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
85
75
  py: '6px',
86
76
  }}
87
77
  >
88
- <Box ref={previewRef} sx={{ display: 'flex', flexWrap: 'nowrap' }}>
78
+ <Box
79
+ ref={previewRef}
80
+ sx={{
81
+ display: 'flex',
82
+ flexWrap: 'nowrap',
83
+ gap: '8px',
84
+ }}
85
+ >
89
86
  {columnDefType !== 'group' &&
90
87
  enableColumnOrdering &&
91
88
  columnDef.enableColumnOrdering !== false &&
@@ -112,10 +109,13 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
112
109
  }}
113
110
  checked={switchChecked}
114
111
  control={<Switch />}
115
- disabled={(isSubMenu && switchChecked) || !column.getCanHide()}
112
+ disabled={
113
+ (isSubMenu && switchChecked) ||
114
+ !column.getCanHide() ||
115
+ column.getIsGrouped()
116
+ }
116
117
  label={columnDef.header}
117
118
  onChange={() => handleToggleColumnHidden(column)}
118
- sx={{ ml: '4px' }}
119
119
  />
120
120
  </Box>
121
121
  </MenuItem>
@@ -12,6 +12,7 @@ import {
12
12
  ReactTableGenerics,
13
13
  getFacetedRowModel,
14
14
  TableState,
15
+ Table,
15
16
  } from '@tanstack/react-table';
16
17
  import {
17
18
  MRT_Cell,
@@ -57,7 +58,8 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
57
58
  const initState = props.initialState ?? {};
58
59
 
59
60
  initState.columnOrder =
60
- initState?.columnOrder ?? props.enableColumnOrdering
61
+ initState?.columnOrder ??
62
+ (props.enableColumnOrdering || props.enableGrouping)
61
63
  ? ([
62
64
  showActionColumn && 'mrt-row-actions',
63
65
  showExpandColumn && 'mrt-expand',
@@ -135,6 +137,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
135
137
 
136
138
  const table = useMemo(
137
139
  () =>
140
+ // @ts-ignore
138
141
  createTable().setOptions({
139
142
  //@ts-ignore
140
143
  filterFns: defaultFilterFNs,
@@ -148,7 +151,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
148
151
  getSubRows: (row) => (row as MRT_Row)?.subRows,
149
152
  idPrefix,
150
153
  initialState,
151
- }),
154
+ }) as Table<D>,
152
155
  [],
153
156
  );
154
157
 
@@ -165,6 +168,8 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
165
168
  ),
166
169
  header: props.localization?.actions,
167
170
  id: 'mrt-row-actions',
171
+ muiTableBodyCellProps: props.muiTableBodyCellProps,
172
+ muiTableHeadCellProps: props.muiTableHeadCellProps,
168
173
  size: 60,
169
174
  }),
170
175
  showExpandColumn &&
@@ -181,6 +186,8 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
181
186
  ) : null,
182
187
  header: props.localization?.expand,
183
188
  id: 'mrt-expand',
189
+ muiTableBodyCellProps: props.muiTableBodyCellProps,
190
+ muiTableHeadCellProps: props.muiTableHeadCellProps,
184
191
  size: 50,
185
192
  }),
186
193
  props.enableRowSelection &&
@@ -197,6 +204,8 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
197
204
  ) : null,
198
205
  header: props.localization?.select,
199
206
  id: 'mrt-select',
207
+ muiTableBodyCellProps: props.muiTableBodyCellProps,
208
+ muiTableHeadCellProps: props.muiTableHeadCellProps,
200
209
  size: 50,
201
210
  }),
202
211
  props.enableRowNumbers &&
@@ -205,6 +214,8 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
205
214
  Header: () => props.localization?.rowNumber,
206
215
  header: props.localization?.rowNumbers,
207
216
  id: 'mrt-row-numbers',
217
+ muiTableBodyCellProps: props.muiTableBodyCellProps,
218
+ muiTableHeadCellProps: props.muiTableHeadCellProps,
208
219
  size: 50,
209
220
  }),
210
221
  ].filter(Boolean),
package/src/utils.ts CHANGED
@@ -1,5 +1,10 @@
1
- import { ColumnDef, Table } from '@tanstack/react-table';
2
- import { MRT_ColumnDef, MRT_FilterFn } from '.';
1
+ import {
2
+ ColumnDef,
3
+ ColumnOrderState,
4
+ Table,
5
+ Updater,
6
+ } from '@tanstack/react-table';
7
+ import { MRT_Column, MRT_ColumnDef, MRT_FilterFn, } from '.';
3
8
  import { MRT_FILTER_OPTION } from './enums';
4
9
  import { defaultFilterFNs } from './filtersFNs';
5
10
 
@@ -52,3 +57,20 @@ export const createDisplayColumn = <D extends Record<string, any> = {}>(
52
57
  table: Table<D>,
53
58
  column: Omit<MRT_ColumnDef<D>, 'header'> & { header?: string },
54
59
  ): ColumnDef<D> => table.createDisplayColumn(column as ColumnDef<D>);
60
+
61
+ export const reorderColumn = (
62
+ movingColumn: MRT_Column,
63
+ receivingColumn: MRT_Column,
64
+ columnOrder: ColumnOrderState,
65
+ setColumnOrder: (updater: Updater<ColumnOrderState>) => void,
66
+ ) => {
67
+ if (movingColumn.getCanPin()) {
68
+ movingColumn.pin(receivingColumn.getIsPinned());
69
+ }
70
+ columnOrder.splice(
71
+ columnOrder.indexOf(receivingColumn.id),
72
+ 0,
73
+ columnOrder.splice(columnOrder.indexOf(movingColumn.id), 1)[0],
74
+ );
75
+ setColumnOrder([...columnOrder]);
76
+ };