material-react-table 0.34.0 → 0.35.1

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.
@@ -22,12 +22,11 @@ export const MRT_TableHeadCellGrabHandle: FC<Props> = ({
22
22
  onColumnDrop,
23
23
  },
24
24
  setColumnOrder,
25
- setCurrentDraggingColumn,
26
- setCurrentHoveredColumn,
25
+ setDraggingColumn,
26
+ setHoveredColumn,
27
27
  } = table;
28
28
  const { columnDef } = column;
29
- const { currentHoveredColumn, currentDraggingColumn, columnOrder } =
30
- getState();
29
+ const { hoveredColumn, draggingColumn, columnOrder } = getState();
31
30
 
32
31
  const mIconButtonProps =
33
32
  muiTableHeadCellDragHandleProps instanceof Function
@@ -45,7 +44,7 @@ export const MRT_TableHeadCellGrabHandle: FC<Props> = ({
45
44
  };
46
45
 
47
46
  const handleDragStart = (e: DragEvent<HTMLButtonElement>) => {
48
- setCurrentDraggingColumn(column);
47
+ setDraggingColumn(column);
49
48
  e.dataTransfer.setDragImage(tableHeadCellRef.current as HTMLElement, 0, 0);
50
49
  };
51
50
 
@@ -53,21 +52,21 @@ export const MRT_TableHeadCellGrabHandle: FC<Props> = ({
53
52
  onColumnDrop?.({
54
53
  event,
55
54
  draggedColumn: column,
56
- targetColumn: currentHoveredColumn,
55
+ targetColumn: hoveredColumn,
57
56
  });
58
- if (currentHoveredColumn?.id === 'drop-zone') {
57
+ if (hoveredColumn?.id === 'drop-zone') {
59
58
  column.toggleGrouping();
60
59
  } else if (
61
60
  enableColumnOrdering &&
62
- currentHoveredColumn &&
63
- currentHoveredColumn?.id !== currentDraggingColumn?.id
61
+ hoveredColumn &&
62
+ hoveredColumn?.id !== draggingColumn?.id
64
63
  ) {
65
64
  setColumnOrder(
66
- reorderColumn(column, currentHoveredColumn as MRT_Column, columnOrder),
65
+ reorderColumn(column, hoveredColumn as MRT_Column, columnOrder),
67
66
  );
68
67
  }
69
- setCurrentDraggingColumn(null);
70
- setCurrentHoveredColumn(null);
68
+ setDraggingColumn(null);
69
+ setHoveredColumn(null);
71
70
  };
72
71
 
73
72
  return (
@@ -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,43 +48,13 @@ 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.globalFilterFn =
52
+ props.globalFilterFn instanceof String
53
+ ? (props.globalFilterFn as MRT_FilterOption)
54
+ : 'fuzzy';
51
55
  return initState;
52
56
  }, []);
53
57
 
54
- const [columnOrder, setColumnOrder] = useState(
55
- initialState.columnOrder ?? [],
56
- );
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
- const [density, setDensity] = useState(
74
- initialState?.density ?? 'comfortable',
75
- );
76
- const [isFullScreen, setIsFullScreen] = useState(
77
- initialState?.isFullScreen ?? false,
78
- );
79
- const [showAlertBanner, setShowAlertBanner] = useState(
80
- props.initialState?.showAlertBanner ?? false,
81
- );
82
- const [showColumnFilters, setShowFilters] = useState(
83
- initialState?.showColumnFilters ?? false,
84
- );
85
- const [showGlobalFilter, setShowGlobalFilter] = useState(
86
- initialState?.showGlobalFilter ?? false,
87
- );
88
58
  const [columnFilterFns, setColumnFilterFns] = useState<{
89
59
  [key: string]: MRT_FilterOption;
90
60
  }>(() =>
@@ -104,10 +74,43 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
104
74
  ),
105
75
  ),
106
76
  );
77
+ const [columnOrder, setColumnOrder] = useState(
78
+ initialState.columnOrder ?? [],
79
+ );
80
+ const [density, setDensity] = useState(
81
+ initialState?.density ?? 'comfortable',
82
+ );
83
+ const [draggingColumn, setDraggingColumn] =
84
+ useState<MRT_Column<TData> | null>(initialState.draggingColumn ?? null);
85
+ const [draggingRow, setDraggingRow] = useState<MRT_Row<TData> | null>(
86
+ initialState.draggingRow ?? null,
87
+ );
88
+ const [editingCell, setEditingCell] = useState<MRT_Cell<TData> | null>(
89
+ initialState.editingCell ?? null,
90
+ );
91
+ const [editingRow, setEditingRow] = useState<MRT_Row<TData> | null>(
92
+ initialState.editingRow ?? null,
93
+ );
107
94
  const [globalFilterFn, setGlobalFilterFn] = useState<MRT_FilterOption>(
108
- props.globalFilterFn instanceof String
109
- ? (props.globalFilterFn as MRT_FilterOption)
110
- : 'fuzzy',
95
+ initialState.globalFilterFn ?? 'fuzzy',
96
+ );
97
+ const [hoveredColumn, setHoveredColumn] = useState<
98
+ MRT_Column<TData> | { id: string } | null
99
+ >(initialState.hoveredColumn ?? null);
100
+ const [hoveredRow, setHoveredRow] = useState<
101
+ MRT_Row<TData> | { id: string } | null
102
+ >(initialState.hoveredRow ?? null);
103
+ const [isFullScreen, setIsFullScreen] = useState(
104
+ initialState?.isFullScreen ?? false,
105
+ );
106
+ const [showAlertBanner, setShowAlertBanner] = useState(
107
+ props.initialState?.showAlertBanner ?? false,
108
+ );
109
+ const [showColumnFilters, setShowFilters] = useState(
110
+ initialState?.showColumnFilters ?? false,
111
+ );
112
+ const [showGlobalFilter, setShowGlobalFilter] = useState(
113
+ initialState?.showGlobalFilter ?? false,
111
114
  );
112
115
 
113
116
  const displayColumns = useMemo(
@@ -190,8 +193,6 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
190
193
  props.enableRowSelection,
191
194
  props.enableSelectAll,
192
195
  props.localization,
193
- props.muiTableBodyCellProps,
194
- props.muiTableHeadCellProps,
195
196
  props.positionActionsColumn,
196
197
  ],
197
198
  );
@@ -248,20 +249,19 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
248
249
  columns: columnDefs,
249
250
  data,
250
251
  globalFilterFn:
251
- //@ts-ignore
252
- props.filterFns[globalFilterFn] ?? props.filterFns.fuzzy,
252
+ props.filterFns?.[globalFilterFn] ?? props.filterFns?.fuzzy,
253
253
  initialState,
254
254
  state: {
255
- columnOrder,
256
- currentDraggingColumn,
257
- currentDraggingRow,
258
- currentEditingCell,
259
- currentEditingRow,
260
255
  columnFilterFns,
261
- globalFilterFn,
262
- currentHoveredColumn,
263
- currentHoveredRow,
256
+ columnOrder,
264
257
  density,
258
+ draggingColumn,
259
+ draggingRow,
260
+ editingCell,
261
+ editingRow,
262
+ globalFilterFn,
263
+ hoveredColumn,
264
+ hoveredRow,
265
265
  isFullScreen,
266
266
  showAlertBanner,
267
267
  showColumnFilters,
@@ -270,21 +270,15 @@ export const MRT_TableRoot = <TData extends Record<string, any> = {}>(
270
270
  } as TableState,
271
271
  tableId,
272
272
  }),
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,
273
+ setColumnFilterFns: props.onFilterFnsChange ?? setColumnFilterFns,
287
274
  setDensity: props.onDensityChange ?? setDensity,
275
+ setDraggingColumn: props.onDraggingColumnChange ?? setDraggingColumn,
276
+ setDraggingRow: props.onDraggingRowChange ?? setDraggingRow,
277
+ setEditingCell: props.onEditingCellChange ?? setEditingCell,
278
+ setEditingRow: props.onEditingRowChange ?? setEditingRow,
279
+ setGlobalFilterFn: props.onGlobalFilterFnChange ?? setGlobalFilterFn,
280
+ setHoveredColumn: props.onHoveredColumnChange ?? setHoveredColumn,
281
+ setHoveredRow: props.onHoveredRowChange ?? setHoveredRow,
288
282
  setIsFullScreen: props.onIsFullScreenChange ?? setIsFullScreen,
289
283
  setShowAlertBanner: props.onShowAlertBannerChange ?? setShowAlertBanner,
290
284
  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>