material-react-table 0.6.2 → 0.6.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.
@@ -0,0 +1,5 @@
1
+ import { FC } from 'react';
2
+ interface Props {
3
+ }
4
+ export declare const MRT_LinearProgressBar: FC<Props>;
5
+ export {};
@@ -1,4 +1,15 @@
1
1
  import { FC } from 'react';
2
+ import { Theme } from '@mui/material';
3
+ import { MRT_TableInstance } from '..';
4
+ export declare const commonToolbarStyles: (theme: Theme, tableInstance: MRT_TableInstance) => {
5
+ backgroundColor: string;
6
+ backgroundImage: string;
7
+ display: string;
8
+ opacity: number;
9
+ p: string;
10
+ width: string;
11
+ zIndex: number;
12
+ };
2
13
  interface Props {
3
14
  }
4
15
  export declare const MRT_ToolbarTop: FC<Props>;
package/dist/useMRT.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import React, { PropsWithChildren, Dispatch, SetStateAction } from 'react';
1
+ import React, { Dispatch, PropsWithChildren, SetStateAction } from 'react';
2
2
  import type { MRT_FilterType, MRT_Row, MRT_TableInstance } from '.';
3
3
  import { MRT_FILTER_TYPE } from './enums';
4
4
  import { MRT_Icons } from './icons';
@@ -0,0 +1,2 @@
1
+ import { MRT_ColumnInterface } from '.';
2
+ export declare const findLowestLevelCols: (columns: MRT_ColumnInterface[]) => MRT_ColumnInterface<{}>[];
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.6.2",
2
+ "version": "0.6.3",
3
3
  "license": "MIT",
4
4
  "name": "material-react-table",
5
5
  "description": "A fully featured Material-UI implementation of react-table, inspired by material-table and the mui DataGrid, written from the ground up in TypeScript.",
@@ -90,7 +90,7 @@ export type MRT_TableOptions<D extends {} = {}> = TableOptions<D> &
90
90
  UseRowSelectOptions<D> &
91
91
  UseRowStateOptions<D> &
92
92
  UseSortByOptions<D> & {
93
- columns: (Column<D> & MRT_ColumnInterface)[];
93
+ columns: MRT_ColumnInterface[];
94
94
  data: D[];
95
95
  initialState?: Partial<MRT_TableState>;
96
96
  };
@@ -107,13 +107,13 @@ export type MRT_TableInstance<D extends {} = {}> = TableInstance<D> &
107
107
  UseRowStateInstanceProps<D> &
108
108
  UseSortByInstanceProps<D> & {
109
109
  columns: (Column<D> & MRT_ColumnInstance<D>)[];
110
- headerGroups: MRT_HeaderGroup<D>[];
111
110
  footerGroups: MRT_HeaderGroup<D>[];
112
- state: MRT_TableState<D>;
113
- rows: MRT_Row<D>[];
111
+ getToggleAllRowsExpandedProps: () => void;
112
+ headerGroups: MRT_HeaderGroup<D>[];
114
113
  page: MRT_Row<D>[];
115
114
  resetResizing: () => void;
116
- getToggleAllRowsExpandedProps: () => void;
115
+ rows: MRT_Row<D>[];
116
+ state: MRT_TableState<D>;
117
117
  };
118
118
 
119
119
  export type MRT_ColumnInterface<D extends {} = {}> = ColumnInterface<D> &
@@ -132,13 +132,13 @@ export type MRT_ColumnInterface<D extends {} = {}> = ColumnInterface<D> &
132
132
  Filter?: ({ column }: { column: MRT_HeaderGroup<D> }) => ReactNode;
133
133
  Footer?: string;
134
134
  Header?: string;
135
- accessor: string;
136
- columns?: (Column<D> & MRT_ColumnInterface<D>)[];
135
+ accessor?: string;
136
+ columns?: MRT_ColumnInterface<D>[];
137
137
  disableFilters?: boolean;
138
138
  editable?: boolean;
139
139
  filter?: MRT_FilterType | string | FilterType<D>;
140
140
  filterSelectOptions?: (string | { text: string; value: string })[];
141
- filterTypes: MRT_FILTER_TYPE[];
141
+ filterTypes?: (MRT_FILTER_TYPE | string)[];
142
142
  muiTableBodyCellEditTextFieldProps?:
143
143
  | TextFieldProps
144
144
  | ((cell: MRT_Cell<D>) => TextFieldProps);
@@ -380,9 +380,11 @@ export default <D extends {}>({
380
380
  ...rest
381
381
  }: MaterialReactTableProps<D>) => (
382
382
  <MaterialReactTableProvider
383
+ //@ts-ignore
383
384
  defaultColumn={defaultColumn}
384
- // @ts-ignore
385
+ //@ts-ignore
385
386
  filterTypes={{ ...defaultFilterFNs, ...filterTypes }}
387
+ //@ts-ignore
386
388
  globalFilter={globalFilter}
387
389
  icons={{ ...MRT_Default_Icons, ...icons }}
388
390
  localization={{ ...MRT_DefaultLocalization_EN, ...localization }}
@@ -28,7 +28,9 @@ export const MRT_TableBody: FC<Props> = () => {
28
28
  return (
29
29
  <TableBody
30
30
  {...tableBodyProps}
31
- sx={{ overflowY: 'hidden', ...tableBodyProps?.sx }}
31
+ sx={{
32
+ ...tableBodyProps?.sx,
33
+ }}
32
34
  >
33
35
  {rows.map((row: MRT_Row) => {
34
36
  tableInstance.prepareRow(row);
@@ -102,15 +102,16 @@ export const MRT_ColumnActionMenu: FC<Props> = ({
102
102
  setAnchorEl(null);
103
103
  };
104
104
 
105
+ const handleShowAllColumns = () => {
106
+ tableInstance.toggleHideAllColumns(false);
107
+ setAnchorEl(null);
108
+ };
109
+
105
110
  const handleOpenFilterModeMenu = (event: React.MouseEvent<HTMLElement>) => {
106
111
  event.stopPropagation();
107
112
  setFilterMenuAnchorEl(event.currentTarget);
108
113
  };
109
114
 
110
- const handleShowAllColumns = () => {
111
- tableInstance.toggleHideAllColumns(false);
112
- };
113
-
114
115
  const handleOpenShowHideColumnsMenu = (
115
116
  event: React.MouseEvent<HTMLElement>,
116
117
  ) => {
@@ -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: 'hidden',
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)].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
+ };