material-react-table 0.7.3 → 0.7.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.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.7.3",
2
+ "version": "0.7.4",
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 X DataGrid, written from the ground up in TypeScript.",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "scripts": {
30
30
  "analyze": "size-limit --why",
31
- "build": "tsdx build && size-limit && rm -rf material-react-table-docs/node_modules/material-react-table/dist && cp -r dist material-react-table-docs/node_modules/material-react-table/ && cp -r src material-react-table-docs/node_modules/material-react-table/ && cp -r package.json material-react-table-docs/node_modules/material-react-table/package.json",
31
+ "build": "tsdx build && size-limit && rm -rf material-react-table-docs/node_modules/material-react-table/dist && cp -r dist material-react-table-docs/node_modules/material-react-table/ && cp -r src material-react-table-docs/node_modules/material-react-table/ && cp -r package.json material-react-table-docs/node_modules/material-react-table/package.json",
32
32
  "build-storybook": "build-storybook",
33
33
  "format": "prettier -w .",
34
34
  "lint": "eslint .",
@@ -59,7 +59,7 @@
59
59
  "@emotion/react": "^11.9.0",
60
60
  "@emotion/styled": "^11.8.1",
61
61
  "@etchteam/storybook-addon-status": "^4.2.1",
62
- "@faker-js/faker": "^6.1.2",
62
+ "@faker-js/faker": "^6.2.0",
63
63
  "@mui/icons-material": "^5.6.2",
64
64
  "@mui/material": "^5.6.2",
65
65
  "@size-limit/preset-small-lib": "^7.0.8",
@@ -75,7 +75,7 @@
75
75
  "@types/react": "^17.0.41",
76
76
  "@types/react-dom": "^17.0.14",
77
77
  "babel-loader": "^8.2.5",
78
- "eslint": "^8.13.0",
78
+ "eslint": "^8.14.0",
79
79
  "eslint-plugin-react-hooks": "^4.4.0",
80
80
  "husky": "^7.0.4",
81
81
  "prettier": "^2.6.2",
@@ -86,7 +86,7 @@
86
86
  "storybook-addon-paddings": "^4.3.0",
87
87
  "storybook-dark-mode": "^1.0.9",
88
88
  "tsdx": "^0.14.1",
89
- "tslib": "^2.3.1",
89
+ "tslib": "^2.4.0",
90
90
  "typescript": "^4.6.3"
91
91
  },
92
92
  "peerDependencies": {
@@ -334,6 +334,7 @@ export type MaterialReactTableProps<D extends Record<string, any> = {}> =
334
334
  enableExpandAll?: boolean;
335
335
  enableFullScreenToggle?: boolean;
336
336
  enablePagination?: boolean;
337
+ enablePersistentState?: boolean;
337
338
  enableRowActions?: boolean;
338
339
  enableRowNumbers?: boolean;
339
340
  enableSelectAll?: boolean;
@@ -694,6 +695,7 @@ export type MaterialReactTableProps<D extends Record<string, any> = {}> =
694
695
  showGlobalFilter: boolean;
695
696
  tableInstance: MRT_TableInstance<D>;
696
697
  }) => void;
698
+ persistentStateMode?: 'localStorage' | 'sessionStorage';
697
699
  positionActionsColumn?: 'first' | 'last';
698
700
  positionPagination?: 'bottom' | 'top' | 'both';
699
701
  positionToolbarActions?: 'bottom' | 'top';
@@ -778,6 +780,7 @@ export default <D extends Record<string, any> = {}>({
778
780
  enableToolbarTop = true,
779
781
  icons,
780
782
  localization,
783
+ persistentStateMode = 'sessionStorage',
781
784
  positionActionsColumn = 'first',
782
785
  positionPagination = 'bottom',
783
786
  positionToolbarActions = 'top',
@@ -809,6 +812,7 @@ export default <D extends Record<string, any> = {}>({
809
812
  enableToolbarTop={enableToolbarTop}
810
813
  icons={{ ...MRT_Default_Icons, ...icons }}
811
814
  localization={{ ...MRT_DefaultLocalization_EN, ...localization }}
815
+ persistentStateMode={persistentStateMode}
812
816
  positionActionsColumn={positionActionsColumn}
813
817
  positionPagination={positionPagination}
814
818
  positionToolbarActions={positionToolbarActions}
@@ -20,6 +20,7 @@ import {
20
20
  MRT_FilterType,
21
21
  MRT_Row,
22
22
  MRT_TableInstance,
23
+ MRT_TableState,
23
24
  } from '..';
24
25
  import { MRT_ExpandAllButton } from '../buttons/MRT_ExpandAllButton';
25
26
  import { MRT_ExpandButton } from '../buttons/MRT_ExpandButton';
@@ -47,29 +48,54 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
47
48
  [props.idPrefix],
48
49
  );
49
50
 
51
+ const initialState: Partial<MRT_TableState<D>> = useMemo(() => {
52
+ if (!props.enablePersistentState || !props.idPrefix) {
53
+ return props.initialState;
54
+ }
55
+ if (typeof window === 'undefined') {
56
+ if (process.env.NODE_ENV !== 'production') {
57
+ console.error(
58
+ 'The MRT Persistent Table State feature is not supported if using SSR, but you can wrap your <MaterialReactTable /> in a MUI <NoSsr> tags to let it work',
59
+ );
60
+ }
61
+ return props.initialState;
62
+ }
63
+ const storedState =
64
+ props.persistentStateMode === 'localStorage'
65
+ ? localStorage.getItem(`mrt-${idPrefix}-table-state`)
66
+ : props.persistentStateMode === 'sessionStorage'
67
+ ? sessionStorage.getItem(`mrt-${idPrefix}-table-state`)
68
+ : '{}';
69
+ if (storedState) {
70
+ const parsedState = JSON.parse(storedState);
71
+ if (parsedState) {
72
+ return { ...props.initialState, ...parsedState };
73
+ }
74
+ }
75
+ return props.initialState;
76
+ }, []);
77
+
50
78
  const [currentEditingCell, setCurrentEditingCell] =
51
- useState<MRT_Cell<D> | null>(
52
- props.initialState?.currentEditingCell ?? null,
53
- );
79
+ useState<MRT_Cell<D> | null>(initialState?.currentEditingCell ?? null);
54
80
  const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row<D> | null>(
55
- props.initialState?.currentEditingRow ?? null,
81
+ initialState?.currentEditingRow ?? null,
56
82
  );
57
83
  const [isDensePadding, setIsDensePadding] = useState(
58
- props.initialState?.isDensePadding ?? false,
84
+ initialState?.isDensePadding ?? false,
59
85
  );
60
86
  const [isFullScreen, setIsFullScreen] = useState(
61
- props.initialState?.isFullScreen ?? false,
87
+ initialState?.isFullScreen ?? false,
62
88
  );
63
89
  const [showFilters, setShowFilters] = useState(
64
- props.initialState?.showFilters ?? false,
90
+ initialState?.showFilters ?? false,
65
91
  );
66
92
  const [showGlobalFilter, setShowGlobalFilter] = useState(
67
- props.initialState?.showGlobalFilter ?? false,
93
+ initialState?.showGlobalFilter ?? false,
68
94
  );
69
95
  const [pagination, setPagination] = useState<PaginationState>({
70
- pageIndex: props.initialState?.pagination?.pageIndex ?? 0,
71
- pageSize: props.initialState?.pagination?.pageSize ?? 10,
72
- pageCount: props.initialState?.pagination?.pageCount ?? -1,
96
+ pageIndex: initialState?.pagination?.pageIndex ?? 0,
97
+ pageSize: initialState?.pagination?.pageSize ?? 10,
98
+ pageCount: initialState?.pagination?.pageCount ?? -1,
73
99
  });
74
100
 
75
101
  const [currentFilterTypes, setCurrentFilterTypes] = useState<{
@@ -80,7 +106,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
80
106
  ...getAllLeafColumnDefs(props.columns as MRT_ColumnDef[]).map((c) => ({
81
107
  [c.id as string]:
82
108
  c.filter ??
83
- props?.initialState?.columnFilters?.find((cf) => cf.id === c.id) ??
109
+ initialState?.currentFilterTypes?.[c.id] ??
84
110
  (!!c.filterSelectOptions?.length
85
111
  ? MRT_FILTER_TYPE.EQUALS
86
112
  : MRT_FILTER_TYPE.BEST_MATCH),
@@ -215,12 +241,14 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
215
241
  getSortedRowModel: getSortedRowModelSync(),
216
242
  getSubRows: (originalRow: D) => originalRow.subRows,
217
243
  globalFilterType: currentGlobalFilterType,
218
- idPrefix,
219
244
  onPaginationChange: (updater: any) =>
220
245
  setPagination((old) => functionalUpdate(updater, old)),
221
246
  ...props,
222
247
  columns,
223
248
  data,
249
+ idPrefix,
250
+ //@ts-ignore
251
+ initialState,
224
252
  state: {
225
253
  currentEditingCell,
226
254
  currentEditingRow,
@@ -247,6 +275,32 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
247
275
  setShowGlobalFilter,
248
276
  };
249
277
 
278
+ useEffect(() => {
279
+ if (typeof window === 'undefined' || !props.enablePersistentState) {
280
+ return;
281
+ }
282
+ if (!props.idPrefix && process.env.NODE_ENV !== 'production') {
283
+ console.warn(
284
+ 'a unique idPrefix prop is required for persistent table state to work',
285
+ );
286
+ return;
287
+ }
288
+ const itemArgs: [string, string] = [
289
+ `mrt-${idPrefix}-table-state`,
290
+ JSON.stringify(tableInstance.getState()),
291
+ ];
292
+ if (props.persistentStateMode === 'localStorage') {
293
+ localStorage.setItem(...itemArgs);
294
+ } else if (props.persistentStateMode === 'sessionStorage') {
295
+ sessionStorage.setItem(...itemArgs);
296
+ }
297
+ }, [
298
+ props.enablePersistentState,
299
+ props.idPrefix,
300
+ props.persistentStateMode,
301
+ tableInstance,
302
+ ]);
303
+
250
304
  return (
251
305
  <>
252
306
  <Dialog