material-react-table 0.9.1 → 0.9.2

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.2",
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;
@@ -1,37 +1,76 @@
1
- import React, { FC } from 'react';
1
+ import React, { FC, RefObject } from 'react';
2
2
  import { TableBody } from '@mui/material';
3
3
  import { MRT_TableBodyRow } from './MRT_TableBodyRow';
4
- import { MRT_TableInstance } from '..';
4
+ import { MRT_Row, MRT_TableInstance } from '..';
5
+ import { useVirtual } from 'react-virtual';
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 = useVirtual({
35
+ overscan: isDensePadding ? 15 : 5,
36
+ size: rows.length,
37
+ parentRef: tableContainerRef,
38
+ });
39
+
40
+ const { virtualItems: virtualRows } = rowVirtualizer;
41
+ const paddingTop = virtualRows.length > 0 ? virtualRows[0].start : 0;
42
+ const paddingBottom =
43
+ virtualRows.length > 0
44
+ ? rowVirtualizer.totalSize - virtualRows[virtualRows.length - 1].end
45
+ : 0;
46
+
26
47
  return (
27
48
  <TableBody {...tableBodyProps}>
28
- {rows.map((row) => (
29
- <MRT_TableBodyRow
30
- key={row.id}
31
- row={row}
32
- tableInstance={tableInstance}
33
- />
34
- ))}
49
+ {enableRowVirtualization && paddingTop > 0 && (
50
+ <tr>
51
+ <td style={{ height: `${paddingTop}px` }} />
52
+ </tr>
53
+ )}
54
+ {/* @ts-ignore */}
55
+ {(enableRowVirtualization ? virtualRows : rows).map(
56
+ (rowOrVirtualRow: any) => {
57
+ const row = enableRowVirtualization
58
+ ? (rows[rowOrVirtualRow.index] as MRT_Row)
59
+ : (rowOrVirtualRow as MRT_Row);
60
+ return (
61
+ <MRT_TableBodyRow
62
+ key={row.id}
63
+ row={row}
64
+ tableInstance={tableInstance}
65
+ />
66
+ );
67
+ },
68
+ )}
69
+ {enableRowVirtualization && paddingBottom > 0 && (
70
+ <tr>
71
+ <td style={{ height: `${paddingBottom}px` }} />
72
+ </tr>
73
+ )}
35
74
  </TableBody>
36
75
  );
37
76
  };
@@ -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,10 +6,14 @@ 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> = ({
14
+ tableContainerRef,
15
+ tableInstance
16
+ }) => {
13
17
  const {
14
18
  options: {
15
19
  enableStickyHeader,
@@ -27,7 +31,10 @@ export const MRT_Table: FC<Props> = ({ tableInstance }) => {
27
31
  return (
28
32
  <Table stickyHeader={enableStickyHeader} {...tableProps}>
29
33
  {enableTableHead && <MRT_TableHead tableInstance={tableInstance} />}
30
- <MRT_TableBody tableInstance={tableInstance} />
34
+ <MRT_TableBody
35
+ tableContainerRef={tableContainerRef}
36
+ tableInstance={tableInstance}
37
+ />
31
38
  {enableTableFooter && <MRT_TableFooter tableInstance={tableInstance} />}
32
39
  </Table>
33
40
  );
@@ -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
  };