material-react-table 0.14.6 → 0.15.0

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.
@@ -2,3 +2,4 @@ import type { MRT_Row } from '.';
2
2
  export declare const MRT_SortingFns: {
3
3
  fuzzy: (rowA: MRT_Row, rowB: MRT_Row, columnId: string) => number;
4
4
  };
5
+ export declare const rankGlobalFuzzy: (rowA: MRT_Row, rowB: MRT_Row) => number;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.14.6",
2
+ "version": "0.15.0",
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.",
@@ -88,6 +88,7 @@ export type MRT_TableInstance<D extends Record<string, any> = {}> = Omit<
88
88
  | 'getFlatHeaders'
89
89
  | 'getLeftLeafColumns'
90
90
  | 'getPaginationRowModel'
91
+ | 'getPreFilteredRowModel'
91
92
  | 'getPrePaginationRowModel'
92
93
  | 'getRightLeafColumns'
93
94
  | 'getRowModel'
@@ -104,6 +105,7 @@ export type MRT_TableInstance<D extends Record<string, any> = {}> = Omit<
104
105
  getFlatHeaders: () => MRT_Header<D>[];
105
106
  getLeftLeafColumns: () => MRT_Column<D>[];
106
107
  getPaginationRowModel: () => MRT_RowModel<D>;
108
+ getPreFilteredRowModel: () => MRT_RowModel<D>;
107
109
  getPrePaginationRowModel: () => MRT_RowModel<D>;
108
110
  getRightLeafColumns: () => MRT_Column<D>[];
109
111
  getRowModel: () => MRT_RowModel<D>;
@@ -395,6 +397,7 @@ export type MaterialReactTableProps<D extends Record<string, any> = {}> =
395
397
  enableExpandAll?: boolean;
396
398
  enableFullScreenToggle?: boolean;
397
399
  enableGlobalFilterChangeMode?: boolean;
400
+ enableGlobalFilterRankedResults?: boolean;
398
401
  enablePagination?: boolean;
399
402
  enablePersistentState?: boolean;
400
403
  enableRowActions?: boolean;
@@ -848,9 +851,11 @@ export default <D extends Record<string, any> = {}>({
848
851
  enableFullScreenToggle = true,
849
852
  enableGlobalFilter = true,
850
853
  enableGlobalFilterChangeMode = true,
854
+ enableGlobalFilterRankedResults = true,
851
855
  enableGrouping = false,
852
856
  enableHiding = true,
853
857
  enableMultiRowSelection = true,
858
+ enableMultiSort = true,
854
859
  enablePagination = true,
855
860
  enablePinning = false,
856
861
  enableRowSelection = false,
@@ -889,9 +894,11 @@ export default <D extends Record<string, any> = {}>({
889
894
  enableFullScreenToggle={enableFullScreenToggle}
890
895
  enableGlobalFilter={enableGlobalFilter}
891
896
  enableGlobalFilterChangeMode={enableGlobalFilterChangeMode}
897
+ enableGlobalFilterRankedResults={enableGlobalFilterRankedResults}
892
898
  enableGrouping={enableGrouping}
893
899
  enableHiding={enableHiding}
894
900
  enableMultiRowSelection={enableMultiRowSelection}
901
+ enableMultiSort={enableMultiSort}
895
902
  enablePagination={enablePagination}
896
903
  enablePinning={enablePinning}
897
904
  enableRowSelection={enableRowSelection}
@@ -1,8 +1,9 @@
1
- import React, { FC, RefObject } from 'react';
1
+ import React, { FC, RefObject, useMemo } from 'react';
2
2
  import { useVirtual } from 'react-virtual';
3
3
  import { TableBody } from '@mui/material';
4
4
  import { MRT_TableBodyRow } from './MRT_TableBodyRow';
5
5
  import type { MRT_Row, MRT_TableInstance } from '..';
6
+ import { rankGlobalFuzzy } from '../sortingFns';
6
7
 
7
8
  interface Props {
8
9
  instance: MRT_TableInstance;
@@ -15,6 +16,7 @@ export const MRT_TableBody: FC<Props> = ({ instance, tableContainerRef }) => {
15
16
  getPrePaginationRowModel,
16
17
  getState,
17
18
  options: {
19
+ enableGlobalFilterRankedResults,
18
20
  enablePagination,
19
21
  enableRowVirtualization,
20
22
  muiTableBodyProps,
@@ -22,20 +24,38 @@ export const MRT_TableBody: FC<Props> = ({ instance, tableContainerRef }) => {
22
24
  },
23
25
  } = instance;
24
26
 
25
- const { density } = getState();
27
+ const { density, globalFilter, pagination } = getState();
26
28
 
27
29
  const tableBodyProps =
28
30
  muiTableBodyProps instanceof Function
29
31
  ? muiTableBodyProps({ instance })
30
32
  : muiTableBodyProps;
31
33
 
32
- const rows = enablePagination
33
- ? getPaginationRowModel().rows
34
- : getPrePaginationRowModel().rows;
34
+ const rows = useMemo(() => {
35
+ if (enableGlobalFilterRankedResults && globalFilter) {
36
+ const rankedRows = getPrePaginationRowModel().rows.sort((a, b) =>
37
+ rankGlobalFuzzy(a, b),
38
+ );
39
+ if (enablePagination) {
40
+ return rankedRows.slice(0, pagination.pageSize);
41
+ }
42
+ return rankedRows;
43
+ }
44
+
45
+ return enablePagination
46
+ ? getPaginationRowModel().rows
47
+ : getPrePaginationRowModel().rows;
48
+ }, [
49
+ enableGlobalFilterRankedResults,
50
+ (enableGlobalFilterRankedResults && globalFilter) || !enablePagination
51
+ ? getPrePaginationRowModel().rows
52
+ : getPaginationRowModel().rows,
53
+ globalFilter,
54
+ ]);
35
55
 
36
56
  const rowVirtualizer = enableRowVirtualization
37
57
  ? useVirtual({
38
- overscan: density === 'compact' ? 15 : 5,
58
+ overscan: density === 'compact' ? 20 : 10,
39
59
  size: rows.length,
40
60
  parentRef: tableContainerRef,
41
61
  ...virtualizerProps,
@@ -35,26 +35,32 @@ export const MRT_ExpandAllButton: FC<Props> = ({ instance }) => {
35
35
  enterNextDelay={1000}
36
36
  title={localization.expandAll}
37
37
  >
38
- <IconButton
39
- aria-label={localization.expandAll}
40
- disabled={!getCanSomeRowsExpand() && !renderDetailPanel}
41
- onClick={() => toggleAllRowsExpanded(!getIsAllRowsExpanded())}
42
- {...iconButtonProps}
43
- sx={{
44
- height: density === 'compact' ? '1.75rem' : '2.25rem',
45
- width: density === 'compact' ? '1.75rem' : '2.25rem',
46
- ...iconButtonProps?.sx,
47
- }}
48
- >
49
- <KeyboardDoubleArrowDownIcon
50
- style={{
51
- transform: `rotate(${
52
- getIsAllRowsExpanded() ? -180 : getIsSomeRowsExpanded() ? -90 : 0
53
- }deg)`,
54
- transition: 'transform 0.2s',
38
+ <span>
39
+ <IconButton
40
+ aria-label={localization.expandAll}
41
+ disabled={!getCanSomeRowsExpand() && !renderDetailPanel}
42
+ onClick={() => toggleAllRowsExpanded(!getIsAllRowsExpanded())}
43
+ {...iconButtonProps}
44
+ sx={{
45
+ height: density === 'compact' ? '1.75rem' : '2.25rem',
46
+ width: density === 'compact' ? '1.75rem' : '2.25rem',
47
+ ...iconButtonProps?.sx,
55
48
  }}
56
- />
57
- </IconButton>
49
+ >
50
+ <KeyboardDoubleArrowDownIcon
51
+ style={{
52
+ transform: `rotate(${
53
+ getIsAllRowsExpanded()
54
+ ? -180
55
+ : getIsSomeRowsExpanded()
56
+ ? -90
57
+ : 0
58
+ }deg)`,
59
+ transition: 'transform 0.2s',
60
+ }}
61
+ />
62
+ </IconButton>
63
+ </span>
58
64
  </Tooltip>
59
65
  );
60
66
  };
@@ -38,30 +38,32 @@ export const MRT_ExpandButton: FC<Props> = ({ row, instance }) => {
38
38
  enterNextDelay={1000}
39
39
  title={localization.expand}
40
40
  >
41
- <IconButton
42
- aria-label={localization.expand}
43
- disabled={!row.getCanExpand() && !renderDetailPanel}
44
- onClick={handleToggleExpand}
45
- {...iconButtonProps}
46
- sx={{
47
- height: density === 'compact' ? '1.75rem' : '2.25rem',
48
- width: density === 'compact' ? '1.75rem' : '2.25rem',
49
- ...iconButtonProps?.sx,
50
- }}
51
- >
52
- <ExpandMoreIcon
53
- style={{
54
- transform: `rotate(${
55
- !row.getCanExpand() && !renderDetailPanel
56
- ? -90
57
- : row.getIsExpanded()
58
- ? -180
59
- : 0
60
- }deg)`,
61
- transition: 'transform 0.2s',
41
+ <span>
42
+ <IconButton
43
+ aria-label={localization.expand}
44
+ disabled={!row.getCanExpand() && !renderDetailPanel}
45
+ onClick={handleToggleExpand}
46
+ {...iconButtonProps}
47
+ sx={{
48
+ height: density === 'compact' ? '1.75rem' : '2.25rem',
49
+ width: density === 'compact' ? '1.75rem' : '2.25rem',
50
+ ...iconButtonProps?.sx,
62
51
  }}
63
- />
64
- </IconButton>
52
+ >
53
+ <ExpandMoreIcon
54
+ style={{
55
+ transform: `rotate(${
56
+ !row.getCanExpand() && !renderDetailPanel
57
+ ? -90
58
+ : row.getIsExpanded()
59
+ ? -180
60
+ : 0
61
+ }deg)`,
62
+ transition: 'transform 0.2s',
63
+ }}
64
+ />
65
+ </IconButton>
66
+ </span>
65
67
  </Tooltip>
66
68
  );
67
69
  };
package/src/sortingFns.ts CHANGED
@@ -19,3 +19,7 @@ const fuzzy = (rowA: MRT_Row, rowB: MRT_Row, columnId: string) => {
19
19
  export const MRT_SortingFns = {
20
20
  fuzzy,
21
21
  };
22
+
23
+ export const rankGlobalFuzzy = (rowA: MRT_Row, rowB: MRT_Row) =>
24
+ Math.max(...Object.values(rowB.columnFiltersMeta).map((v: any) => v.rank)) -
25
+ Math.max(...Object.values(rowA.columnFiltersMeta).map((v: any) => v.rank));