material-react-table 0.8.11 → 0.8.14

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.
Files changed (34) hide show
  1. package/dist/MaterialReactTable.d.ts +7 -5
  2. package/dist/buttons/MRT_GrabHandleButton.d.ts +8 -0
  3. package/dist/localization.d.ts +1 -0
  4. package/dist/material-react-table.cjs.development.js +2087 -2033
  5. package/dist/material-react-table.cjs.development.js.map +1 -1
  6. package/dist/material-react-table.cjs.production.min.js +1 -1
  7. package/dist/material-react-table.cjs.production.min.js.map +1 -1
  8. package/dist/material-react-table.esm.js +233 -179
  9. package/dist/material-react-table.esm.js.map +1 -1
  10. package/dist/menus/MRT_ShowHideColumnsMenu.d.ts +1 -1
  11. package/dist/menus/MRT_ShowHideColumnsMenuItems.d.ts +1 -0
  12. package/dist/table/MRT_TableRoot.d.ts +0 -1
  13. package/dist/utils.d.ts +1 -1
  14. package/package.json +12 -12
  15. package/src/MaterialReactTable.tsx +8 -6
  16. package/src/body/MRT_TableBodyCell.tsx +8 -9
  17. package/src/buttons/MRT_ColumnPinningButtons.tsx +1 -1
  18. package/src/buttons/MRT_CopyButton.tsx +7 -3
  19. package/src/{head/MRT_TableHeadCellGrabHandle.tsx → buttons/MRT_GrabHandleButton.tsx} +2 -3
  20. package/src/buttons/MRT_ToggleColumnActionMenuButton.tsx +5 -4
  21. package/src/footer/MRT_TableFooterCell.tsx +2 -2
  22. package/src/footer/MRT_TableFooterRow.tsx +4 -4
  23. package/src/head/MRT_DraggableTableHeadCell.tsx +15 -9
  24. package/src/head/MRT_TableHeadCell.tsx +6 -12
  25. package/src/head/MRT_TableHeadCellFilterLabel.tsx +3 -1
  26. package/src/head/MRT_TableHeadCellResizeHandle.tsx +3 -2
  27. package/src/head/MRT_TableHeadCellSortLabel.tsx +4 -8
  28. package/src/inputs/MRT_FilterTextField.tsx +1 -1
  29. package/src/inputs/MRT_SelectCheckbox.tsx +6 -1
  30. package/src/localization.ts +2 -0
  31. package/src/menus/MRT_ShowHideColumnsMenu.tsx +25 -38
  32. package/src/menus/MRT_ShowHideColumnsMenuItems.tsx +74 -21
  33. package/src/table/MRT_TableRoot.tsx +49 -41
  34. package/dist/head/MRT_TableHeadCellGrabHandle.d.ts +0 -9
@@ -1,33 +1,65 @@
1
- import React, { FC } from 'react';
2
- import { FormControlLabel, MenuItem, Switch } from '@mui/material';
3
- import type { MRT_Column, MRT_TableInstance } from '..';
1
+ import React, { FC, Ref } from 'react';
2
+ import { useDrag, useDrop } from 'react-dnd';
3
+ import { Box, FormControlLabel, MenuItem, Switch } from '@mui/material';
4
4
  import { MRT_ColumnPinningButtons } from '../buttons/MRT_ColumnPinningButtons';
5
+ import { MRT_GrabHandleButton } from '../buttons/MRT_GrabHandleButton';
6
+ import type { MRT_Column, MRT_TableInstance } from '..';
5
7
 
6
8
  interface Props {
9
+ allColumns: MRT_Column[];
7
10
  column: MRT_Column;
8
11
  isSubMenu?: boolean;
9
12
  tableInstance: MRT_TableInstance;
10
13
  }
11
14
 
12
15
  export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
16
+ allColumns,
13
17
  column,
14
18
  isSubMenu,
15
19
  tableInstance,
16
20
  }) => {
17
21
  const {
18
22
  getState,
19
- options: { onToggleColumnVisibility },
23
+ options: { enableColumnOrdering, onToggleColumnVisibility },
24
+ setColumnOrder,
20
25
  } = tableInstance;
21
26
 
22
- const { columnVisibility } = getState();
27
+ const { columnOrder, columnVisibility } = getState();
28
+
29
+ const { columnDef, columnDefType } = column;
30
+
31
+ const reorder = (movingColumn: MRT_Column, receivingColumn: MRT_Column) => {
32
+ if (movingColumn.getCanPin()) {
33
+ movingColumn.pin(receivingColumn.getIsPinned());
34
+ }
35
+ columnOrder.splice(
36
+ columnOrder.indexOf(receivingColumn.id),
37
+ 0,
38
+ columnOrder.splice(columnOrder.indexOf(movingColumn.id), 1)[0],
39
+ );
40
+ setColumnOrder([...columnOrder]);
41
+ };
42
+
43
+ const [, dropRef] = useDrop({
44
+ accept: 'column',
45
+ drop: (movingColumn: MRT_Column) => reorder(movingColumn, column),
46
+ });
47
+
48
+ const [, dragRef, previewRef] = useDrag({
49
+ collect: (monitor) => ({
50
+ isDragging: monitor.isDragging(),
51
+ }),
52
+ item: () => column,
53
+ type: 'column',
54
+ });
23
55
 
24
56
  const switchChecked =
25
- (column.columnDefType !== 'group' && column.getIsVisible()) ||
26
- (column.columnDefType === 'group' &&
57
+ (columnDefType !== 'group' && column.getIsVisible()) ||
58
+ (columnDefType === 'group' &&
27
59
  column.getLeafColumns().some((col) => col.getIsVisible()));
28
60
 
29
61
  const handleToggleColumnHidden = (column: MRT_Column) => {
30
- if (column.columnDefType === 'group') {
62
+ if (columnDefType === 'group') {
31
63
  column?.columns?.forEach?.((childColumn: MRT_Column) => {
32
64
  childColumn.toggleVisibility(!switchChecked);
33
65
  });
@@ -44,6 +76,7 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
44
76
  return (
45
77
  <>
46
78
  <MenuItem
79
+ ref={columnDefType === 'data' ? dropRef : undefined}
47
80
  sx={{
48
81
  alignItems: 'center',
49
82
  justifyContent: 'flex-start',
@@ -52,23 +85,43 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
52
85
  py: '6px',
53
86
  }}
54
87
  >
55
- {!isSubMenu && column.getCanPin() && (
56
- <MRT_ColumnPinningButtons
57
- column={column}
58
- tableInstance={tableInstance}
88
+ <Box ref={previewRef} sx={{ display: 'flex', flexWrap: 'nowrap' }}>
89
+ {columnDefType !== 'group' &&
90
+ enableColumnOrdering &&
91
+ columnDef.enableColumnOrdering !== false &&
92
+ !allColumns.some((col) => col.columnDefType === 'group') && (
93
+ <MRT_GrabHandleButton
94
+ ref={dragRef as Ref<HTMLButtonElement>}
95
+ tableInstance={tableInstance}
96
+ />
97
+ )}
98
+ {!isSubMenu && column.getCanPin() && (
99
+ <MRT_ColumnPinningButtons
100
+ column={column}
101
+ tableInstance={tableInstance}
102
+ />
103
+ )}
104
+ <FormControlLabel
105
+ componentsProps={{
106
+ typography: {
107
+ sx: {
108
+ mb: 0,
109
+ opacity: columnDefType !== 'display' ? 1 : 0.5,
110
+ },
111
+ },
112
+ }}
113
+ checked={switchChecked}
114
+ control={<Switch />}
115
+ disabled={(isSubMenu && switchChecked) || !column.getCanHide()}
116
+ label={columnDef.header}
117
+ onChange={() => handleToggleColumnHidden(column)}
118
+ sx={{ ml: '4px' }}
59
119
  />
60
- )}
61
- <FormControlLabel
62
- componentsProps={{ typography: { sx: { marginBottom: 0 } } }}
63
- checked={switchChecked}
64
- control={<Switch />}
65
- disabled={(isSubMenu && switchChecked) || !column.getCanHide()}
66
- label={column.columnDef.header}
67
- onChange={() => handleToggleColumnHidden(column)}
68
- />
120
+ </Box>
69
121
  </MenuItem>
70
122
  {column.columns?.map((c: MRT_Column, i) => (
71
123
  <MRT_ShowHideColumnsMenuItems
124
+ allColumns={allColumns}
72
125
  key={`${i}-${c.id}`}
73
126
  column={c}
74
127
  isSubMenu={isSubMenu}
@@ -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;
@@ -118,13 +133,29 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
118
133
  MRT_FILTER_OPTION | FilterFn<ReactTableGenerics> | string | number | symbol
119
134
  >(props.globalFilterFn ?? MRT_FILTER_OPTION.FUZZY);
120
135
 
121
- const table = useMemo(() => createTable(), []);
136
+ const table = useMemo(
137
+ () =>
138
+ createTable().setOptions({
139
+ //@ts-ignore
140
+ filterFns: defaultFilterFNs,
141
+ getCoreRowModel: getCoreRowModel(),
142
+ getExpandedRowModel: getExpandedRowModel(),
143
+ getFacetedRowModel: getFacetedRowModel(),
144
+ getFilteredRowModel: getFilteredRowModel(),
145
+ getGroupedRowModel: getGroupedRowModel(),
146
+ getPaginationRowModel: getPaginationRowModel(),
147
+ getSortedRowModel: getSortedRowModel(),
148
+ getSubRows: (row) => (row as MRT_Row)?.subRows,
149
+ idPrefix,
150
+ initialState,
151
+ }),
152
+ [],
153
+ );
122
154
 
123
155
  const displayColumns = useMemo(
124
156
  () =>
125
157
  [
126
- (props.enableRowActions ||
127
- (props.enableEditing && props.editingMode === 'row')) &&
158
+ showActionColumn &&
128
159
  createDisplayColumn(table, {
129
160
  Cell: ({ cell }) => (
130
161
  <MRT_ToggleRowActionMenuButton
@@ -136,7 +167,7 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
136
167
  id: 'mrt-row-actions',
137
168
  size: 60,
138
169
  }),
139
- (props.enableExpanding || props.enableGrouping) &&
170
+ showExpandColumn &&
140
171
  createDisplayColumn(table, {
141
172
  Cell: ({ cell }) => (
142
173
  <MRT_ExpandButton
@@ -222,50 +253,27 @@ export const MRT_TableRoot = <D extends Record<string, any> = {}>(
222
253
  [props.data, props.state?.isLoading, props.state?.showSkeletons],
223
254
  );
224
255
 
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
256
  //@ts-ignore
232
257
  const tableInstance = {
233
258
  //@ts-ignore
234
259
  ...useTableInstance(table, {
235
- filterFns: defaultFilterFNs,
236
- getCoreRowModel: getCoreRowModel(),
237
- getExpandedRowModel: getExpandedRowModel(),
238
- getFacetedRowModel: getFacetedRowModel(),
239
- getFilteredRowModel: getFilteredRowModel(),
240
- getGroupedRowModel: getGroupedRowModel(),
241
- getPaginationRowModel: getPaginationRowModel(),
242
- getSortedRowModel: getSortedRowModel(),
243
- getSubRows: (row) => (row as MRT_Row)?.subRows,
244
- //@ts-ignore
245
- globalFilterFn: currentGlobalFilterFn,
246
- onColumnOrderChange: setColumnOrder,
247
- onPaginationChange: (updater: any) =>
248
- setPagination((old) => functionalUpdate(updater, old)),
249
260
  ...props,
250
261
  //@ts-ignore
251
262
  columns,
252
263
  data,
253
- idPrefix,
254
- initialState,
264
+ //@ts-ignore
265
+ globalFilterFn: currentGlobalFilterFn,
255
266
  state: {
256
- columnOrder,
257
267
  currentEditingCell,
258
268
  currentEditingRow,
259
269
  currentFilterFns,
260
270
  currentGlobalFilterFn,
261
271
  isDensePadding,
262
272
  isFullScreen,
263
- //@ts-ignore
264
- pagination,
265
273
  showFilters,
266
274
  showGlobalFilter,
267
275
  ...props.state,
268
- },
276
+ } as TableState,
269
277
  }),
270
278
  setCurrentEditingCell,
271
279
  setCurrentEditingRow,
@@ -1,9 +0,0 @@
1
- import { FC, Ref } from 'react';
2
- import { MRT_Header, MRT_TableInstance } from '..';
3
- interface Props {
4
- header: MRT_Header;
5
- ref: Ref<HTMLButtonElement>;
6
- tableInstance: MRT_TableInstance;
7
- }
8
- export declare const MRT_TableHeadCellGrabHandle: FC<Props>;
9
- export {};