material-react-table 0.7.1 → 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.
@@ -14,7 +14,14 @@ import {
14
14
  getCoreRowModelSync,
15
15
  ColumnDef,
16
16
  } from '@tanstack/react-table';
17
- import { MRT_ColumnDef, MRT_FilterType, MRT_Row, MRT_TableInstance } from '..';
17
+ import {
18
+ MRT_Cell,
19
+ MRT_ColumnDef,
20
+ MRT_FilterType,
21
+ MRT_Row,
22
+ MRT_TableInstance,
23
+ MRT_TableState,
24
+ } from '..';
18
25
  import { MRT_ExpandAllButton } from '../buttons/MRT_ExpandAllButton';
19
26
  import { MRT_ExpandButton } from '../buttons/MRT_ExpandButton';
20
27
  import { MRT_ToggleRowActionMenuButton } from '../buttons/MRT_ToggleRowActionMenuButton';
@@ -41,25 +48,54 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
41
48
  [props.idPrefix],
42
49
  );
43
50
 
44
- const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row | null>(
45
- null,
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
+
78
+ const [currentEditingCell, setCurrentEditingCell] =
79
+ useState<MRT_Cell<D> | null>(initialState?.currentEditingCell ?? null);
80
+ const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row<D> | null>(
81
+ initialState?.currentEditingRow ?? null,
46
82
  );
47
83
  const [isDensePadding, setIsDensePadding] = useState(
48
- props.initialState?.isDensePadding ?? false,
84
+ initialState?.isDensePadding ?? false,
49
85
  );
50
86
  const [isFullScreen, setIsFullScreen] = useState(
51
- props.initialState?.isFullScreen ?? false,
87
+ initialState?.isFullScreen ?? false,
52
88
  );
53
89
  const [showFilters, setShowFilters] = useState(
54
- props.initialState?.showFilters ?? false,
90
+ initialState?.showFilters ?? false,
55
91
  );
56
92
  const [showGlobalFilter, setShowGlobalFilter] = useState(
57
- props.initialState?.showGlobalFilter ?? false,
93
+ initialState?.showGlobalFilter ?? false,
58
94
  );
59
95
  const [pagination, setPagination] = useState<PaginationState>({
60
- pageIndex: props.initialState?.pagination?.pageIndex ?? 0,
61
- pageSize: props.initialState?.pagination?.pageSize ?? 10,
62
- pageCount: props.initialState?.pagination?.pageCount ?? -1,
96
+ pageIndex: initialState?.pagination?.pageIndex ?? 0,
97
+ pageSize: initialState?.pagination?.pageSize ?? 10,
98
+ pageCount: initialState?.pagination?.pageCount ?? -1,
63
99
  });
64
100
 
65
101
  const [currentFilterTypes, setCurrentFilterTypes] = useState<{
@@ -70,7 +106,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
70
106
  ...getAllLeafColumnDefs(props.columns as MRT_ColumnDef[]).map((c) => ({
71
107
  [c.id as string]:
72
108
  c.filter ??
73
- props?.initialState?.columnFilters?.find((cf) => cf.id === c.id) ??
109
+ initialState?.currentFilterTypes?.[c.id] ??
74
110
  (!!c.filterSelectOptions?.length
75
111
  ? MRT_FILTER_TYPE.EQUALS
76
112
  : MRT_FILTER_TYPE.BEST_MATCH),
@@ -82,15 +118,13 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
82
118
  props.globalFilterType ?? MRT_FILTER_TYPE.BEST_MATCH_FIRST,
83
119
  );
84
120
 
85
- const table = useMemo(
86
- () => createTable<{ Row: D }>(),
87
- [],
88
- ) as unknown as Table<D>;
121
+ const table = useMemo(() => createTable() as unknown as Table<D>, []);
89
122
 
90
123
  const displayColumns = useMemo(
91
124
  () =>
92
125
  [
93
- (props.enableRowActions || props.enableEditing) &&
126
+ (props.enableRowActions ||
127
+ (props.enableEditing && props.editingMode === 'row')) &&
94
128
  createDisplayColumn(table, {
95
129
  Cell: ({ cell }) => (
96
130
  <MRT_ToggleRowActionMenuButton
@@ -149,6 +183,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
149
183
  }),
150
184
  ].filter(Boolean),
151
185
  [
186
+ props.editingMode,
152
187
  props.enableEditing,
153
188
  props.enableExpandAll,
154
189
  props.enableExpanded,
@@ -206,13 +241,16 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
206
241
  getSortedRowModel: getSortedRowModelSync(),
207
242
  getSubRows: (originalRow: D) => originalRow.subRows,
208
243
  globalFilterType: currentGlobalFilterType,
209
- idPrefix,
210
244
  onPaginationChange: (updater: any) =>
211
245
  setPagination((old) => functionalUpdate(updater, old)),
212
246
  ...props,
213
247
  columns,
214
248
  data,
249
+ idPrefix,
250
+ //@ts-ignore
251
+ initialState,
215
252
  state: {
253
+ currentEditingCell,
216
254
  currentEditingRow,
217
255
  currentFilterTypes,
218
256
  currentGlobalFilterType,
@@ -225,6 +263,9 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
225
263
  ...props.state,
226
264
  },
227
265
  }),
266
+ //@ts-ignore
267
+ setCurrentEditingCell,
268
+ //@ts-ignore
228
269
  setCurrentEditingRow,
229
270
  setCurrentFilterTypes,
230
271
  setCurrentGlobalFilterType,
@@ -234,6 +275,32 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
234
275
  setShowGlobalFilter,
235
276
  };
236
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
+
237
304
  return (
238
305
  <>
239
306
  <Dialog
package/src/utils.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ColumnDef, Table } from '@tanstack/react-table';
2
2
  import { MRT_ColumnDef, MRT_FilterType } from '.';
3
3
  import { MRT_FILTER_TYPE } from './enums';
4
+ import { defaultFilterFNs } from './filtersFNs';
4
5
 
5
6
  export const getAllLeafColumnDefs = (
6
7
  columns: MRT_ColumnDef[],
@@ -40,7 +41,10 @@ export const createDataColumn = <D extends Record<string, any> = {}>(
40
41
  currentFilterTypes: { [key: string]: MRT_FilterType },
41
42
  ): ColumnDef<D> => // @ts-ignore
42
43
  table.createDataColumn(column.id, {
43
- filterType: currentFilterTypes[column.id] || MRT_FILTER_TYPE.BEST_MATCH,
44
+ filterFn:
45
+ currentFilterTypes[column.id] instanceof Function
46
+ ? currentFilterTypes[column.id]
47
+ : defaultFilterFNs[currentFilterTypes[column.id] as MRT_FILTER_TYPE],
44
48
  ...column,
45
49
  }) as any;
46
50