material-react-table 0.6.2 → 0.6.5

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 (33) hide show
  1. package/dist/MaterialReactTable.d.ts +11 -11
  2. package/dist/filtersFNs.d.ts +10 -10
  3. package/dist/material-react-table.cjs.development.js +179 -143
  4. package/dist/material-react-table.cjs.development.js.map +1 -1
  5. package/dist/material-react-table.cjs.production.min.js +1 -1
  6. package/dist/material-react-table.cjs.production.min.js.map +1 -1
  7. package/dist/material-react-table.esm.js +181 -145
  8. package/dist/material-react-table.esm.js.map +1 -1
  9. package/dist/menus/MRT_ColumnActionMenu.d.ts +0 -1
  10. package/dist/toolbar/MRT_LinearProgressBar.d.ts +5 -0
  11. package/dist/toolbar/MRT_ToolbarTop.d.ts +11 -0
  12. package/dist/useMRT.d.ts +1 -1
  13. package/dist/utils.d.ts +2 -0
  14. package/package.json +1 -1
  15. package/src/MaterialReactTable.tsx +14 -12
  16. package/src/body/MRT_TableBody.tsx +3 -1
  17. package/src/body/MRT_TableBodyCell.tsx +2 -1
  18. package/src/body/MRT_TableBodyRow.tsx +2 -9
  19. package/src/buttons/MRT_ExpandButton.tsx +9 -1
  20. package/src/filtersFNs.ts +30 -30
  21. package/src/inputs/MRT_EditCellTextField.tsx +1 -1
  22. package/src/inputs/MRT_FilterTextField.tsx +24 -12
  23. package/src/menus/MRT_ColumnActionMenu.tsx +30 -15
  24. package/src/menus/MRT_FilterTypeMenu.tsx +20 -20
  25. package/src/menus/MRT_RowActionMenu.tsx +11 -4
  26. package/src/menus/MRT_ShowHideColumnsMenuItems.tsx +3 -0
  27. package/src/table/MRT_TableContainer.tsx +4 -28
  28. package/src/toolbar/MRT_LinearProgressBar.tsx +25 -0
  29. package/src/toolbar/MRT_TablePagination.tsx +1 -1
  30. package/src/toolbar/MRT_ToolbarBottom.tsx +18 -21
  31. package/src/toolbar/MRT_ToolbarTop.tsx +21 -10
  32. package/src/useMRT.tsx +21 -20
  33. package/src/utils.ts +17 -0
@@ -3,6 +3,7 @@ import { FormControlLabel, MenuItem, Switch } from '@mui/material';
3
3
  import { ColumnInstance } from 'react-table';
4
4
  import type { MRT_ColumnInstance } from '..';
5
5
  import { commonMenuItemStyles } from './MRT_ColumnActionMenu';
6
+ import { useMRT } from '../useMRT';
6
7
 
7
8
  interface Props {
8
9
  column: MRT_ColumnInstance;
@@ -13,6 +14,7 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
13
14
  column,
14
15
  isSubMenu,
15
16
  }) => {
17
+ const { onColumnHide, tableInstance } = useMRT();
16
18
  const isParentHeader = !!column?.columns?.length;
17
19
 
18
20
  const allChildColumnsVisible =
@@ -29,6 +31,7 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
29
31
  } else {
30
32
  column.toggleHidden();
31
33
  }
34
+ onColumnHide?.(column, tableInstance.state.hiddenColumns);
32
35
  };
33
36
 
34
37
  return (
@@ -1,11 +1,5 @@
1
- import React, { FC, useEffect, useRef } from 'react';
2
- import {
3
- LinearProgress,
4
- Paper,
5
- TableContainer,
6
- Collapse,
7
- Box,
8
- } from '@mui/material';
1
+ import React, { FC, useEffect } from 'react';
2
+ import { Paper, TableContainer, Box } from '@mui/material';
9
3
  import { useMRT } from '../useMRT';
10
4
  import { MRT_Table } from './MRT_Table';
11
5
  import { MRT_ToolbarTop } from '../toolbar/MRT_ToolbarTop';
@@ -17,28 +11,17 @@ export const MRT_TableContainer: FC<Props> = () => {
17
11
  const {
18
12
  hideToolbarBottom,
19
13
  hideToolbarTop,
20
- isFetching,
21
- isLoading,
22
- muiLinearProgressProps,
23
14
  muiTableContainerProps,
24
15
  tableInstance,
25
16
  } = useMRT();
26
17
  const fullScreen = tableInstance.state.fullScreen;
27
- const originalBodyOverflowStyle = useRef<string | undefined>();
28
-
29
- useEffect(() => {
30
- if (typeof window !== 'undefined') {
31
- originalBodyOverflowStyle.current = document?.body?.style?.overflow;
32
- }
33
- }, []);
34
18
 
35
19
  useEffect(() => {
36
20
  if (typeof window !== 'undefined') {
37
21
  if (fullScreen) {
38
22
  document.body.style.overflow = 'hidden';
39
23
  } else {
40
- document.body.style.overflow =
41
- originalBodyOverflowStyle.current ?? 'auto';
24
+ document.body.style.overflow = 'auto';
42
25
  }
43
26
  }
44
27
  }, [fullScreen]);
@@ -48,11 +31,6 @@ export const MRT_TableContainer: FC<Props> = () => {
48
31
  ? muiTableContainerProps(tableInstance)
49
32
  : muiTableContainerProps;
50
33
 
51
- const linearProgressProps =
52
- muiLinearProgressProps instanceof Function
53
- ? muiLinearProgressProps(tableInstance)
54
- : muiLinearProgressProps;
55
-
56
34
  return (
57
35
  <TableContainer
58
36
  component={Paper}
@@ -62,6 +40,7 @@ export const MRT_TableContainer: FC<Props> = () => {
62
40
  height: fullScreen ? '100%' : undefined,
63
41
  left: fullScreen ? '0' : undefined,
64
42
  m: fullScreen ? '0' : undefined,
43
+ overflowY: !fullScreen ? 'hidden' : undefined,
65
44
  position: fullScreen ? 'fixed' : undefined,
66
45
  right: fullScreen ? '0' : undefined,
67
46
  top: fullScreen ? '0' : undefined,
@@ -72,9 +51,6 @@ export const MRT_TableContainer: FC<Props> = () => {
72
51
  }}
73
52
  >
74
53
  {!hideToolbarTop && <MRT_ToolbarTop />}
75
- <Collapse in={isFetching || isLoading} unmountOnExit>
76
- <LinearProgress {...linearProgressProps} />
77
- </Collapse>
78
54
  <Box
79
55
  sx={{
80
56
  maxWidth: '100%',
@@ -0,0 +1,25 @@
1
+ import React, { FC } from 'react';
2
+ import { Collapse, LinearProgress } from '@mui/material';
3
+ import { useMRT } from '../useMRT';
4
+
5
+ interface Props {}
6
+
7
+ export const MRT_LinearProgressBar: FC<Props> = () => {
8
+ const { muiLinearProgressProps, isFetching, isLoading, tableInstance } =
9
+ useMRT();
10
+
11
+ const linearProgressProps =
12
+ muiLinearProgressProps instanceof Function
13
+ ? muiLinearProgressProps(tableInstance)
14
+ : muiLinearProgressProps;
15
+
16
+ return (
17
+ <Collapse in={isFetching || isLoading} unmountOnExit>
18
+ <LinearProgress
19
+ aria-label="Loading"
20
+ aria-busy="true"
21
+ {...linearProgressProps}
22
+ />
23
+ </Collapse>
24
+ );
25
+ };
@@ -34,7 +34,7 @@ export const MRT_TablePagination: FC<Props> = () => {
34
34
  }
35
35
  {...tablePaginationProps}
36
36
  sx={{
37
- p: 0,
37
+ m: '0 0.5rem',
38
38
  position: 'relative',
39
39
  zIndex: 2,
40
40
  ...tablePaginationProps?.sx,
@@ -1,9 +1,11 @@
1
1
  import React, { FC } from 'react';
2
- import { alpha, Toolbar } from '@mui/material';
2
+ import { Box, Toolbar } from '@mui/material';
3
3
  import { useMRT } from '../useMRT';
4
4
  import { MRT_TablePagination } from './MRT_TablePagination';
5
5
  import { MRT_ToolbarInternalButtons } from './MRT_ToolbarInternalButtons';
6
6
  import { MRT_ToolbarAlertBanner } from './MRT_ToolbarAlertBanner';
7
+ import { MRT_LinearProgressBar } from './MRT_LinearProgressBar';
8
+ import { commonToolbarStyles } from './MRT_ToolbarTop';
7
9
 
8
10
  interface Props {}
9
11
 
@@ -29,33 +31,28 @@ export const MRT_ToolbarBottom: FC<Props> = () => {
29
31
  {...toolbarProps}
30
32
  sx={(theme) =>
31
33
  ({
32
- backgroundColor: theme.palette.background.default,
33
- backgroundImage: `linear-gradient(${alpha(
34
- theme.palette.common.white,
35
- 0.05,
36
- )},${alpha(theme.palette.common.white, 0.05)})`,
37
34
  bottom: tableInstance.state.fullScreen ? '0' : undefined,
38
- display: 'flex',
39
- justifyContent: 'space-between',
40
- overflowY: 'hidden',
41
- p: '0 0.5rem !important',
42
35
  position: tableInstance.state.fullScreen ? 'fixed' : undefined,
43
- width: 'calc(100% - 1rem)',
44
- zIndex: 1,
36
+ ...commonToolbarStyles(theme, tableInstance),
45
37
  ...toolbarProps?.sx,
46
38
  } as any)
47
39
  }
48
40
  >
49
- {!hideToolbarInternalActions && positionToolbarActions === 'bottom' ? (
50
- <MRT_ToolbarInternalButtons />
51
- ) : (
52
- <span />
53
- )}
54
- {positionToolbarAlertBanner === 'bottom' && <MRT_ToolbarAlertBanner />}
55
- {!manualPagination &&
56
- ['bottom', 'both'].includes(positionPagination ?? '') && (
57
- <MRT_TablePagination />
41
+ <MRT_LinearProgressBar />
42
+ <Box
43
+ sx={{ display: 'flex', justifyContent: 'space-between', width: '100%' }}
44
+ >
45
+ {!hideToolbarInternalActions && positionToolbarActions === 'bottom' ? (
46
+ <MRT_ToolbarInternalButtons />
47
+ ) : (
48
+ <span />
58
49
  )}
50
+ {positionToolbarAlertBanner === 'bottom' && <MRT_ToolbarAlertBanner />}
51
+ {!manualPagination &&
52
+ ['bottom', 'both'].includes(positionPagination ?? '') && (
53
+ <MRT_TablePagination />
54
+ )}
55
+ </Box>
59
56
  </Toolbar>
60
57
  );
61
58
  };
@@ -1,10 +1,28 @@
1
1
  import React, { FC } from 'react';
2
- import { alpha, Box, Toolbar } from '@mui/material';
2
+ import { alpha, Box, Theme, Toolbar } from '@mui/material';
3
3
  import { MRT_SearchTextField } from '../inputs/MRT_SearchTextField';
4
4
  import { useMRT } from '../useMRT';
5
5
  import { MRT_ToolbarInternalButtons } from './MRT_ToolbarInternalButtons';
6
6
  import { MRT_TablePagination } from './MRT_TablePagination';
7
7
  import { MRT_ToolbarAlertBanner } from './MRT_ToolbarAlertBanner';
8
+ import { MRT_LinearProgressBar } from './MRT_LinearProgressBar';
9
+ import { MRT_TableInstance } from '..';
10
+
11
+ export const commonToolbarStyles = (
12
+ theme: Theme,
13
+ tableInstance: MRT_TableInstance,
14
+ ) => ({
15
+ backgroundColor: theme.palette.background.default,
16
+ backgroundImage: `linear-gradient(${alpha(
17
+ theme.palette.common.white,
18
+ 0.05,
19
+ )},${alpha(theme.palette.common.white, 0.05)})`,
20
+ display: 'grid',
21
+ opacity: tableInstance.state.fullScreen ? 0.95 : 1,
22
+ p: '0 !important',
23
+ width: '100%',
24
+ zIndex: 1,
25
+ });
8
26
 
9
27
  interface Props {}
10
28
 
@@ -32,17 +50,9 @@ export const MRT_ToolbarTop: FC<Props> = () => {
32
50
  {...toolbarProps}
33
51
  sx={(theme) =>
34
52
  ({
35
- backgroundColor: theme.palette.background.default,
36
- backgroundImage: `linear-gradient(${alpha(
37
- theme.palette.common.white,
38
- 0.05,
39
- )},${alpha(theme.palette.common.white, 0.05)})`,
40
- display: 'grid',
41
- p: '0 !important',
42
53
  position: tableInstance.state.fullScreen ? 'sticky' : undefined,
43
54
  top: tableInstance.state.fullScreen ? '0' : undefined,
44
- width: '100%',
45
- zIndex: 1,
55
+ ...commonToolbarStyles(theme, tableInstance),
46
56
  ...toolbarProps?.sx,
47
57
  } as any)
48
58
  }
@@ -76,6 +86,7 @@ export const MRT_ToolbarTop: FC<Props> = () => {
76
86
  <MRT_TablePagination />
77
87
  )}
78
88
  </div>
89
+ <MRT_LinearProgressBar />
79
90
  </Toolbar>
80
91
  );
81
92
  };
package/src/useMRT.tsx CHANGED
@@ -1,13 +1,13 @@
1
1
  import React, {
2
2
  Context,
3
+ Dispatch,
3
4
  PropsWithChildren,
5
+ SetStateAction,
4
6
  createContext,
7
+ useCallback,
5
8
  useContext,
6
9
  useMemo,
7
10
  useState,
8
- Dispatch,
9
- SetStateAction,
10
- useCallback,
11
11
  } from 'react';
12
12
  import {
13
13
  PluginHook,
@@ -32,6 +32,7 @@ import { MRT_FILTER_TYPE } from './enums';
32
32
  import { MRT_Icons } from './icons';
33
33
  import { MRT_Localization } from './localization';
34
34
  import { MaterialReactTableProps } from './MaterialReactTable';
35
+ import { findLowestLevelCols } from './utils';
35
36
 
36
37
  export type UseMRT<D extends {} = {}> = MaterialReactTableProps<D> & {
37
38
  anyRowsCanExpand: boolean;
@@ -88,28 +89,12 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
88
89
  props.initialState?.showSearch ?? false,
89
90
  );
90
91
 
91
- const findLowestLevelCols = useCallback(() => {
92
- let lowestLevelColumns: any[] = props.columns;
93
- let currentCols: any[] = props.columns;
94
- while (!!currentCols.length && currentCols.some((col) => col.columns)) {
95
- const nextCols = currentCols
96
- .filter((col) => !!col.columns)
97
- .map((col) => col.columns)
98
- .flat();
99
- if (nextCols.every((col) => !col.columns)) {
100
- lowestLevelColumns = [...lowestLevelColumns, ...nextCols];
101
- }
102
- currentCols = nextCols;
103
- }
104
- return lowestLevelColumns.filter((col) => !col.columns);
105
- }, [props.columns]);
106
-
107
92
  const [currentFilterTypes, setCurrentFilterTypes] = useState<{
108
93
  [key: string]: MRT_FilterType;
109
94
  }>(() =>
110
95
  Object.assign(
111
96
  {},
112
- ...findLowestLevelCols().map((c) => ({
97
+ ...findLowestLevelCols(props.columns).map((c) => ({
113
98
  [c.accessor as string]:
114
99
  c.filter ??
115
100
  props?.initialState?.filters?.[c.accessor as any] ??
@@ -139,12 +124,28 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
139
124
  [props.columns, applyFiltersToColumns],
140
125
  );
141
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
142
  const tableInstance = useTable(
143
143
  // @ts-ignore
144
144
  {
145
145
  ...props,
146
146
  // @ts-ignore
147
147
  columns,
148
+ data,
148
149
  useControlledState: (state) =>
149
150
  useMemo(
150
151
  () => ({
package/src/utils.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { MRT_ColumnInterface } from '.';
2
+
3
+ export const findLowestLevelCols = (columns: MRT_ColumnInterface[]) => {
4
+ let lowestLevelColumns: MRT_ColumnInterface[] = columns;
5
+ let currentCols: MRT_ColumnInterface[] | undefined = columns;
6
+ while (!!currentCols?.length && currentCols.some((col) => col.columns)) {
7
+ const nextCols: MRT_ColumnInterface[] = currentCols
8
+ .filter((col) => !!col.columns)
9
+ .map((col) => col.columns)
10
+ .flat() as MRT_ColumnInterface[];
11
+ if (nextCols.every((col) => !col?.columns)) {
12
+ lowestLevelColumns = [...lowestLevelColumns, ...nextCols];
13
+ }
14
+ currentCols = nextCols;
15
+ }
16
+ return lowestLevelColumns.filter((col) => !col.columns);
17
+ };