material-react-table 0.8.11 → 0.8.12

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.
@@ -1,9 +1,7 @@
1
1
  import React, { useEffect, useMemo, useState } from 'react';
2
2
  import {
3
3
  FilterFn,
4
- PaginationState,
5
4
  createTable,
6
- functionalUpdate,
7
5
  getExpandedRowModel,
8
6
  getPaginationRowModel,
9
7
  useTableInstance,
@@ -13,6 +11,7 @@ import {
13
11
  getFilteredRowModel,
14
12
  ReactTableGenerics,
15
13
  getFacetedRowModel,
14
+ TableState,
16
15
  } from '@tanstack/react-table';
17
16
  import {
18
17
  MRT_Cell,
@@ -48,9 +47,30 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
48
47
  [props.idPrefix],
49
48
  );
50
49
 
50
+ const showActionColumn =
51
+ props.enableRowActions ||
52
+ (props.enableEditing && props.editingMode === 'row');
53
+
54
+ const showExpandColumn = props.enableExpanding || props.enableGrouping;
55
+
51
56
  const initialState: Partial<MRT_TableState<D>> = useMemo(() => {
57
+ const initState = props.initialState ?? {};
58
+
59
+ initState.columnOrder =
60
+ initState?.columnOrder ?? props.enableColumnOrdering
61
+ ? ([
62
+ showActionColumn && 'mrt-row-actions',
63
+ showExpandColumn && 'mrt-expand',
64
+ props.enableRowSelection && 'mrt-select',
65
+ props.enableRowNumbers && 'mrt-row-numbers',
66
+ ...getAllLeafColumnDefs(props.columns as MRT_ColumnDef[]).map(
67
+ (c) => c.id,
68
+ ),
69
+ ].filter(Boolean) as string[])
70
+ : [];
71
+
52
72
  if (!props.enablePersistentState || !props.idPrefix) {
53
- return props.initialState;
73
+ return initState;
54
74
  }
55
75
  if (typeof window === 'undefined') {
56
76
  if (process.env.NODE_ENV !== 'production') {
@@ -58,7 +78,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
58
78
  '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
79
  );
60
80
  }
61
- return props.initialState;
81
+ return initState;
62
82
  }
63
83
  const storedState =
64
84
  props.persistentStateMode === 'localStorage'
@@ -69,10 +89,10 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
69
89
  if (storedState) {
70
90
  const parsedState = JSON.parse(storedState);
71
91
  if (parsedState) {
72
- return { ...props.initialState, ...parsedState };
92
+ return { ...initState, ...parsedState };
73
93
  }
74
94
  }
75
- return props.initialState;
95
+ return initState;
76
96
  }, []);
77
97
 
78
98
  const [currentEditingCell, setCurrentEditingCell] =
@@ -92,11 +112,6 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
92
112
  const [showGlobalFilter, setShowGlobalFilter] = useState(
93
113
  initialState?.showGlobalFilter ?? false,
94
114
  );
95
- const [pagination, setPagination] = useState<PaginationState>({
96
- pageIndex: initialState?.pagination?.pageIndex ?? 0,
97
- pageSize: initialState?.pagination?.pageSize ?? 10,
98
- pageCount: initialState?.pagination?.pageCount,
99
- });
100
115
 
101
116
  const [currentFilterFns, setCurrentFilterFns] = useState<{
102
117
  [key: string]: MRT_FilterFn;
@@ -123,8 +138,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
123
138
  const displayColumns = useMemo(
124
139
  () =>
125
140
  [
126
- (props.enableRowActions ||
127
- (props.enableEditing && props.editingMode === 'row')) &&
141
+ showActionColumn &&
128
142
  createDisplayColumn(table, {
129
143
  Cell: ({ cell }) => (
130
144
  <MRT_ToggleRowActionMenuButton
@@ -136,7 +150,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
136
150
  id: 'mrt-row-actions',
137
151
  size: 60,
138
152
  }),
139
- (props.enableExpanding || props.enableGrouping) &&
153
+ showExpandColumn &&
140
154
  createDisplayColumn(table, {
141
155
  Cell: ({ cell }) => (
142
156
  <MRT_ExpandButton
@@ -222,15 +236,8 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
222
236
  [props.data, props.state?.isLoading, props.state?.showSkeletons],
223
237
  );
224
238
 
225
- const [columnOrder, setColumnOrder] = useState<string[]>(() =>
226
- initialState?.columnOrder ?? props.enableColumnOrdering
227
- ? getAllLeafColumnDefs(columns as MRT_ColumnDef[]).map((c) => c.id)
228
- : [],
229
- );
230
-
231
239
  //@ts-ignore
232
240
  const tableInstance = {
233
- //@ts-ignore
234
241
  ...useTableInstance(table, {
235
242
  filterFns: defaultFilterFNs,
236
243
  getCoreRowModel: getCoreRowModel(),
@@ -243,9 +250,6 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
243
250
  getSubRows: (row) => (row as MRT_Row)?.subRows,
244
251
  //@ts-ignore
245
252
  globalFilterFn: currentGlobalFilterFn,
246
- onColumnOrderChange: setColumnOrder,
247
- onPaginationChange: (updater: any) =>
248
- setPagination((old) => functionalUpdate(updater, old)),
249
253
  ...props,
250
254
  //@ts-ignore
251
255
  columns,
@@ -253,19 +257,16 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
253
257
  idPrefix,
254
258
  initialState,
255
259
  state: {
256
- columnOrder,
257
260
  currentEditingCell,
258
261
  currentEditingRow,
259
262
  currentFilterFns,
260
263
  currentGlobalFilterFn,
261
264
  isDensePadding,
262
265
  isFullScreen,
263
- //@ts-ignore
264
- pagination,
265
266
  showFilters,
266
267
  showGlobalFilter,
267
268
  ...props.state,
268
- },
269
+ } as TableState,
269
270
  }),
270
271
  setCurrentEditingCell,
271
272
  setCurrentEditingRow,