material-react-table 0.19.0 → 0.22.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.
@@ -1,5 +1,10 @@
1
- import React, { FC, Ref } from 'react';
2
- import { useDrag, useDrop } from 'react-dnd';
1
+ import React, {
2
+ Dispatch,
3
+ DragEvent,
4
+ FC,
5
+ SetStateAction,
6
+ useState,
7
+ } from 'react';
3
8
  import {
4
9
  Box,
5
10
  FormControlLabel,
@@ -16,12 +21,16 @@ import type { MRT_Column, MRT_TableInstance } from '..';
16
21
  interface Props {
17
22
  allColumns: MRT_Column[];
18
23
  column: MRT_Column;
24
+ currentHoveredColumn: MRT_Column | null;
19
25
  isSubMenu?: boolean;
26
+ setCurrentHoveredColumn: Dispatch<SetStateAction<MRT_Column | null>>;
20
27
  table: MRT_TableInstance;
21
28
  }
22
29
 
23
30
  export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
24
31
  allColumns,
32
+ currentHoveredColumn,
33
+ setCurrentHoveredColumn,
25
34
  column,
26
35
  isSubMenu,
27
36
  table,
@@ -40,22 +49,6 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
40
49
  const { columnDef } = column;
41
50
  const { columnDefType } = columnDef;
42
51
 
43
- const [, dropRef] = useDrop({
44
- accept: 'column',
45
- drop: (movingColumn: MRT_Column) => {
46
- const newColumnOrder = reorderColumn(movingColumn, column, columnOrder);
47
- setColumnOrder(newColumnOrder);
48
- },
49
- });
50
-
51
- const [, dragRef, previewRef] = useDrag({
52
- collect: (monitor) => ({
53
- isDragging: monitor.isDragging(),
54
- }),
55
- item: () => column,
56
- type: 'column',
57
- });
58
-
59
52
  const switchChecked =
60
53
  (columnDefType !== 'group' && column.getIsVisible()) ||
61
54
  (columnDefType === 'group' &&
@@ -71,41 +64,73 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
71
64
  }
72
65
  };
73
66
 
67
+ const menuItemRef = React.useRef<HTMLElement>(null);
68
+
69
+ const [isDragging, setIsDragging] = useState(false);
70
+
71
+ const handleDragStart = (e: DragEvent<HTMLButtonElement>) => {
72
+ setIsDragging(true);
73
+ e.dataTransfer.setDragImage(menuItemRef.current as HTMLElement, 0, 0);
74
+ };
75
+
76
+ const handleDragEnd = (_e: DragEvent<HTMLButtonElement>) => {
77
+ setIsDragging(false);
78
+ setCurrentHoveredColumn(null);
79
+ if (currentHoveredColumn) {
80
+ setColumnOrder(reorderColumn(column, currentHoveredColumn, columnOrder));
81
+ }
82
+ };
83
+
84
+ const handleDragEnter = (_e: DragEvent) => {
85
+ if (!isDragging) {
86
+ setCurrentHoveredColumn(column);
87
+ }
88
+ };
89
+
74
90
  return (
75
91
  <>
76
92
  <MenuItem
77
- ref={columnDefType === 'data' ? dropRef : undefined}
78
- sx={{
93
+ disableRipple={enableColumnOrdering}
94
+ ref={menuItemRef as any}
95
+ onDragEnter={handleDragEnter}
96
+ sx={(theme) => ({
79
97
  alignItems: 'center',
80
98
  justifyContent: 'flex-start',
81
99
  my: 0,
100
+ opacity: isDragging ? 0.5 : 1,
101
+ outline: isDragging
102
+ ? `1px dashed ${theme.palette.divider}`
103
+ : currentHoveredColumn?.id === column.id
104
+ ? `2px dashed ${theme.palette.primary.main}`
105
+ : 'none',
82
106
  pl: `${(column.depth + 0.5) * 2}rem`,
83
107
  py: '6px',
84
- }}
108
+ })}
85
109
  >
86
110
  <Box
87
- ref={previewRef}
88
111
  sx={{
89
112
  display: 'flex',
90
113
  flexWrap: 'nowrap',
91
114
  gap: '8px',
92
115
  }}
93
116
  >
94
- {columnDefType !== 'group' &&
117
+ {!isSubMenu &&
118
+ columnDefType !== 'group' &&
95
119
  enableColumnOrdering &&
96
120
  !allColumns.some(
97
121
  (col) => col.columnDef.columnDefType === 'group',
98
122
  ) &&
99
123
  (columnDef.enableColumnOrdering !== false ? (
100
124
  <MRT_GrabHandleButton
101
- ref={dragRef as Ref<HTMLButtonElement>}
125
+ handleDragEnd={handleDragEnd}
126
+ handleDragStart={handleDragStart}
102
127
  table={table}
103
128
  />
104
129
  ) : (
105
130
  <Box sx={{ width: '28px' }} />
106
131
  ))}
107
- {enablePinning &&
108
- !isSubMenu &&
132
+ {!isSubMenu &&
133
+ enablePinning &&
109
134
  (column.getCanPin() ? (
110
135
  <MRT_ColumnPinningButtons column={column} table={table} />
111
136
  ) : (
@@ -150,9 +175,11 @@ export const MRT_ShowHideColumnsMenuItems: FC<Props> = ({
150
175
  {column.columns?.map((c: MRT_Column, i) => (
151
176
  <MRT_ShowHideColumnsMenuItems
152
177
  allColumns={allColumns}
153
- key={`${i}-${c.id}`}
154
178
  column={c}
179
+ currentHoveredColumn={currentHoveredColumn}
155
180
  isSubMenu={isSubMenu}
181
+ key={`${i}-${c.id}`}
182
+ setCurrentHoveredColumn={setCurrentHoveredColumn}
156
183
  table={table}
157
184
  />
158
185
  ))}
@@ -1,9 +1,9 @@
1
- import React, { FC, RefObject } from 'react';
1
+ import React, { FC, RefObject, useState } from 'react';
2
2
  import { Table } from '@mui/material';
3
3
  import { MRT_TableHead } from '../head/MRT_TableHead';
4
4
  import { MRT_TableBody } from '../body/MRT_TableBody';
5
5
  import { MRT_TableFooter } from '../footer/MRT_TableFooter';
6
- import { MRT_TableInstance } from '..';
6
+ import { MRT_Column, MRT_TableInstance } from '..';
7
7
 
8
8
  interface Props {
9
9
  tableContainerRef: RefObject<HTMLDivElement>;
@@ -29,6 +29,9 @@ export const MRT_Table: FC<Props> = ({ tableContainerRef, table }) => {
29
29
  ? muiTableProps({ table })
30
30
  : muiTableProps;
31
31
 
32
+ const [currentHoveredColumn, setCurrentHoveredColumn] =
33
+ useState<MRT_Column | null>(null);
34
+
32
35
  return (
33
36
  <Table
34
37
  stickyHeader={
@@ -41,8 +44,18 @@ export const MRT_Table: FC<Props> = ({ tableContainerRef, table }) => {
41
44
  ...tableProps?.sx,
42
45
  }}
43
46
  >
44
- {enableTableHead && <MRT_TableHead table={table} />}
45
- <MRT_TableBody tableContainerRef={tableContainerRef} table={table} />
47
+ {enableTableHead && (
48
+ <MRT_TableHead
49
+ currentHoveredColumn={currentHoveredColumn}
50
+ setCurrentHoveredColumn={setCurrentHoveredColumn}
51
+ table={table}
52
+ />
53
+ )}
54
+ <MRT_TableBody
55
+ setCurrentHoveredColumn={setCurrentHoveredColumn}
56
+ tableContainerRef={tableContainerRef}
57
+ table={table}
58
+ />
46
59
  {enableTableFooter && <MRT_TableFooter table={table} />}
47
60
  </Table>
48
61
  );
@@ -49,8 +49,8 @@ export const MRT_TableContainer: FC<Props> = ({ table }) => {
49
49
 
50
50
  return (
51
51
  <TableContainer
52
- {...tableContainerProps}
53
52
  ref={tableContainerRef}
53
+ {...tableContainerProps}
54
54
  sx={{
55
55
  maxWidth: '100%',
56
56
  maxHeight:
@@ -1,7 +1,5 @@
1
1
  import React, { FC, useEffect } from 'react';
2
2
  import { Paper } from '@mui/material';
3
- import { DndProvider } from 'react-dnd';
4
- import { HTML5Backend } from 'react-dnd-html5-backend';
5
3
  import { MRT_ToolbarTop } from '../toolbar/MRT_ToolbarTop';
6
4
  import { MRT_ToolbarBottom } from '../toolbar/MRT_ToolbarBottom';
7
5
  import { MRT_TableContainer } from './MRT_TableContainer';
@@ -34,28 +32,26 @@ export const MRT_TablePaper: FC<Props> = ({ table }) => {
34
32
  : muiTablePaperProps;
35
33
 
36
34
  return (
37
- <DndProvider backend={HTML5Backend}>
38
- <Paper
39
- elevation={2}
40
- {...tablePaperProps}
41
- sx={{
42
- transition: 'all 0.2s ease-in-out',
43
- ...tablePaperProps?.sx,
44
- }}
45
- style={{
46
- ...tablePaperProps?.style,
47
- height: isFullScreen ? '100vh' : undefined,
48
- margin: isFullScreen ? '0' : undefined,
49
- maxHeight: isFullScreen ? '100vh' : undefined,
50
- maxWidth: isFullScreen ? '100vw' : undefined,
51
- padding: isFullScreen ? '0' : undefined,
52
- width: isFullScreen ? '100vw' : undefined,
53
- }}
54
- >
55
- {enableToolbarTop && <MRT_ToolbarTop table={table} />}
56
- <MRT_TableContainer table={table} />
57
- {enableToolbarBottom && <MRT_ToolbarBottom table={table} />}
58
- </Paper>
59
- </DndProvider>
35
+ <Paper
36
+ elevation={2}
37
+ {...tablePaperProps}
38
+ sx={{
39
+ transition: 'all 0.2s ease-in-out',
40
+ ...tablePaperProps?.sx,
41
+ }}
42
+ style={{
43
+ ...tablePaperProps?.style,
44
+ height: isFullScreen ? '100vh' : undefined,
45
+ margin: isFullScreen ? '0' : undefined,
46
+ maxHeight: isFullScreen ? '100vh' : undefined,
47
+ maxWidth: isFullScreen ? '100vw' : undefined,
48
+ padding: isFullScreen ? '0' : undefined,
49
+ width: isFullScreen ? '100vw' : undefined,
50
+ }}
51
+ >
52
+ {enableToolbarTop && <MRT_ToolbarTop table={table} />}
53
+ <MRT_TableContainer table={table} />
54
+ {enableToolbarBottom && <MRT_ToolbarBottom table={table} />}
55
+ </Paper>
60
56
  );
61
57
  };
@@ -1,8 +0,0 @@
1
- import { FC } from 'react';
2
- import type { MRT_Header, MRT_TableInstance } from '..';
3
- interface Props {
4
- header: MRT_Header;
5
- table: MRT_TableInstance;
6
- }
7
- export declare const MRT_DraggableTableHeadCell: FC<Props>;
8
- export {};
@@ -1,43 +0,0 @@
1
- import React, { FC } from 'react';
2
- import { useDrag, useDrop } from 'react-dnd';
3
- import { MRT_TableHeadCell } from './MRT_TableHeadCell';
4
- import type { MRT_Column, MRT_Header, MRT_TableInstance } from '..';
5
- import { reorderColumn } from '../utils';
6
-
7
- interface Props {
8
- header: MRT_Header;
9
- table: MRT_TableInstance;
10
- }
11
-
12
- export const MRT_DraggableTableHeadCell: FC<Props> = ({ header, table }) => {
13
- const { getState, setColumnOrder } = table;
14
- const { columnOrder } = getState();
15
- const { column } = header;
16
-
17
- const [, dropRef] = useDrop({
18
- accept: 'column',
19
- drop: (movingColumn: MRT_Column) => {
20
- const newColumnOrder = reorderColumn(movingColumn, column, columnOrder);
21
- setColumnOrder(newColumnOrder);
22
- },
23
- });
24
-
25
- const [{ isDragging }, dragRef, previewRef] = useDrag({
26
- collect: (monitor) => ({
27
- isDragging: monitor.isDragging(),
28
- }),
29
- item: () => column,
30
- type: 'column',
31
- });
32
-
33
- return (
34
- <MRT_TableHeadCell
35
- dragRef={dragRef}
36
- dropRef={dropRef}
37
- header={header}
38
- isDragging={isDragging}
39
- previewRef={previewRef}
40
- table={table}
41
- />
42
- );
43
- };