material-react-table 0.9.1 → 0.9.4

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.
@@ -1,6 +1,7 @@
1
- import { FC } from 'react';
1
+ import { FC, RefObject } from 'react';
2
2
  import { MRT_TableInstance } from '..';
3
3
  interface Props {
4
+ tableContainerRef: RefObject<HTMLDivElement>;
4
5
  tableInstance: MRT_TableInstance;
5
6
  }
6
7
  export declare const MRT_Table: FC<Props>;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.9.1",
2
+ "version": "0.9.4",
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.",
@@ -100,6 +100,7 @@
100
100
  "@tanstack/match-sorter-utils": "^8.0.0-alpha.83",
101
101
  "@tanstack/react-table": "^8.0.0-beta.8",
102
102
  "react-dnd": "^16.0.1",
103
- "react-dnd-html5-backend": "^16.0.1"
103
+ "react-dnd-html5-backend": "^16.0.1",
104
+ "react-virtual": "^2.10.4"
104
105
  }
105
106
  }
@@ -352,6 +352,7 @@ export type MaterialReactTableProps<D extends Record<string, any> = {}> =
352
352
  enablePersistentState?: boolean;
353
353
  enableRowActions?: boolean;
354
354
  enableRowNumbers?: boolean;
355
+ enableRowVirtualization?: boolean;
355
356
  enableSelectAll?: boolean;
356
357
  enableStickyHeader?: boolean;
357
358
  enableTableFooter?: boolean;
@@ -766,6 +767,7 @@ export type MaterialReactTableProps<D extends Record<string, any> = {}> =
766
767
  IconButtonProps & { tableInstance: MRT_TableInstance<D> }
767
768
  >;
768
769
  }) => ReactNode;
770
+ rowNumberMode?: 'original' | 'static';
769
771
  selectAllMode?: 'all' | 'page';
770
772
  tableId?: string;
771
773
  };
@@ -773,7 +775,7 @@ export type MaterialReactTableProps<D extends Record<string, any> = {}> =
773
775
  export default <D extends Record<string, any> = {}>({
774
776
  autoResetExpanded = false,
775
777
  columnResizeMode = 'onEnd',
776
- defaultColumn = { minSize: 50, maxSize: 1000, size: 150 },
778
+ defaultColumn = { minSize: 30, maxSize: 1000, size: 180 },
777
779
  editingMode = 'row',
778
780
  enableColumnActions = true,
779
781
  enableColumnFilters = true,
@@ -805,6 +807,7 @@ export default <D extends Record<string, any> = {}>({
805
807
  positionGlobalFilter = 'right',
806
808
  positionToolbarActions = 'top',
807
809
  positionToolbarAlertBanner = 'top',
810
+ rowNumberMode = 'original',
808
811
  selectAllMode = 'all',
809
812
  ...rest
810
813
  }: MaterialReactTableProps<D>) => (
@@ -843,6 +846,7 @@ export default <D extends Record<string, any> = {}>({
843
846
  positionPagination={positionPagination}
844
847
  positionToolbarActions={positionToolbarActions}
845
848
  positionToolbarAlertBanner={positionToolbarAlertBanner}
849
+ rowNumberMode={rowNumberMode}
846
850
  selectAllMode={selectAllMode}
847
851
  {...rest}
848
852
  />
@@ -1,37 +1,81 @@
1
- import React, { FC } from 'react';
1
+ import React, { FC, RefObject } from 'react';
2
+ import { useVirtual } from 'react-virtual';
2
3
  import { TableBody } from '@mui/material';
3
4
  import { MRT_TableBodyRow } from './MRT_TableBodyRow';
4
- import { MRT_TableInstance } from '..';
5
+ import type { MRT_Row, MRT_TableInstance } from '..';
5
6
 
6
7
  interface Props {
7
8
  tableInstance: MRT_TableInstance;
9
+ tableContainerRef: RefObject<HTMLDivElement>;
8
10
  }
9
11
 
10
- export const MRT_TableBody: FC<Props> = ({ tableInstance }) => {
12
+ export const MRT_TableBody: FC<Props> = ({
13
+ tableInstance,
14
+ tableContainerRef,
15
+ }) => {
11
16
  const {
12
17
  getPaginationRowModel,
13
18
  getPrePaginationRowModel,
14
- options: { enablePagination, muiTableBodyProps },
19
+ getState,
20
+ options: { enablePagination, enableRowVirtualization, muiTableBodyProps },
15
21
  } = tableInstance;
16
22
 
17
- const rows = enablePagination
18
- ? getPaginationRowModel().rows
19
- : getPrePaginationRowModel().rows;
23
+ const { isDensePadding } = getState();
20
24
 
21
25
  const tableBodyProps =
22
26
  muiTableBodyProps instanceof Function
23
27
  ? muiTableBodyProps({ tableInstance })
24
28
  : muiTableBodyProps;
25
29
 
30
+ const rows = enablePagination
31
+ ? getPaginationRowModel().rows
32
+ : getPrePaginationRowModel().rows;
33
+
34
+ const rowVirtualizer = enableRowVirtualization
35
+ ? useVirtual({
36
+ overscan: isDensePadding ? 15 : 5,
37
+ size: rows.length,
38
+ parentRef: tableContainerRef,
39
+ })
40
+ : ({} as any);
41
+
42
+ const { virtualItems: virtualRows } = rowVirtualizer;
43
+ const paddingTop = virtualRows?.length > 0 ? virtualRows[0].start : 0;
44
+ const paddingBottom =
45
+ virtualRows?.length > 0
46
+ ? rowVirtualizer.totalSize - virtualRows[virtualRows.length - 1].end
47
+ : 0;
48
+
26
49
  return (
27
50
  <TableBody {...tableBodyProps}>
28
- {rows.map((row) => (
29
- <MRT_TableBodyRow
30
- key={row.id}
31
- row={row}
32
- tableInstance={tableInstance}
33
- />
34
- ))}
51
+ {enableRowVirtualization && paddingTop > 0 && (
52
+ <tr>
53
+ <td style={{ height: `${paddingTop}px` }} />
54
+ </tr>
55
+ )}
56
+ {/* @ts-ignore */}
57
+ {(enableRowVirtualization ? virtualRows : rows).map(
58
+ (rowOrVirtualRow: any, rowIndex: number) => {
59
+ const row = enableRowVirtualization
60
+ ? (rows[rowOrVirtualRow.index] as MRT_Row)
61
+ : (rowOrVirtualRow as MRT_Row);
62
+ return (
63
+ <MRT_TableBodyRow
64
+ key={row.id}
65
+ row={row}
66
+ rowIndex={
67
+ enableRowVirtualization ? rowOrVirtualRow.index : rowIndex
68
+ }
69
+ tableInstance={tableInstance}
70
+ />
71
+ );
72
+ },
73
+ )}
74
+ {enableRowVirtualization && paddingBottom > 0 && (
75
+ <tr>
76
+ <td style={{ height: `${paddingBottom}px` }} />
77
+ </tr>
78
+ )}
35
79
  </TableBody>
36
80
  );
37
81
  };
@@ -9,12 +9,14 @@ import type { MRT_Cell, MRT_Column, MRT_TableInstance } from '..';
9
9
  interface Props {
10
10
  cell: MRT_Cell;
11
11
  enableHover?: boolean;
12
+ rowIndex: number;
12
13
  tableInstance: MRT_TableInstance;
13
14
  }
14
15
 
15
16
  export const MRT_TableBodyCell: FC<Props> = ({
16
17
  cell,
17
18
  enableHover,
19
+ rowIndex,
18
20
  tableInstance,
19
21
  }) => {
20
22
  const {
@@ -23,11 +25,15 @@ export const MRT_TableBodyCell: FC<Props> = ({
23
25
  editingMode,
24
26
  enableClickToCopy,
25
27
  enableColumnOrdering,
28
+ enableColumnResizing,
26
29
  enableEditing,
27
- tableId,
30
+ enableRowNumbers,
31
+ enableRowVirtualization,
28
32
  muiTableBodyCellProps,
29
33
  muiTableBodyCellSkeletonProps,
30
34
  onMrtCellClick,
35
+ rowNumberMode,
36
+ tableId,
31
37
  },
32
38
  setColumnOrder,
33
39
  setCurrentEditingCell,
@@ -131,6 +137,15 @@ export const MRT_TableBodyCell: FC<Props> = ({
131
137
  onMrtCellClick?.({ event, cell, tableInstance })
132
138
  }
133
139
  onDoubleClick={handleDoubleClick}
140
+ title={
141
+ (enableRowVirtualization || enableColumnResizing) &&
142
+ !columnDef?.Cell &&
143
+ !cell.getIsGrouped() &&
144
+ !columnDef.enableClickToCopy &&
145
+ typeof cell.getValue() === 'string'
146
+ ? (cell.getValue() as string)
147
+ : ''
148
+ }
134
149
  {...tableCellProps}
135
150
  ref={
136
151
  columnDefType === 'data' && enableColumnOrdering ? dropRef : undefined
@@ -149,6 +164,7 @@ export const MRT_TableBodyCell: FC<Props> = ({
149
164
  column.getIsPinned() === 'left'
150
165
  ? `${column.getStart('left')}px`
151
166
  : undefined,
167
+ overflow: 'hidden',
152
168
  p: isDensePadding
153
169
  ? columnDefType === 'display'
154
170
  ? '0 0.5rem'
@@ -163,6 +179,7 @@ export const MRT_TableBodyCell: FC<Props> = ({
163
179
  position: column.getIsPinned() ? 'sticky' : 'relative',
164
180
  right:
165
181
  column.getIsPinned() === 'right' ? `${getTotalRight()}px` : undefined,
182
+ textOverflow: 'ellipsis',
166
183
  transition: 'all 0.2s ease-in-out',
167
184
  whiteSpace: isDensePadding ? 'nowrap' : 'normal',
168
185
  zIndex: column.getIsPinned() ? 1 : undefined,
@@ -193,6 +210,10 @@ export const MRT_TableBodyCell: FC<Props> = ({
193
210
  width={skeletonWidth}
194
211
  {...muiTableBodyCellSkeletonProps}
195
212
  />
213
+ ) : enableRowNumbers &&
214
+ rowNumberMode === 'static' &&
215
+ column.id === 'mrt-row-numbers' ? (
216
+ rowIndex + 1
196
217
  ) : columnDefType === 'display' ? (
197
218
  columnDef.Cell?.({ cell, tableInstance })
198
219
  ) : cell.getIsPlaceholder() ||
@@ -6,10 +6,11 @@ import type { MRT_Row, MRT_TableInstance } from '..';
6
6
 
7
7
  interface Props {
8
8
  row: MRT_Row;
9
+ rowIndex: number;
9
10
  tableInstance: MRT_TableInstance;
10
11
  }
11
12
 
12
- export const MRT_TableBodyRow: FC<Props> = ({ row, tableInstance }) => {
13
+ export const MRT_TableBodyRow: FC<Props> = ({ row, rowIndex, tableInstance }) => {
13
14
  const {
14
15
  getIsSomeColumnsPinned,
15
16
  options: { muiTableBodyRowProps, onMrtRowClick, renderDetailPanel },
@@ -48,6 +49,7 @@ export const MRT_TableBodyRow: FC<Props> = ({ row, tableInstance }) => {
48
49
  cell={cell}
49
50
  key={cell.id}
50
51
  enableHover={tableRowProps?.hover !== false}
52
+ rowIndex={rowIndex}
51
53
  tableInstance={tableInstance}
52
54
  />
53
55
  ))}
@@ -1,4 +1,4 @@
1
- import React, { FC } from 'react';
1
+ import React, { FC, RefObject } 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';
@@ -6,12 +6,15 @@ import { MRT_TableFooter } from '../footer/MRT_TableFooter';
6
6
  import { MRT_TableInstance } from '..';
7
7
 
8
8
  interface Props {
9
+ tableContainerRef: RefObject<HTMLDivElement>;
9
10
  tableInstance: MRT_TableInstance;
10
11
  }
11
12
 
12
- export const MRT_Table: FC<Props> = ({ tableInstance }) => {
13
+ export const MRT_Table: FC<Props> = ({ tableContainerRef, tableInstance }) => {
13
14
  const {
14
15
  options: {
16
+ enableColumnResizing,
17
+ enableRowVirtualization,
15
18
  enableStickyHeader,
16
19
  enableTableFooter,
17
20
  enableTableHead,
@@ -25,9 +28,20 @@ export const MRT_Table: FC<Props> = ({ tableInstance }) => {
25
28
  : muiTableProps;
26
29
 
27
30
  return (
28
- <Table stickyHeader={enableStickyHeader} {...tableProps}>
31
+ <Table
32
+ stickyHeader={enableStickyHeader}
33
+ {...tableProps}
34
+ sx={{
35
+ tableLayout:
36
+ enableColumnResizing || enableRowVirtualization ? 'fixed' : undefined,
37
+ ...tableProps?.sx,
38
+ }}
39
+ >
29
40
  {enableTableHead && <MRT_TableHead tableInstance={tableInstance} />}
30
- <MRT_TableBody tableInstance={tableInstance} />
41
+ <MRT_TableBody
42
+ tableContainerRef={tableContainerRef}
43
+ tableInstance={tableInstance}
44
+ />
31
45
  {enableTableFooter && <MRT_TableFooter tableInstance={tableInstance} />}
32
46
  </Table>
33
47
  );
@@ -13,7 +13,7 @@ interface Props {
13
13
  export const MRT_TableContainer: FC<Props> = ({ tableInstance }) => {
14
14
  const {
15
15
  getState,
16
- options: { enableStickyHeader, tableId, muiTableContainerProps },
16
+ options: { enableStickyHeader, muiTableContainerProps, tableId },
17
17
  } = tableInstance;
18
18
 
19
19
  const { isFullScreen } = getState();
@@ -41,9 +41,12 @@ export const MRT_TableContainer: FC<Props> = ({ tableInstance }) => {
41
41
  setTotalToolbarHeight(topToolbarHeight + bottomToolbarHeight);
42
42
  });
43
43
 
44
+ const tableContainerRef = React.useRef<HTMLDivElement>(null);
45
+
44
46
  return (
45
47
  <TableContainer
46
48
  {...tableContainerProps}
49
+ ref={tableContainerRef}
47
50
  sx={{
48
51
  maxWidth: '100%',
49
52
  maxHeight: enableStickyHeader
@@ -58,7 +61,10 @@ export const MRT_TableContainer: FC<Props> = ({ tableInstance }) => {
58
61
  : undefined,
59
62
  }}
60
63
  >
61
- <MRT_Table tableInstance={tableInstance} />
64
+ <MRT_Table
65
+ tableContainerRef={tableContainerRef}
66
+ tableInstance={tableInstance}
67
+ />
62
68
  </TableContainer>
63
69
  );
64
70
  };
@@ -58,7 +58,7 @@ export const MRT_ToolbarBottom: FC<Props> = ({ tableInstance }) => {
58
58
  } as any)
59
59
  }
60
60
  >
61
- <MRT_LinearProgressBar alignTo='top' tableInstance={tableInstance} />
61
+ <MRT_LinearProgressBar alignTo="top" tableInstance={tableInstance} />
62
62
  {positionToolbarAlertBanner === 'bottom' && (
63
63
  <MRT_ToolbarAlertBanner tableInstance={tableInstance} />
64
64
  )}