material-react-table 0.6.1 → 0.6.4

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.
@@ -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,
@@ -22,18 +22,24 @@ import {
22
22
  useSortBy,
23
23
  useTable,
24
24
  } from 'react-table';
25
- import type { MRT_FilterType, MRT_Row, MRT_TableInstance } from '.';
25
+ import type {
26
+ MRT_ColumnInterface,
27
+ MRT_FilterType,
28
+ MRT_Row,
29
+ MRT_TableInstance,
30
+ } from '.';
26
31
  import { MRT_FILTER_TYPE } from './enums';
27
- import { defaultFilterFNs } from './filtersFNs';
28
32
  import { MRT_Icons } from './icons';
29
33
  import { MRT_Localization } from './localization';
30
34
  import { MaterialReactTableProps } from './MaterialReactTable';
35
+ import { findLowestLevelCols } from './utils';
31
36
 
32
37
  export type UseMRT<D extends {} = {}> = MaterialReactTableProps<D> & {
33
38
  anyRowsCanExpand: boolean;
34
39
  anyRowsExpanded: boolean;
35
40
  icons: MRT_Icons;
36
41
  idPrefix: string;
42
+ filterTypes: { [key in MRT_FILTER_TYPE]: any };
37
43
  localization: MRT_Localization;
38
44
  setCurrentEditingRow: Dispatch<SetStateAction<MRT_Row<D> | null>>;
39
45
  setCurrentFilterTypes: Dispatch<
@@ -83,38 +89,12 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
83
89
  props.initialState?.showSearch ?? false,
84
90
  );
85
91
 
86
- const filterTypes = useMemo<{
87
- [key in MRT_FILTER_TYPE]: any;
88
- }>(
89
- () => ({
90
- ...defaultFilterFNs,
91
- ...props.filterTypes,
92
- }),
93
- [props.filterTypes],
94
- );
95
-
96
- const findLowestLevelCols = useCallback(() => {
97
- let lowestLevelColumns: any[] = props.columns;
98
- let currentCols: any[] = props.columns;
99
- while (!!currentCols.length && currentCols.some((col) => col.columns)) {
100
- const nextCols = currentCols
101
- .filter((col) => !!col.columns)
102
- .map((col) => col.columns)
103
- .flat();
104
- if (nextCols.every((col) => !col.columns)) {
105
- lowestLevelColumns = [...lowestLevelColumns, ...nextCols];
106
- }
107
- currentCols = nextCols;
108
- }
109
- return lowestLevelColumns.filter((col) => !col.columns);
110
- }, [props.columns]);
111
-
112
92
  const [currentFilterTypes, setCurrentFilterTypes] = useState<{
113
93
  [key: string]: MRT_FilterType;
114
94
  }>(() =>
115
95
  Object.assign(
116
96
  {},
117
- ...findLowestLevelCols().map((c) => ({
97
+ ...findLowestLevelCols(props.columns).map((c) => ({
118
98
  [c.accessor as string]:
119
99
  c.filter ??
120
100
  props?.initialState?.filters?.[c.accessor as any] ??
@@ -123,25 +103,49 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
123
103
  ),
124
104
  );
125
105
 
126
- const columns = useMemo(
127
- () =>
128
- findLowestLevelCols().map((column) => {
129
- column.filter =
130
- filterTypes[
131
- currentFilterTypes[column.accessor as string] as MRT_FILTER_TYPE
132
- ];
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
+ }
133
117
  return column;
134
118
  }),
135
- [props.columns, filterTypes, currentFilterTypes],
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],
136
140
  );
137
141
 
138
142
  const tableInstance = useTable(
143
+ // @ts-ignore
139
144
  {
140
145
  ...props,
141
- columns,
142
146
  // @ts-ignore
143
- filterTypes,
144
- globalFilter: props.globalFilter ?? 'globalFuzzy',
147
+ columns,
148
+ data,
145
149
  useControlledState: (state) =>
146
150
  useMemo(
147
151
  () => ({
@@ -167,7 +171,7 @@ export const MaterialReactTableProvider = <D extends {} = {}>(
167
171
  ),
168
172
  },
169
173
  ...hooks,
170
- ) as MRT_TableInstance<D>;
174
+ ) as unknown as MRT_TableInstance<D>;
171
175
 
172
176
  const idPrefix = useMemo(
173
177
  () => props.idPrefix ?? Math.random().toString(36).substring(2, 9),
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
+ };