material-react-table 0.6.9 → 0.7.0-alpha.10

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.
Files changed (108) hide show
  1. package/README.md +29 -19
  2. package/dist/MaterialReactTable.d.ts +314 -108
  3. package/dist/body/MRT_TableBody.d.ts +3 -0
  4. package/dist/body/MRT_TableBodyCell.d.ts +2 -11
  5. package/dist/body/MRT_TableBodyRow.d.ts +3 -1
  6. package/dist/body/MRT_TableDetailPanel.d.ts +2 -1
  7. package/dist/buttons/MRT_CopyButton.d.ts +2 -1
  8. package/dist/buttons/MRT_EditActionButtons.d.ts +2 -1
  9. package/dist/buttons/MRT_ExpandAllButton.d.ts +2 -0
  10. package/dist/buttons/MRT_ExpandButton.d.ts +2 -1
  11. package/dist/buttons/MRT_FullScreenToggleButton.d.ts +2 -0
  12. package/dist/buttons/MRT_ShowHideColumnsButton.d.ts +2 -0
  13. package/dist/buttons/MRT_ToggleColumnActionMenuButton.d.ts +3 -2
  14. package/dist/buttons/MRT_ToggleDensePaddingButton.d.ts +2 -0
  15. package/dist/buttons/MRT_ToggleFiltersButton.d.ts +2 -0
  16. package/dist/buttons/MRT_ToggleRowActionMenuButton.d.ts +2 -1
  17. package/dist/buttons/MRT_ToggleSearchButton.d.ts +2 -0
  18. package/dist/enums.d.ts +2 -1
  19. package/dist/filtersFNs.d.ts +13 -5
  20. package/dist/footer/MRT_TableFooter.d.ts +3 -0
  21. package/dist/footer/MRT_TableFooterCell.d.ts +3 -2
  22. package/dist/footer/MRT_TableFooterRow.d.ts +2 -1
  23. package/dist/head/MRT_TableHead.d.ts +3 -0
  24. package/dist/head/MRT_TableHeadCell.d.ts +4 -18
  25. package/dist/head/MRT_TableHeadRow.d.ts +2 -1
  26. package/dist/icons.d.ts +1 -1
  27. package/dist/inputs/MRT_EditCellTextField.d.ts +2 -1
  28. package/dist/inputs/MRT_FilterTextField.d.ts +3 -2
  29. package/dist/inputs/MRT_SearchTextField.d.ts +2 -0
  30. package/dist/inputs/MRT_SelectCheckbox.d.ts +2 -1
  31. package/dist/localization.d.ts +10 -2
  32. package/dist/material-react-table.cjs.development.js +2286 -2459
  33. package/dist/material-react-table.cjs.development.js.map +1 -1
  34. package/dist/material-react-table.cjs.production.min.js +1 -1
  35. package/dist/material-react-table.cjs.production.min.js.map +1 -1
  36. package/dist/material-react-table.esm.js +2288 -2461
  37. package/dist/material-react-table.esm.js.map +1 -1
  38. package/dist/menus/MRT_ColumnActionMenu.d.ts +3 -2
  39. package/dist/menus/MRT_FilterTypeMenu.d.ts +4 -3
  40. package/dist/menus/MRT_RowActionMenu.d.ts +3 -2
  41. package/dist/menus/MRT_ShowHideColumnsMenu.d.ts +2 -0
  42. package/dist/menus/MRT_ShowHideColumnsMenuItems.d.ts +2 -1
  43. package/dist/table/MRT_Table.d.ts +3 -0
  44. package/dist/table/MRT_TableContainer.d.ts +2 -0
  45. package/dist/table/MRT_TablePaper.d.ts +7 -0
  46. package/dist/table/MRT_TableRoot.d.ts +3 -0
  47. package/dist/toolbar/MRT_LinearProgressBar.d.ts +2 -0
  48. package/dist/toolbar/MRT_TablePagination.d.ts +2 -0
  49. package/dist/toolbar/MRT_ToolbarAlertBanner.d.ts +2 -0
  50. package/dist/toolbar/MRT_ToolbarBottom.d.ts +2 -0
  51. package/dist/toolbar/MRT_ToolbarInternalButtons.d.ts +2 -0
  52. package/dist/toolbar/MRT_ToolbarTop.d.ts +4 -2
  53. package/dist/utils.d.ts +12 -2
  54. package/package.json +29 -30
  55. package/src/MaterialReactTable.tsx +614 -297
  56. package/src/body/MRT_TableBody.tsx +26 -22
  57. package/src/body/MRT_TableBodyCell.tsx +79 -55
  58. package/src/body/MRT_TableBodyRow.tsx +37 -67
  59. package/src/body/MRT_TableDetailPanel.tsx +21 -17
  60. package/src/buttons/MRT_CopyButton.tsx +34 -10
  61. package/src/buttons/MRT_EditActionButtons.tsx +16 -13
  62. package/src/buttons/MRT_ExpandAllButton.tsx +36 -28
  63. package/src/buttons/MRT_ExpandButton.tsx +35 -41
  64. package/src/buttons/MRT_FullScreenToggleButton.tsx +18 -11
  65. package/src/buttons/MRT_ShowHideColumnsButton.tsx +18 -7
  66. package/src/buttons/MRT_ToggleColumnActionMenuButton.tsx +22 -11
  67. package/src/buttons/MRT_ToggleDensePaddingButton.tsx +18 -11
  68. package/src/buttons/MRT_ToggleFiltersButton.tsx +16 -9
  69. package/src/buttons/MRT_ToggleRowActionMenuButton.tsx +31 -26
  70. package/src/buttons/MRT_ToggleSearchButton.tsx +24 -12
  71. package/src/enums.ts +2 -1
  72. package/src/filtersFNs.ts +17 -3
  73. package/src/footer/MRT_TableFooter.tsx +24 -8
  74. package/src/footer/MRT_TableFooterCell.tsx +34 -26
  75. package/src/footer/MRT_TableFooterRow.tsx +21 -39
  76. package/src/head/MRT_TableHead.tsx +24 -8
  77. package/src/head/MRT_TableHeadCell.tsx +185 -142
  78. package/src/head/MRT_TableHeadRow.tsx +16 -93
  79. package/src/icons.ts +3 -3
  80. package/src/inputs/MRT_EditCellTextField.tsx +39 -24
  81. package/src/inputs/MRT_FilterTextField.tsx +53 -39
  82. package/src/inputs/MRT_SearchTextField.tsx +71 -25
  83. package/src/inputs/MRT_SelectCheckbox.tsx +42 -30
  84. package/src/localization.ts +20 -4
  85. package/src/menus/MRT_ColumnActionMenu.tsx +129 -69
  86. package/src/menus/MRT_FilterTypeMenu.tsx +55 -23
  87. package/src/menus/MRT_RowActionMenu.tsx +22 -15
  88. package/src/menus/MRT_ShowHideColumnsMenu.tsx +52 -20
  89. package/src/menus/MRT_ShowHideColumnsMenuItems.tsx +27 -17
  90. package/src/table/MRT_Table.tsx +25 -15
  91. package/src/table/MRT_TableContainer.tsx +106 -45
  92. package/src/table/MRT_TablePaper.tsx +65 -0
  93. package/src/table/MRT_TableRoot.tsx +239 -0
  94. package/src/toolbar/MRT_LinearProgressBar.tsx +10 -7
  95. package/src/toolbar/MRT_TablePagination.tsx +33 -19
  96. package/src/toolbar/MRT_ToolbarAlertBanner.tsx +33 -20
  97. package/src/toolbar/MRT_ToolbarBottom.tsx +32 -21
  98. package/src/toolbar/MRT_ToolbarInternalButtons.tsx +34 -20
  99. package/src/toolbar/MRT_ToolbarTop.tsx +36 -29
  100. package/src/utils.ts +37 -8
  101. package/dist/head/MRT_TableHeadCellActions.d.ts +0 -5
  102. package/dist/table/MRT_TableSpacerCell.d.ts +0 -6
  103. package/dist/useMRT.d.ts +0 -27
  104. package/src/@types/faker.d.ts +0 -4
  105. package/src/@types/react-table-config.d.ts +0 -53
  106. package/src/head/MRT_TableHeadCellActions.tsx +0 -29
  107. package/src/table/MRT_TableSpacerCell.tsx +0 -20
  108. package/src/useMRT.tsx +0 -215
@@ -1,63 +1,68 @@
1
1
  import React, { FC } from 'react';
2
2
  import { alpha, Box, Theme, Toolbar } from '@mui/material';
3
3
  import { MRT_SearchTextField } from '../inputs/MRT_SearchTextField';
4
- import { useMRT } from '../useMRT';
5
4
  import { MRT_ToolbarInternalButtons } from './MRT_ToolbarInternalButtons';
6
5
  import { MRT_TablePagination } from './MRT_TablePagination';
7
6
  import { MRT_ToolbarAlertBanner } from './MRT_ToolbarAlertBanner';
8
7
  import { MRT_LinearProgressBar } from './MRT_LinearProgressBar';
9
8
  import { MRT_TableInstance } from '..';
10
9
 
11
- export const commonToolbarStyles = (
12
- theme: Theme,
13
- tableInstance: MRT_TableInstance,
14
- ) => ({
10
+ export const commonToolbarStyles = ({ theme }: { theme: Theme }) => ({
15
11
  backgroundColor: theme.palette.background.default,
16
12
  backgroundImage: `linear-gradient(${alpha(
17
13
  theme.palette.common.white,
18
14
  0.05,
19
15
  )},${alpha(theme.palette.common.white, 0.05)})`,
20
16
  display: 'grid',
21
- opacity: tableInstance.state.fullScreen ? 0.95 : 1,
22
17
  p: '0 !important',
23
18
  width: '100%',
24
19
  zIndex: 1,
25
20
  });
26
21
 
27
- interface Props {}
22
+ interface Props {
23
+ tableInstance: MRT_TableInstance;
24
+ }
28
25
 
29
- export const MRT_ToolbarTop: FC<Props> = () => {
26
+ export const MRT_ToolbarTop: FC<Props> = ({ tableInstance }) => {
30
27
  const {
31
- disableGlobalFilter,
32
- hideToolbarInternalActions,
33
- manualPagination,
34
- muiTableToolbarTopProps,
35
- positionPagination,
36
- positionToolbarActions,
37
- positionToolbarAlertBanner,
38
- renderToolbarCustomActions,
39
- tableInstance,
40
- } = useMRT();
28
+ getState,
29
+ options: {
30
+ enableGlobalFilter,
31
+ enablePagination,
32
+ hideToolbarInternalActions,
33
+ idPrefix,
34
+ muiTableToolbarTopProps,
35
+ positionPagination,
36
+ positionToolbarActions,
37
+ positionToolbarAlertBanner,
38
+ renderToolbarCustomActions,
39
+ },
40
+ } = tableInstance;
41
+
42
+ const { isFullScreen } = getState();
41
43
 
42
44
  const toolbarProps =
43
45
  muiTableToolbarTopProps instanceof Function
44
- ? muiTableToolbarTopProps(tableInstance)
46
+ ? muiTableToolbarTopProps({ tableInstance })
45
47
  : muiTableToolbarTopProps;
46
48
 
47
49
  return (
48
50
  <Toolbar
51
+ id={`mrt-${idPrefix}-toolbar-top`}
49
52
  variant="dense"
50
53
  {...toolbarProps}
51
54
  sx={(theme) =>
52
55
  ({
53
- position: tableInstance.state.fullScreen ? 'sticky' : undefined,
54
- top: tableInstance.state.fullScreen ? '0' : undefined,
55
- ...commonToolbarStyles(theme, tableInstance),
56
+ position: isFullScreen ? 'sticky' : undefined,
57
+ top: isFullScreen ? '0' : undefined,
58
+ ...commonToolbarStyles({ theme }),
56
59
  ...toolbarProps?.sx,
57
60
  } as any)
58
61
  }
59
62
  >
60
- {positionToolbarAlertBanner === 'top' && <MRT_ToolbarAlertBanner />}
63
+ {positionToolbarAlertBanner === 'top' && (
64
+ <MRT_ToolbarAlertBanner tableInstance={tableInstance} />
65
+ )}
61
66
  <Box
62
67
  sx={{
63
68
  p: '0.5rem',
@@ -65,7 +70,7 @@ export const MRT_ToolbarTop: FC<Props> = () => {
65
70
  justifyContent: 'space-between',
66
71
  }}
67
72
  >
68
- {renderToolbarCustomActions?.(tableInstance) ?? <span />}
73
+ {renderToolbarCustomActions?.({ tableInstance }) ?? <span />}
69
74
  <Box
70
75
  sx={{
71
76
  display: 'flex',
@@ -74,19 +79,21 @@ export const MRT_ToolbarTop: FC<Props> = () => {
74
79
  zIndex: 3,
75
80
  }}
76
81
  >
77
- {!disableGlobalFilter && <MRT_SearchTextField />}
82
+ {enableGlobalFilter && (
83
+ <MRT_SearchTextField tableInstance={tableInstance} />
84
+ )}
78
85
  {!hideToolbarInternalActions && positionToolbarActions === 'top' && (
79
- <MRT_ToolbarInternalButtons />
86
+ <MRT_ToolbarInternalButtons tableInstance={tableInstance} />
80
87
  )}
81
88
  </Box>
82
89
  </Box>
83
90
  <div>
84
- {!manualPagination &&
91
+ {enablePagination &&
85
92
  ['top', 'both'].includes(positionPagination ?? '') && (
86
- <MRT_TablePagination />
93
+ <MRT_TablePagination tableInstance={tableInstance} />
87
94
  )}
88
95
  </div>
89
- <MRT_LinearProgressBar />
96
+ <MRT_LinearProgressBar tableInstance={tableInstance} />
90
97
  </Toolbar>
91
98
  );
92
99
  };
package/src/utils.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { MRT_ColumnInstance, MRT_ColumnInterface } from '.';
1
+ import { ColumnDef, Table } from '@tanstack/react-table';
2
+ import { MRT_ColumnInterface, MRT_FilterType } from '.';
3
+ import { MRT_FILTER_TYPE } from './enums';
2
4
 
3
- export const findLowestLevelCols = (
4
- columns: MRT_ColumnInterface[] | MRT_ColumnInstance[],
5
- ) => {
6
- let lowestLevelColumns: MRT_ColumnInterface[] | MRT_ColumnInstance[] =
7
- columns;
8
- let currentCols: MRT_ColumnInterface[] | MRT_ColumnInstance[] | undefined =
9
- columns;
5
+ export const getAllLeafColumnDefs = (
6
+ columns: MRT_ColumnInterface[],
7
+ ): MRT_ColumnInterface[] => {
8
+ let lowestLevelColumns: MRT_ColumnInterface[] = columns;
9
+ let currentCols: MRT_ColumnInterface[] | undefined = columns;
10
10
  while (!!currentCols?.length && currentCols.some((col) => col.columns)) {
11
11
  const nextCols: MRT_ColumnInterface[] = currentCols
12
12
  .filter((col) => !!col.columns)
@@ -19,3 +19,32 @@ export const findLowestLevelCols = (
19
19
  }
20
20
  return lowestLevelColumns.filter((col) => !col.columns);
21
21
  };
22
+
23
+ export const createGroup = <D extends Record<string, any> = {}>(
24
+ table: Table<D>,
25
+ column: MRT_ColumnInterface<D>,
26
+ currentFilterTypes: { [key: string]: MRT_FilterType },
27
+ ): ColumnDef<D> =>
28
+ table.createGroup({
29
+ ...column,
30
+ columns: column?.columns?.map?.((col) =>
31
+ col.columns
32
+ ? createGroup<D>(table, col, currentFilterTypes)
33
+ : createDataColumn(table, col, currentFilterTypes),
34
+ ),
35
+ } as any);
36
+
37
+ export const createDataColumn = <D extends Record<string, any> = {}>(
38
+ table: Table<D>,
39
+ column: MRT_ColumnInterface<D>,
40
+ currentFilterTypes: { [key: string]: MRT_FilterType },
41
+ ): ColumnDef<D> => // @ts-ignore
42
+ table.createDataColumn(column.id, {
43
+ filterType: currentFilterTypes[column.id] || MRT_FILTER_TYPE.BEST_MATCH,
44
+ ...column,
45
+ }) as any;
46
+
47
+ export const createDisplayColumn = <D extends Record<string, any> = {}>(
48
+ table: Table<D>,
49
+ column: Omit<MRT_ColumnInterface<D>, 'header'> & { header?: string },
50
+ ): ColumnDef<D> => table.createDisplayColumn(column);
@@ -1,5 +0,0 @@
1
- import { FC } from 'react';
2
- interface Props {
3
- }
4
- export declare const MRT_TableHeadCellActions: FC<Props>;
5
- export {};
@@ -1,6 +0,0 @@
1
- import { CSSProperties, FC } from 'react';
2
- interface Props {
3
- width?: CSSProperties['width'];
4
- }
5
- export declare const MRT_TableSpacerCell: FC<Props>;
6
- export {};
package/dist/useMRT.d.ts DELETED
@@ -1,27 +0,0 @@
1
- import React, { Dispatch, PropsWithChildren, SetStateAction } from 'react';
2
- import type { MRT_FilterType, MRT_Row, MRT_TableInstance } from '.';
3
- import { MRT_FILTER_TYPE } from './enums';
4
- import { MRT_Icons } from './icons';
5
- import { MRT_Localization } from './localization';
6
- import { MaterialReactTableProps } from './MaterialReactTable';
7
- export declare type UseMRT<D extends {} = {}> = MaterialReactTableProps<D> & {
8
- anyRowsCanExpand: boolean;
9
- anyRowsExpanded: boolean;
10
- icons: MRT_Icons;
11
- idPrefix: string;
12
- filterTypes: {
13
- [key in MRT_FILTER_TYPE]: any;
14
- };
15
- localization: MRT_Localization;
16
- setCurrentEditingRow: Dispatch<SetStateAction<MRT_Row<D> | null>>;
17
- setCurrentFilterTypes: Dispatch<SetStateAction<{
18
- [key: string]: MRT_FilterType;
19
- }>>;
20
- setDensePadding: Dispatch<SetStateAction<boolean>>;
21
- setFullScreen: Dispatch<SetStateAction<boolean>>;
22
- setShowFilters: Dispatch<SetStateAction<boolean>>;
23
- setShowSearch: Dispatch<SetStateAction<boolean>>;
24
- tableInstance: MRT_TableInstance<D>;
25
- };
26
- export declare const MaterialReactTableProvider: <D extends {} = {}>(props: React.PropsWithChildren<MaterialReactTableProps<D>>) => JSX.Element;
27
- export declare const useMRT: <D extends {} = {}>() => UseMRT<D>;
@@ -1,4 +0,0 @@
1
- declare module '@faker-js/faker' {
2
- import faker from 'faker';
3
- export default faker;
4
- }
@@ -1,53 +0,0 @@
1
- import {
2
- UseGroupByHooks,
3
- UseRowSelectHooks,
4
- UseSortByHooks,
5
- } from 'react-table';
6
- import {
7
- MRT_Cell,
8
- MRT_ColumnInstance,
9
- MRT_ColumnInterface,
10
- MRT_Row,
11
- MRT_TableInstance,
12
- MRT_TableOptions,
13
- MRT_TableState,
14
- } from '..';
15
-
16
- declare module 'react-table' {
17
- export interface TableOptions<D extends Record<string, unknown>>
18
- extends MRT_TableOptions<D> {}
19
-
20
- export interface Hooks<
21
- D extends Record<string, unknown> = Record<string, unknown>,
22
- > extends UseExpandedHooks<D>,
23
- UseGroupByHooks<D>,
24
- UseRowSelectHooks<D>,
25
- UseSortByHooks<D> {}
26
-
27
- export interface TableInstance<
28
- D extends Record<string, unknown> = Record<string, unknown>,
29
- > extends MRT_TableInstance<D> {
30
- rows: MRT_Row<D>[];
31
- }
32
-
33
- export interface TableState<
34
- D extends Record<string, unknown> = Record<string, unknown>,
35
- > extends MRT_TableState<D> {}
36
-
37
- export interface ColumnInterface<
38
- D extends Record<string, unknown> = Record<string, unknown>,
39
- > extends MRT_ColumnInterface<D> {}
40
-
41
- export interface ColumnInstance<
42
- D extends Record<string, unknown> = Record<string, unknown>,
43
- > extends MRT_ColumnInstance<D> {}
44
-
45
- export interface Cell<
46
- D extends Record<string, unknown> = Record<string, unknown>,
47
- V = any,
48
- > extends MRT_Cell<D> {}
49
-
50
- export interface Row<
51
- D extends Record<string, unknown> = Record<string, unknown>,
52
- > extends MRT_Row<D> {}
53
- }
@@ -1,29 +0,0 @@
1
- import React, { FC } from 'react';
2
- import { TableCell } from '@mui/material';
3
- import { useMRT } from '../useMRT';
4
- import { commonTableHeadCellStyles } from './MRT_TableHeadCell';
5
-
6
- interface Props {}
7
-
8
- export const MRT_TableHeadCellActions: FC<Props> = () => {
9
- const {
10
- localization,
11
- tableInstance: {
12
- state: { densePadding },
13
- },
14
- } = useMRT();
15
-
16
- return (
17
- <TableCell
18
- style={{ textAlign: 'center' }}
19
- sx={{
20
- ...commonTableHeadCellStyles(densePadding),
21
- textAlign: 'center',
22
- maxWidth: '4rem',
23
- width: '4rem',
24
- }}
25
- >
26
- {localization.actions}
27
- </TableCell>
28
- );
29
- };
@@ -1,20 +0,0 @@
1
- import React, { CSSProperties, FC } from 'react';
2
- import { TableCell } from '@mui/material';
3
- import { useMRT } from '../useMRT';
4
-
5
- interface Props {
6
- width?: CSSProperties['width'];
7
- }
8
-
9
- export const MRT_TableSpacerCell: FC<Props> = ({ width }) => {
10
- const { muiTableBodyCellProps } = useMRT();
11
-
12
- const tableCellProps =
13
- muiTableBodyCellProps instanceof Function
14
- ? muiTableBodyCellProps()
15
- : muiTableBodyCellProps;
16
-
17
- return (
18
- <TableCell {...tableCellProps} sx={{ width, ...tableCellProps?.sx }} />
19
- );
20
- };
package/src/useMRT.tsx DELETED
@@ -1,215 +0,0 @@
1
- import React, {
2
- Context,
3
- Dispatch,
4
- PropsWithChildren,
5
- SetStateAction,
6
- createContext,
7
- useCallback,
8
- useContext,
9
- useMemo,
10
- useState,
11
- } from 'react';
12
- import {
13
- PluginHook,
14
- useExpanded,
15
- useFilters,
16
- useFlexLayout,
17
- useGlobalFilter,
18
- useGroupBy,
19
- usePagination,
20
- useResizeColumns,
21
- useRowSelect,
22
- useSortBy,
23
- useTable,
24
- } from 'react-table';
25
- import type {
26
- MRT_ColumnInterface,
27
- MRT_FilterType,
28
- MRT_Row,
29
- MRT_TableInstance,
30
- } from '.';
31
- import { MRT_FILTER_TYPE } from './enums';
32
- import { MRT_Icons } from './icons';
33
- import { MRT_Localization } from './localization';
34
- import { MaterialReactTableProps } from './MaterialReactTable';
35
- import { findLowestLevelCols } from './utils';
36
-
37
- export type UseMRT<D extends {} = {}> = MaterialReactTableProps<D> & {
38
- anyRowsCanExpand: boolean;
39
- anyRowsExpanded: boolean;
40
- icons: MRT_Icons;
41
- idPrefix: string;
42
- filterTypes: { [key in MRT_FILTER_TYPE]: any };
43
- localization: MRT_Localization;
44
- setCurrentEditingRow: Dispatch<SetStateAction<MRT_Row<D> | null>>;
45
- setCurrentFilterTypes: Dispatch<
46
- SetStateAction<{
47
- [key: string]: MRT_FilterType;
48
- }>
49
- >;
50
- setDensePadding: Dispatch<SetStateAction<boolean>>;
51
- setFullScreen: Dispatch<SetStateAction<boolean>>;
52
- setShowFilters: Dispatch<SetStateAction<boolean>>;
53
- setShowSearch: Dispatch<SetStateAction<boolean>>;
54
- tableInstance: MRT_TableInstance<D>;
55
- };
56
-
57
- const MaterialReactTableContext = (<D extends {} = {}>() =>
58
- createContext<UseMRT<D>>({} as UseMRT<D>) as Context<UseMRT<D>>)();
59
-
60
- export const MaterialReactTableProvider = <D extends {} = {}>(
61
- props: PropsWithChildren<MaterialReactTableProps<D>>,
62
- ) => {
63
- const hooks: PluginHook<D>[] = [
64
- useFilters,
65
- useGlobalFilter,
66
- useGroupBy,
67
- useSortBy,
68
- useExpanded,
69
- usePagination,
70
- useRowSelect,
71
- ];
72
-
73
- if (props.enableColumnResizing)
74
- hooks.unshift(useResizeColumns, useFlexLayout);
75
-
76
- const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row<D> | null>(
77
- null,
78
- );
79
- const [densePadding, setDensePadding] = useState(
80
- props.initialState?.densePadding ?? false,
81
- );
82
- const [fullScreen, setFullScreen] = useState(
83
- props.initialState?.fullScreen ?? false,
84
- );
85
- const [showFilters, setShowFilters] = useState(
86
- props.initialState?.showFilters ?? false,
87
- );
88
- const [showSearch, setShowSearch] = useState(
89
- props.initialState?.showSearch ?? false,
90
- );
91
-
92
- const [currentFilterTypes, setCurrentFilterTypes] = useState<{
93
- [key: string]: MRT_FilterType;
94
- }>(() =>
95
- Object.assign(
96
- {},
97
- ...findLowestLevelCols(props.columns).map((c) => ({
98
- [c.accessor as string]:
99
- c.filter ??
100
- props?.initialState?.filters?.[c.accessor as any] ??
101
- (!!c.filterSelectOptions ? 'equals' : 'fuzzy'),
102
- })),
103
- ),
104
- );
105
-
106
- const applyFiltersToColumns = useCallback(
107
- (cols: MRT_ColumnInterface[]) =>
108
- cols.map((column) => {
109
- if (column.columns) {
110
- applyFiltersToColumns(column.columns);
111
- } else {
112
- column.filter =
113
- props?.filterTypes?.[
114
- currentFilterTypes[column.accessor as string] as MRT_FILTER_TYPE
115
- ];
116
- }
117
- return column;
118
- }),
119
- [currentFilterTypes, props.filterTypes],
120
- );
121
-
122
- const columns = useMemo(
123
- () => applyFiltersToColumns(props.columns),
124
- [props.columns, applyFiltersToColumns],
125
- );
126
-
127
- const data = useMemo(
128
- () =>
129
- !props.isLoading || !!props.data.length
130
- ? props.data
131
- : [...Array(10).fill(null)].map((_) =>
132
- Object.assign(
133
- {},
134
- ...findLowestLevelCols(props.columns).map((c) => ({
135
- [c.accessor as string]: null,
136
- })),
137
- ),
138
- ),
139
- [props.data, props.isLoading],
140
- );
141
-
142
- const tableInstance = useTable(
143
- // @ts-ignore
144
- {
145
- ...props,
146
- // @ts-ignore
147
- columns,
148
- data,
149
- useControlledState: (state) =>
150
- useMemo(
151
- () => ({
152
- ...state,
153
- currentEditingRow,
154
- currentFilterTypes,
155
- densePadding,
156
- fullScreen,
157
- showFilters,
158
- showSearch,
159
- //@ts-ignore
160
- ...props?.useControlledState?.(state),
161
- }),
162
- [
163
- currentEditingRow,
164
- currentFilterTypes,
165
- densePadding,
166
- fullScreen,
167
- showFilters,
168
- showSearch,
169
- state,
170
- ],
171
- ),
172
- },
173
- ...hooks,
174
- ) as unknown as MRT_TableInstance<D>;
175
-
176
- const idPrefix = useMemo(
177
- () => props.idPrefix ?? Math.random().toString(36).substring(2, 9),
178
- [props.idPrefix],
179
- );
180
- const anyRowsCanExpand = useMemo(
181
- () => tableInstance.rows.some((row) => row.canExpand),
182
- [tableInstance.rows],
183
- );
184
- const anyRowsExpanded = useMemo(
185
- () => tableInstance.rows.some((row) => row.isExpanded),
186
- [tableInstance.rows],
187
- );
188
-
189
- return (
190
- <MaterialReactTableContext.Provider
191
- value={{
192
- ...props,
193
- anyRowsCanExpand,
194
- anyRowsExpanded,
195
- idPrefix,
196
- //@ts-ignore
197
- setCurrentEditingRow,
198
- setCurrentFilterTypes,
199
- setDensePadding,
200
- setFullScreen,
201
- setShowFilters,
202
- setShowSearch,
203
- //@ts-ignore
204
- tableInstance,
205
- }}
206
- >
207
- {props.children}
208
- </MaterialReactTableContext.Provider>
209
- );
210
- };
211
-
212
- export const useMRT = <D extends {} = {}>(): UseMRT<D> =>
213
- useContext<UseMRT<D>>(
214
- MaterialReactTableContext as unknown as Context<UseMRT<D>>,
215
- );