material-react-table 0.13.2 → 0.13.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.
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ColumnDef, ColumnOrderState, Table, Updater } from '@tanstack/react-table';
2
- import { MRT_Column, MRT_ColumnDef, MRT_FilterFn } from '.';
2
+ import { MaterialReactTableProps, MRT_Column, MRT_ColumnDef, MRT_FilterFn } from '.';
3
3
  export declare const getAllLeafColumnDefs: (columns: MRT_ColumnDef[]) => MRT_ColumnDef[];
4
4
  export declare const createGroup: <D extends Record<string, any> = {}>(table: Table<D>, column: MRT_ColumnDef<D>, currentFilterFns: {
5
5
  [key: string]: import("@tanstack/react-table").FilterFnOption<import("@tanstack/react-table").TableGenerics>;
@@ -7,7 +7,10 @@ export declare const createGroup: <D extends Record<string, any> = {}>(table: Ta
7
7
  export declare const createDataColumn: <D extends Record<string, any> = {}>(table: Table<D>, column: MRT_ColumnDef<D>, currentFilterFns: {
8
8
  [key: string]: import("@tanstack/react-table").FilterFnOption<import("@tanstack/react-table").TableGenerics>;
9
9
  }) => ColumnDef<D>;
10
- export declare const createDisplayColumn: <D extends Record<string, any> = {}>(table: Table<D>, column: Pick<MRT_ColumnDef<D>, "accessorFN" | "footer" | "columns" | "filterFn" | "id" | "accessorKey" | "accessorFn" | "cell" | "meta" | "enableHiding" | "enablePinning" | "enableColumnFilter" | "enableGlobalFilter" | "sortingFn" | "sortDescFirst" | "enableSorting" | "enableMultiSort" | "invertSorting" | "sortUndefined" | "aggregationFn" | "aggregatedCell" | "enableGrouping" | "enableResizing" | "size" | "minSize" | "maxSize" | "Cell" | "Edit" | "Filter" | "Footer" | "Header" | "enableClickToCopy" | "enableColumnActions" | "enableColumnOrdering" | "enableEditing" | "enabledColumnFilterOptions" | "filterSelectOptions" | "muiTableBodyCellCopyButtonProps" | "muiTableBodyCellEditTextFieldProps" | "muiTableBodyCellProps" | "muiTableFooterCellProps" | "muiTableHeadCellColumnActionsButtonProps" | "muiTableHeadCellFilterTextFieldProps" | "muiTableHeadCellProps" | "onMrtCellEditBlur" | "onMrtCellEditChange" | "onMrtFilterValueChange"> & {
10
+ export declare const createDisplayColumn: <D extends Record<string, any> = {}>(table: Table<D>, column: Pick<MRT_ColumnDef<D>, "accessorFn" | "footer" | "columns" | "filterFn" | "id" | "accessorKey" | "meta" | "enableHiding" | "enablePinning" | "enableColumnFilter" | "enableGlobalFilter" | "sortingFn" | "sortDescFirst" | "enableSorting" | "enableMultiSort" | "invertSorting" | "sortUndefined" | "aggregationFn" | "enableGrouping" | "enableResizing" | "size" | "minSize" | "maxSize" | "AggregatedCell" | "Cell" | "Edit" | "Filter" | "Footer" | "Header" | "enableClickToCopy" | "enableColumnActions" | "enableColumnOrdering" | "enableEditing" | "enabledColumnFilterOptions" | "filterSelectOptions" | "muiTableBodyCellCopyButtonProps" | "muiTableBodyCellEditTextFieldProps" | "muiTableBodyCellProps" | "muiTableFooterCellProps" | "muiTableHeadCellColumnActionsButtonProps" | "muiTableHeadCellFilterTextFieldProps" | "muiTableHeadCellProps" | "onCellEditBlur" | "onCellEditChanged" | "onColumnFilterValueChanged" | "onColumnFilterValueChangedDebounced"> & {
11
11
  header?: string | undefined;
12
12
  }) => ColumnDef<D>;
13
13
  export declare const reorderColumn: (movingColumn: MRT_Column, receivingColumn: MRT_Column, columnOrder: ColumnOrderState, setColumnOrder: (updater: Updater<ColumnOrderState>) => void) => void;
14
+ export declare const getLeadingDisplayColumnIds: (props: MaterialReactTableProps<any>) => string[];
15
+ export declare const getTrailingDisplayColumnIds: (props: MaterialReactTableProps<any>) => (string | false | undefined)[];
16
+ export declare const getDefaultColumnOrderIds: (props: MaterialReactTableProps<any>) => string[];
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.13.2",
2
+ "version": "0.13.3",
3
3
  "license": "MIT",
4
4
  "name": "material-react-table",
5
5
  "description": "A fully featured Material UI implementation of TanStack React Table, inspired by material-table and the MUI X DataGrid, written from the ground up in TypeScript.",
@@ -514,7 +514,11 @@ export type MaterialReactTableProps<D extends Record<string, any> = {}> =
514
514
  }) => TableCellProps);
515
515
  muiTableFooterProps?:
516
516
  | TableFooterProps
517
- | (({ instance }: { instance: MRT_TableInstance<D> }) => TableFooterProps);
517
+ | (({
518
+ instance,
519
+ }: {
520
+ instance: MRT_TableInstance<D>;
521
+ }) => TableFooterProps);
518
522
  muiTableFooterRowProps?:
519
523
  | TableRowProps
520
524
  | (({
@@ -838,6 +842,7 @@ export default <D extends Record<string, any> = {}>({
838
842
  enableMultiRowSelection = true,
839
843
  enablePagination = true,
840
844
  enablePinning = false,
845
+ enableRowSelection = false,
841
846
  enableSelectAll = true,
842
847
  enableSorting = true,
843
848
  enableStickyHeader = false,
@@ -850,8 +855,8 @@ export default <D extends Record<string, any> = {}>({
850
855
  localization,
851
856
  persistentStateMode = 'sessionStorage',
852
857
  positionActionsColumn = 'first',
853
- positionPagination = 'bottom',
854
858
  positionGlobalFilter = 'right',
859
+ positionPagination = 'bottom',
855
860
  positionToolbarActions = 'top',
856
861
  positionToolbarAlertBanner = 'top',
857
862
  rowNumberMode = 'original',
@@ -877,6 +882,7 @@ export default <D extends Record<string, any> = {}>({
877
882
  enableMultiRowSelection={enableMultiRowSelection}
878
883
  enablePagination={enablePagination}
879
884
  enablePinning={enablePinning}
885
+ enableRowSelection={enableRowSelection}
880
886
  enableSelectAll={enableSelectAll}
881
887
  enableSorting={enableSorting}
882
888
  enableStickyHeader={enableStickyHeader}
@@ -2,6 +2,7 @@ import React, { FC, useMemo } from 'react';
2
2
  import { Button, Menu, Divider, Box } from '@mui/material';
3
3
  import { MRT_ShowHideColumnsMenuItems } from './MRT_ShowHideColumnsMenuItems';
4
4
  import type { MRT_Column, MRT_TableInstance } from '..';
5
+ import { getDefaultColumnOrderIds } from '../utils';
5
6
 
6
7
  interface Props {
7
8
  anchorEl: HTMLElement | null;
@@ -88,7 +89,13 @@ export const MRT_ShowHideColumnsMenu: FC<Props> = ({
88
89
  </Button>
89
90
  )}
90
91
  {!isSubMenu && enableColumnOrdering && (
91
- <Button onClick={() => instance.resetColumnOrder()}>
92
+ <Button
93
+ onClick={() =>
94
+ instance.setColumnOrder(
95
+ getDefaultColumnOrderIds(instance.options as any),
96
+ )
97
+ }
98
+ >
92
99
  {localization.resetOrder}
93
100
  </Button>
94
101
  )}
@@ -34,6 +34,7 @@ import {
34
34
  createDisplayColumn,
35
35
  createGroup,
36
36
  getAllLeafColumnDefs,
37
+ getDefaultColumnOrderIds,
37
38
  } from '../utils';
38
39
  import { defaultFilterFNs } from '../filtersFNs';
39
40
  import { Box, Dialog, Grow } from '@mui/material';
@@ -48,28 +49,10 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
48
49
  [props.tableId],
49
50
  );
50
51
 
51
- const showActionColumn =
52
- props.enableRowActions ||
53
- (props.enableEditing && props.editingMode === 'row');
54
-
55
- const showExpandColumn = props.enableExpanding || props.enableGrouping;
56
-
57
52
  const initialState: Partial<MRT_TableState<D>> = useMemo(() => {
58
53
  const initState = props.initialState ?? {};
59
-
60
54
  initState.columnOrder =
61
- initState?.columnOrder ??
62
- (props.enableColumnOrdering || props.enableGrouping)
63
- ? ([
64
- showActionColumn && 'mrt-row-actions',
65
- showExpandColumn && 'mrt-expand',
66
- props.enableRowSelection && 'mrt-select',
67
- props.enableRowNumbers && 'mrt-row-numbers',
68
- ...getAllLeafColumnDefs(props.columns as MRT_ColumnDef[]).map(
69
- (c) => c.id,
70
- ),
71
- ].filter(Boolean) as string[])
72
- : [];
55
+ initState.columnOrder ?? getDefaultColumnOrderIds(props);
73
56
 
74
57
  if (!props.enablePersistentState || !props.tableId) {
75
58
  return initState;
@@ -97,6 +80,9 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
97
80
  return initState;
98
81
  }, []);
99
82
 
83
+ const [columnOrder, setColumnOrder] = useState(
84
+ initialState.columnOrder ?? [],
85
+ );
100
86
  const [currentEditingCell, setCurrentEditingCell] =
101
87
  useState<MRT_Cell<D> | null>(initialState?.currentEditingCell ?? null);
102
88
  const [currentEditingRow, setCurrentEditingRow] = useState<MRT_Row<D> | null>(
@@ -148,14 +134,13 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
148
134
  getSortedRowModel: getSortedRowModel(),
149
135
  getSubRows: (row) => (row as MRT_Row)?.subRows,
150
136
  tableId,
151
- initialState,
152
137
  }) as Table<D>,
153
138
  [],
154
139
  );
155
140
 
156
- const [leadingDisplayColumns, trailingDisplayColumns] = useMemo(() => {
157
- const leadingDisplayColumns = [
158
- showActionColumn &&
141
+ const displayColumns = useMemo(() => {
142
+ return [
143
+ columnOrder.includes('mrt-row-actions') &&
159
144
  createDisplayColumn(table, {
160
145
  Cell: ({ cell }) => (
161
146
  <MRT_ToggleRowActionMenuButton
@@ -169,7 +154,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
169
154
  muiTableHeadCellProps: props.muiTableHeadCellProps,
170
155
  size: 70,
171
156
  }),
172
- showExpandColumn &&
157
+ columnOrder.includes('mrt-expand') &&
173
158
  createDisplayColumn(table, {
174
159
  Cell: ({ cell }) => (
175
160
  <MRT_ExpandButton row={cell.row as any} instance={instance} />
@@ -184,7 +169,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
184
169
  muiTableHeadCellProps: props.muiTableHeadCellProps,
185
170
  size: 50,
186
171
  }),
187
- props.enableRowSelection &&
172
+ columnOrder.includes('mrt-select') &&
188
173
  createDisplayColumn(table, {
189
174
  Cell: ({ cell }) => (
190
175
  <MRT_SelectCheckbox row={cell.row as any} instance={instance} />
@@ -199,7 +184,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
199
184
  muiTableHeadCellProps: props.muiTableHeadCellProps,
200
185
  size: 50,
201
186
  }),
202
- props.enableRowNumbers &&
187
+ columnOrder.includes('mrt-row-numbers') &&
203
188
  createDisplayColumn(table, {
204
189
  Cell: ({ cell }) => cell.row.index + 1,
205
190
  Header: () => props.localization?.rowNumber,
@@ -210,22 +195,8 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
210
195
  size: 50,
211
196
  }),
212
197
  ].filter(Boolean) as MRT_ColumnDef<D>[];
213
-
214
- const trailingDisplayColumns = [] as MRT_ColumnDef<D>[];
215
-
216
- if (props.enableRowActions && props.positionActionsColumn === 'last') {
217
- trailingDisplayColumns.push(
218
- ...leadingDisplayColumns.splice(
219
- leadingDisplayColumns.findIndex(
220
- (col) => col.id === 'mrt-row-actions',
221
- ),
222
- 1,
223
- ),
224
- );
225
- }
226
-
227
- return [leadingDisplayColumns, trailingDisplayColumns];
228
198
  }, [
199
+ columnOrder,
229
200
  props.editingMode,
230
201
  props.enableEditing,
231
202
  props.enableExpandAll,
@@ -244,13 +215,12 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
244
215
 
245
216
  const columns = useMemo(
246
217
  () => [
247
- ...leadingDisplayColumns,
218
+ ...displayColumns,
248
219
  ...props.columns.map((column) =>
249
220
  column.columns
250
221
  ? createGroup(table, column as any, currentFilterFns)
251
222
  : createDataColumn(table, column as any, currentFilterFns),
252
223
  ),
253
- ...trailingDisplayColumns,
254
224
  ],
255
225
  [table, props.columns, currentFilterFns],
256
226
  );
@@ -277,13 +247,16 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
277
247
  const instance = {
278
248
  //@ts-ignore
279
249
  ...useTableInstance(table, {
250
+ onColumnOrderChange: setColumnOrder,
280
251
  ...props,
281
252
  //@ts-ignore
282
253
  columns,
283
254
  data,
284
255
  //@ts-ignore
285
256
  globalFilterFn: currentGlobalFilterFn,
257
+ initialState,
286
258
  state: {
259
+ columnOrder,
287
260
  currentEditingCell,
288
261
  currentEditingRow,
289
262
  currentFilterFns,
package/src/utils.ts CHANGED
@@ -4,7 +4,12 @@ import {
4
4
  Table,
5
5
  Updater,
6
6
  } from '@tanstack/react-table';
7
- import { MRT_Column, MRT_ColumnDef, MRT_FilterFn } from '.';
7
+ import {
8
+ MaterialReactTableProps,
9
+ MRT_Column,
10
+ MRT_ColumnDef,
11
+ MRT_FilterFn,
12
+ } from '.';
8
13
  import { defaultFilterFNs } from './filtersFNs';
9
14
 
10
15
  export const getAllLeafColumnDefs = (
@@ -74,3 +79,30 @@ export const reorderColumn = (
74
79
  );
75
80
  setColumnOrder([...columnOrder]);
76
81
  };
82
+
83
+ export const getLeadingDisplayColumnIds = (
84
+ props: MaterialReactTableProps<any>,
85
+ ) =>
86
+ [
87
+ ((props.positionActionsColumn === 'first' && props.enableRowActions) ||
88
+ (props.enableEditing && props.editingMode === 'row')) &&
89
+ 'mrt-row-actions',
90
+ (props.enableExpanding || props.enableGrouping) && 'mrt-expand',
91
+ props.enableRowSelection && 'mrt-select',
92
+ props.enableRowNumbers && 'mrt-row-numbers',
93
+ ].filter(Boolean) as string[];
94
+
95
+ export const getTrailingDisplayColumnIds = (
96
+ props: MaterialReactTableProps<any>,
97
+ ) => [
98
+ ((props.positionActionsColumn === 'last' && props.enableRowActions) ||
99
+ (props.enableEditing && props.editingMode === 'row')) &&
100
+ 'mrt-row-actions',
101
+ ];
102
+
103
+ export const getDefaultColumnOrderIds = (props: MaterialReactTableProps<any>) =>
104
+ [
105
+ ...getLeadingDisplayColumnIds(props),
106
+ ...getAllLeafColumnDefs(props.columns as MRT_ColumnDef[]).map((c) => c.id),
107
+ ...getTrailingDisplayColumnIds(props),
108
+ ].filter(Boolean) as string[];