material-react-table 0.34.1 → 0.35.0

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.
@@ -18,34 +18,34 @@ export const MRT_EditCellTextField: FC<Props> = ({ cell, table }) => {
18
18
  getState,
19
19
  options: {
20
20
  tableId,
21
- enableEditing,
22
21
  muiTableBodyCellEditTextFieldProps,
23
22
  onCellEditBlur,
24
23
  onCellEditChange,
25
24
  },
26
- setCurrentEditingCell,
27
- setCurrentEditingRow,
25
+ setEditingCell,
26
+ setEditingRow,
28
27
  } = table;
29
28
  const { column, row } = cell;
30
29
  const { columnDef } = column;
30
+ const { editingRow } = getState();
31
31
 
32
- const [value, setValue] = useState(cell.getValue<string>());
32
+ const [value, setValue] = useState(() => cell.getValue<string>());
33
33
 
34
34
  const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
35
35
  setValue(event.target.value);
36
- columnDef.onCellEditChange?.({ event, cell, table });
37
- onCellEditChange?.({ event, cell, table });
36
+ columnDef.onCellEditChange?.({ event, cell, table, value });
37
+ onCellEditChange?.({ event, cell, table, value });
38
38
  };
39
39
 
40
40
  const handleBlur = (event: FocusEvent<HTMLInputElement>) => {
41
- if (getState().currentEditingRow) {
41
+ if (editingRow) {
42
42
  if (!row._valuesCache) row._valuesCache = {};
43
43
  (row._valuesCache as Record<string, any>)[column.id] = value;
44
- setCurrentEditingRow({ ...getState().currentEditingRow } as MRT_Row);
44
+ setEditingRow({ ...editingRow } as MRT_Row);
45
45
  }
46
- setCurrentEditingCell(null);
47
- columnDef.onCellEditBlur?.({ event, cell, table });
48
- onCellEditBlur?.({ event, cell, table });
46
+ setEditingCell(null);
47
+ columnDef.onCellEditBlur?.({ event, cell, table, value });
48
+ onCellEditBlur?.({ event, cell, table, value });
49
49
  };
50
50
 
51
51
  const mTableBodyCellEditTextFieldProps =
@@ -66,14 +66,14 @@ export const MRT_EditCellTextField: FC<Props> = ({ cell, table }) => {
66
66
  ...mcTableBodyCellEditTextFieldProps,
67
67
  };
68
68
 
69
- if (enableEditing && columnDef.enableEditing !== false && columnDef.Edit) {
69
+ if (columnDef.Edit) {
70
70
  return <>{columnDef.Edit?.({ cell, column, table })}</>;
71
71
  }
72
72
 
73
73
  return (
74
74
  <TextField
75
75
  id={`mrt-${tableId}-edit-cell-text-field-${cell.id}`}
76
- margin="dense"
76
+ margin="none"
77
77
  onBlur={handleBlur}
78
78
  onChange={handleChange}
79
79
  onClick={(e: MouseEvent<HTMLInputElement>) => e.stopPropagation()}
@@ -1,9 +1,4 @@
1
- import React, {
2
- ChangeEvent,
3
- MouseEvent,
4
- useCallback,
5
- useState,
6
- } from 'react';
1
+ import React, { ChangeEvent, MouseEvent, useCallback, useState } from 'react';
7
2
  import {
8
3
  Collapse,
9
4
  debounce,
@@ -64,8 +64,9 @@ export const MRT_ShowHideColumnsMenu = <
64
64
  getRightLeafColumns(),
65
65
  ]) as MRT_Column<TData>[];
66
66
 
67
- const [currentHoveredColumn, setCurrentHoveredColumn] =
68
- useState<MRT_Column<TData> | null>(null);
67
+ const [hoveredColumn, setHoveredColumn] = useState<MRT_Column<TData> | null>(
68
+ null,
69
+ );
69
70
 
70
71
  return (
71
72
  <Menu
@@ -123,10 +124,10 @@ export const MRT_ShowHideColumnsMenu = <
123
124
  <MRT_ShowHideColumnsMenuItems
124
125
  allColumns={allColumns}
125
126
  column={column}
126
- currentHoveredColumn={currentHoveredColumn}
127
+ hoveredColumn={hoveredColumn}
127
128
  isSubMenu={isSubMenu}
128
129
  key={`${index}-${column.id}`}
129
- setCurrentHoveredColumn={setCurrentHoveredColumn}
130
+ setHoveredColumn={setHoveredColumn}
130
131
  table={table}
131
132
  />
132
133
  ))}
@@ -21,9 +21,9 @@ import type { MRT_Column, MRT_TableInstance } from '..';
21
21
  interface Props<TData extends Record<string, any> = {}> {
22
22
  allColumns: MRT_Column<TData>[];
23
23
  column: MRT_Column<TData>;
24
- currentHoveredColumn: MRT_Column<TData> | null;
24
+ hoveredColumn: MRT_Column<TData> | null;
25
25
  isSubMenu?: boolean;
26
- setCurrentHoveredColumn: Dispatch<SetStateAction<MRT_Column<TData> | null>>;
26
+ setHoveredColumn: Dispatch<SetStateAction<MRT_Column<TData> | null>>;
27
27
  table: MRT_TableInstance<TData>;
28
28
  }
29
29
 
@@ -31,8 +31,8 @@ export const MRT_ShowHideColumnsMenuItems = <
31
31
  TData extends Record<string, any> = {},
32
32
  >({
33
33
  allColumns,
34
- currentHoveredColumn,
35
- setCurrentHoveredColumn,
34
+ hoveredColumn,
35
+ setHoveredColumn,
36
36
  column,
37
37
  isSubMenu,
38
38
  table,
@@ -77,15 +77,15 @@ export const MRT_ShowHideColumnsMenuItems = <
77
77
 
78
78
  const handleDragEnd = (_e: DragEvent<HTMLButtonElement>) => {
79
79
  setIsDragging(false);
80
- setCurrentHoveredColumn(null);
81
- if (currentHoveredColumn) {
82
- setColumnOrder(reorderColumn(column, currentHoveredColumn, columnOrder));
80
+ setHoveredColumn(null);
81
+ if (hoveredColumn) {
82
+ setColumnOrder(reorderColumn(column, hoveredColumn, columnOrder));
83
83
  }
84
84
  };
85
85
 
86
86
  const handleDragEnter = (_e: DragEvent) => {
87
87
  if (!isDragging) {
88
- setCurrentHoveredColumn(column);
88
+ setHoveredColumn(column);
89
89
  }
90
90
  };
91
91
 
@@ -102,7 +102,7 @@ export const MRT_ShowHideColumnsMenuItems = <
102
102
  opacity: isDragging ? 0.5 : 1,
103
103
  outline: isDragging
104
104
  ? `1px dashed ${theme.palette.divider}`
105
- : currentHoveredColumn?.id === column.id
105
+ : hoveredColumn?.id === column.id
106
106
  ? `2px dashed ${theme.palette.primary.main}`
107
107
  : 'none',
108
108
  pl: `${(column.depth + 0.5) * 2}rem`,
@@ -178,10 +178,10 @@ export const MRT_ShowHideColumnsMenuItems = <
178
178
  <MRT_ShowHideColumnsMenuItems
179
179
  allColumns={allColumns}
180
180
  column={c}
181
- currentHoveredColumn={currentHoveredColumn}
181
+ hoveredColumn={hoveredColumn}
182
182
  isSubMenu={isSubMenu}
183
183
  key={`${i}-${c.id}`}
184
- setCurrentHoveredColumn={setCurrentHoveredColumn}
184
+ setHoveredColumn={setHoveredColumn}
185
185
  table={table}
186
186
  />
187
187
  ))}
@@ -48,31 +48,57 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
48
48
  const initState = props.initialState ?? {};
49
49
  initState.columnOrder =
50
50
  initState.columnOrder ?? getDefaultColumnOrderIds(props);
51
+ initState.columnFilterFns = Object.assign(
52
+ {},
53
+ ...getAllLeafColumnDefs(props.columns as MRT_ColumnDef<TData>[]).map(
54
+ (col) => ({
55
+ [col.id?.toString() ?? col.accessorKey?.toString() ?? '']:
56
+ col.filterFn instanceof Function
57
+ ? col.filterFn.name ?? 'custom'
58
+ : col.filterFn ??
59
+ initialState?.columnFilterFns?.[
60
+ col.id?.toString() ?? col.accessorKey?.toString() ?? ''
61
+ ] ??
62
+ getDefaultColumnFilterFn(col),
63
+ }),
64
+ ),
65
+ );
66
+ initState.globalFilterFn =
67
+ props.globalFilterFn instanceof String
68
+ ? (props.globalFilterFn as MRT_FilterOption)
69
+ : 'fuzzy';
51
70
  return initState;
52
71
  }, []);
53
72
 
73
+ const [columnFilterFns, setColumnFilterFns] = useState<{
74
+ [key: string]: MRT_FilterOption;
75
+ }>(initialState.columnFilterFns ?? {});
54
76
  const [columnOrder, setColumnOrder] = useState(
55
77
  initialState.columnOrder ?? [],
56
78
  );
57
- const [currentDraggingColumn, setCurrentDraggingColumn] =
58
- useState<MRT_Column<TData> | null>(
59
- initialState.currentDraggingColumn ?? null,
60
- );
61
- const [currentDraggingRow, setCurrentDraggingRow] =
62
- useState<MRT_Row<TData> | null>(initialState.currentDraggingRow ?? null);
63
- const [currentEditingCell, setCurrentEditingCell] =
64
- useState<MRT_Cell<TData> | null>(initialState.currentEditingCell ?? null);
65
- const [currentEditingRow, setCurrentEditingRow] =
66
- useState<MRT_Row<TData> | null>(initialState.currentEditingRow ?? null);
67
- const [currentHoveredColumn, setCurrentHoveredColumn] = useState<
68
- MRT_Column<TData> | { id: string } | null
69
- >(initialState.currentHoveredColumn ?? null);
70
- const [currentHoveredRow, setCurrentHoveredRow] = useState<
71
- MRT_Row<TData> | { id: string } | null
72
- >(initialState.currentHoveredRow ?? null);
73
79
  const [density, setDensity] = useState(
74
80
  initialState?.density ?? 'comfortable',
75
81
  );
82
+ const [draggingColumn, setDraggingColumn] =
83
+ useState<MRT_Column<TData> | null>(initialState.draggingColumn ?? null);
84
+ const [draggingRow, setDraggingRow] = useState<MRT_Row<TData> | null>(
85
+ initialState.draggingRow ?? null,
86
+ );
87
+ const [editingCell, setEditingCell] = useState<MRT_Cell<TData> | null>(
88
+ initialState.editingCell ?? null,
89
+ );
90
+ const [editingRow, setEditingRow] = useState<MRT_Row<TData> | null>(
91
+ initialState.editingRow ?? null,
92
+ );
93
+ const [globalFilterFn, setGlobalFilterFn] = useState<MRT_FilterOption>(
94
+ initialState.globalFilterFn ?? 'fuzzy',
95
+ );
96
+ const [hoveredColumn, setHoveredColumn] = useState<
97
+ MRT_Column<TData> | { id: string } | null
98
+ >(initialState.hoveredColumn ?? null);
99
+ const [hoveredRow, setHoveredRow] = useState<
100
+ MRT_Row<TData> | { id: string } | null
101
+ >(initialState.hoveredRow ?? null);
76
102
  const [isFullScreen, setIsFullScreen] = useState(
77
103
  initialState?.isFullScreen ?? false,
78
104
  );
@@ -85,30 +111,6 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
85
111
  const [showGlobalFilter, setShowGlobalFilter] = useState(
86
112
  initialState?.showGlobalFilter ?? false,
87
113
  );
88
- const [columnFilterFns, setColumnFilterFns] = useState<{
89
- [key: string]: MRT_FilterOption;
90
- }>(() =>
91
- Object.assign(
92
- {},
93
- ...getAllLeafColumnDefs(props.columns as MRT_ColumnDef<TData>[]).map(
94
- (col) => ({
95
- [col.id?.toString() ?? col.accessorKey?.toString() ?? '']:
96
- col.filterFn instanceof Function
97
- ? col.filterFn.name ?? 'custom'
98
- : col.filterFn ??
99
- initialState?.columnFilterFns?.[
100
- col.id?.toString() ?? col.accessorKey?.toString() ?? ''
101
- ] ??
102
- getDefaultColumnFilterFn(col),
103
- }),
104
- ),
105
- ),
106
- );
107
- const [globalFilterFn, setGlobalFilterFn] = useState<MRT_FilterOption>(
108
- props.globalFilterFn instanceof String
109
- ? (props.globalFilterFn as MRT_FilterOption)
110
- : 'fuzzy',
111
- );
112
114
 
113
115
  const displayColumns = useMemo(
114
116
  () =>
@@ -190,8 +192,6 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
190
192
  props.enableRowSelection,
191
193
  props.enableSelectAll,
192
194
  props.localization,
193
- props.muiTableBodyCellProps,
194
- props.muiTableHeadCellProps,
195
195
  props.positionActionsColumn,
196
196
  ],
197
197
  );
@@ -248,20 +248,19 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
248
248
  columns: columnDefs,
249
249
  data,
250
250
  globalFilterFn:
251
- //@ts-ignore
252
- props.filterFns[globalFilterFn] ?? props.filterFns.fuzzy,
251
+ props.filterFns?.[globalFilterFn] ?? props.filterFns?.fuzzy,
253
252
  initialState,
254
253
  state: {
255
- columnOrder,
256
- currentDraggingColumn,
257
- currentDraggingRow,
258
- currentEditingCell,
259
- currentEditingRow,
260
254
  columnFilterFns,
261
- globalFilterFn,
262
- currentHoveredColumn,
263
- currentHoveredRow,
255
+ columnOrder,
264
256
  density,
257
+ draggingColumn,
258
+ draggingRow,
259
+ editingCell,
260
+ editingRow,
261
+ globalFilterFn,
262
+ hoveredColumn,
263
+ hoveredRow,
265
264
  isFullScreen,
266
265
  showAlertBanner,
267
266
  showColumnFilters,
@@ -270,21 +269,15 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
270
269
  } as TableState,
271
270
  tableId,
272
271
  }),
273
- setCurrentDraggingColumn:
274
- props.onCurrentDraggingColumnChange ?? setCurrentDraggingColumn,
275
- setCurrentDraggingRow:
276
- props.onCurrentDraggingRowChange ?? setCurrentDraggingRow,
277
- setCurrentEditingCell:
278
- props.onCurrentEditingCellChange ?? setCurrentEditingCell,
279
- setCurrentEditingRow:
280
- props.onCurrentEditingRowChange ?? setCurrentEditingRow,
281
- setColumnFilterFns: props.onCurrentFilterFnsChange ?? setColumnFilterFns,
282
- setGlobalFilterFn: props.onCurrentGlobalFilterFnChange ?? setGlobalFilterFn,
283
- setCurrentHoveredColumn:
284
- props.onCurrentHoveredColumnChange ?? setCurrentHoveredColumn,
285
- setCurrentHoveredRow:
286
- props.onCurrentHoveredRowChange ?? setCurrentHoveredRow,
272
+ setColumnFilterFns: props.onFilterFnsChange ?? setColumnFilterFns,
287
273
  setDensity: props.onDensityChange ?? setDensity,
274
+ setDraggingColumn: props.onDraggingColumnChange ?? setDraggingColumn,
275
+ setDraggingRow: props.onDraggingRowChange ?? setDraggingRow,
276
+ setEditingCell: props.onEditingCellChange ?? setEditingCell,
277
+ setEditingRow: props.onEditingRowChange ?? setEditingRow,
278
+ setGlobalFilterFn: props.onGlobalFilterFnChange ?? setGlobalFilterFn,
279
+ setHoveredColumn: props.onHoveredColumnChange ?? setHoveredColumn,
280
+ setHoveredRow: props.onHoveredRowChange ?? setHoveredRow,
288
281
  setIsFullScreen: props.onIsFullScreenChange ?? setIsFullScreen,
289
282
  setShowAlertBanner: props.onShowAlertBannerChange ?? setShowAlertBanner,
290
283
  setShowFilters: props.onShowFiltersChange ?? setShowFilters,
@@ -10,27 +10,23 @@ export const MRT_ToolbarDropZone: FC<Props> = ({ table }) => {
10
10
  const {
11
11
  getState,
12
12
  options: { enableGrouping, localization },
13
- setCurrentHoveredColumn,
13
+ setHoveredColumn,
14
14
  } = table;
15
15
 
16
- const { currentDraggingColumn, currentHoveredColumn } = getState();
16
+ const { draggingColumn, hoveredColumn } = getState();
17
17
 
18
18
  const handleDragEnter = (_event: DragEvent<HTMLDivElement>) => {
19
- setCurrentHoveredColumn({ id: 'drop-zone' });
19
+ setHoveredColumn({ id: 'drop-zone' });
20
20
  };
21
21
 
22
22
  return (
23
- <Fade
24
- unmountOnExit
25
- mountOnEnter
26
- in={!!enableGrouping && !!currentDraggingColumn}
27
- >
23
+ <Fade unmountOnExit mountOnEnter in={!!enableGrouping && !!draggingColumn}>
28
24
  <Box
29
25
  sx={(theme) => ({
30
26
  alignItems: 'center',
31
27
  backgroundColor: alpha(
32
28
  theme.palette.info.main,
33
- currentHoveredColumn?.id === 'drop-zone' ? 0.2 : 0.1,
29
+ hoveredColumn?.id === 'drop-zone' ? 0.2 : 0.1,
34
30
  ),
35
31
  border: `dashed ${theme.palette.info.main} 2px`,
36
32
  display: 'flex',
@@ -45,7 +41,7 @@ export const MRT_ToolbarDropZone: FC<Props> = ({ table }) => {
45
41
  <Typography>
46
42
  {localization.dropToGroupBy.replace(
47
43
  '{column}',
48
- currentDraggingColumn?.columnDef?.header ?? '',
44
+ draggingColumn?.columnDef?.header ?? '',
49
45
  )}
50
46
  </Typography>
51
47
  </Box>