material-react-table 0.9.0 → 0.9.3

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>;
@@ -1,2 +1,3 @@
1
+ /// <reference types="react" />
1
2
  import { MaterialReactTableProps } from '../MaterialReactTable';
2
3
  export declare const MRT_TableRoot: <D extends Record<string, any> = {}>(props: MaterialReactTableProps<D>) => JSX.Element;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.9.0",
2
+ "version": "0.9.3",
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.",
@@ -63,14 +63,14 @@
63
63
  "@mui/icons-material": "^5.8.2",
64
64
  "@mui/material": "^5.8.2",
65
65
  "@size-limit/preset-small-lib": "^7.0.8",
66
- "@storybook/addon-a11y": "^6.5.6",
67
- "@storybook/addon-actions": "^6.5.6",
68
- "@storybook/addon-essentials": "^6.5.6",
66
+ "@storybook/addon-a11y": "^6.5.7",
67
+ "@storybook/addon-actions": "^6.5.7",
68
+ "@storybook/addon-essentials": "^6.5.7",
69
69
  "@storybook/addon-info": "^5.3.21",
70
- "@storybook/addon-links": "^6.5.6",
71
- "@storybook/addons": "^6.5.6",
72
- "@storybook/react": "^6.5.6",
73
- "@types/react": "^18.0.11",
70
+ "@storybook/addon-links": "^6.5.7",
71
+ "@storybook/addons": "^6.5.7",
72
+ "@storybook/react": "^6.5.7",
73
+ "@types/react": "^18.0.12",
74
74
  "@types/react-dom": "^18.0.5",
75
75
  "babel-loader": "^8.2.5",
76
76
  "eslint": "^8.17.0",
@@ -98,8 +98,9 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "@tanstack/match-sorter-utils": "^8.0.0-alpha.83",
101
- "@tanstack/react-table": "^8.0.0-beta.4",
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;
@@ -773,7 +774,7 @@ export type MaterialReactTableProps<D extends Record<string, any> = {}> =
773
774
  export default <D extends Record<string, any> = {}>({
774
775
  autoResetExpanded = false,
775
776
  columnResizeMode = 'onEnd',
776
- defaultColumn = { minSize: 50, maxSize: 1000, size: 150 },
777
+ defaultColumn = { minSize: 30, maxSize: 1000, size: 180 },
777
778
  editingMode = 'row',
778
779
  enableColumnActions = true,
779
780
  enableColumnFilters = true,
@@ -1,37 +1,78 @@
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) => {
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
+ tableInstance={tableInstance}
67
+ />
68
+ );
69
+ },
70
+ )}
71
+ {enableRowVirtualization && paddingBottom > 0 && (
72
+ <tr>
73
+ <td style={{ height: `${paddingBottom}px` }} />
74
+ </tr>
75
+ )}
35
76
  </TableBody>
36
77
  );
37
78
  };
@@ -23,11 +23,13 @@ export const MRT_TableBodyCell: FC<Props> = ({
23
23
  editingMode,
24
24
  enableClickToCopy,
25
25
  enableColumnOrdering,
26
+ enableColumnResizing,
26
27
  enableEditing,
27
- tableId,
28
+ enableRowVirtualization,
28
29
  muiTableBodyCellProps,
29
30
  muiTableBodyCellSkeletonProps,
30
31
  onMrtCellClick,
32
+ tableId,
31
33
  },
32
34
  setColumnOrder,
33
35
  setCurrentEditingCell,
@@ -131,6 +133,15 @@ export const MRT_TableBodyCell: FC<Props> = ({
131
133
  onMrtCellClick?.({ event, cell, tableInstance })
132
134
  }
133
135
  onDoubleClick={handleDoubleClick}
136
+ title={
137
+ (enableRowVirtualization || enableColumnResizing) &&
138
+ !columnDef?.Cell &&
139
+ !cell.getIsGrouped() &&
140
+ !columnDef.enableClickToCopy &&
141
+ typeof cell.getValue() === 'string'
142
+ ? (cell.getValue() as string)
143
+ : ''
144
+ }
134
145
  {...tableCellProps}
135
146
  ref={
136
147
  columnDefType === 'data' && enableColumnOrdering ? dropRef : undefined
@@ -149,6 +160,7 @@ export const MRT_TableBodyCell: FC<Props> = ({
149
160
  column.getIsPinned() === 'left'
150
161
  ? `${column.getStart('left')}px`
151
162
  : undefined,
163
+ overflow: 'hidden',
152
164
  p: isDensePadding
153
165
  ? columnDefType === 'display'
154
166
  ? '0 0.5rem'
@@ -163,15 +175,20 @@ export const MRT_TableBodyCell: FC<Props> = ({
163
175
  position: column.getIsPinned() ? 'sticky' : 'relative',
164
176
  right:
165
177
  column.getIsPinned() === 'right' ? `${getTotalRight()}px` : undefined,
178
+ textOverflow: 'ellipsis',
166
179
  transition: 'all 0.2s ease-in-out',
167
180
  whiteSpace: isDensePadding ? 'nowrap' : 'normal',
168
181
  zIndex: column.getIsPinned() ? 1 : undefined,
169
182
  '&:hover': {
170
- backgroundColor: enableHover
171
- ? theme.palette.mode === 'dark'
172
- ? `${lighten(theme.palette.background.default, 0.13)} !important`
173
- : `${darken(theme.palette.background.default, 0.07)} !important`
174
- : undefined,
183
+ backgroundColor:
184
+ enableHover && enableEditing && editingMode !== 'row'
185
+ ? theme.palette.mode === 'dark'
186
+ ? `${lighten(
187
+ theme.palette.background.default,
188
+ 0.13,
189
+ )} !important`
190
+ : `${darken(theme.palette.background.default, 0.07)} !important`
191
+ : undefined,
175
192
  },
176
193
  ...(tableCellProps?.sx as any),
177
194
  })}
@@ -11,6 +11,7 @@ interface Props {
11
11
 
12
12
  export const MRT_TableBodyRow: FC<Props> = ({ row, tableInstance }) => {
13
13
  const {
14
+ getIsSomeColumnsPinned,
14
15
  options: { muiTableBodyRowProps, onMrtRowClick, renderDetailPanel },
15
16
  } = tableInstance;
16
17
 
@@ -33,7 +34,7 @@ export const MRT_TableBodyRow: FC<Props> = ({ row, tableInstance }) => {
33
34
  transition: 'all 0.2s ease-in-out',
34
35
  '&:hover td': {
35
36
  backgroundColor:
36
- tableRowProps?.hover !== false
37
+ tableRowProps?.hover !== false && getIsSomeColumnsPinned()
37
38
  ? theme.palette.mode === 'dark'
38
39
  ? `${lighten(theme.palette.background.default, 0.12)}`
39
40
  : `${darken(theme.palette.background.default, 0.05)}`
@@ -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
  )}