@lminii/material-react-table 4.0.0 → 4.0.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.
@@ -1,227 +1,227 @@
1
- ---
2
- name: drag-and-drop-ordering
3
- description: >
4
- Reorder columns and rows by drag and drop in Material React Table V4: enableColumnOrdering with the columnOrder state and mrt-row-* display column ids, enableRowOrdering with muiRowDragHandleProps onDragEnd reading draggingRow and hoveredRow, enableRowDragging for dropping rows onto other tables or UI, enableColumnDragging for column drags without reordering, and the draggingColumn, hoveredColumn, draggingRow, hoveredRow state. Load for user-arranged columns, sortable lists, moving rows between tables, or a drop that never fires.
5
- metadata:
6
- type: framework
7
- library: '@lminii/material-react-table'
8
- library_version: '4.0.0'
9
- framework: react
10
- requires:
11
- - getting-started
12
- - state-and-server-data
13
- - '@tanstack/table-core#column-ordering'
14
- sources:
15
- - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/column-ordering-dnd.mdx'
16
- - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/row-ordering-dnd.mdx'
17
- - 'lminii/material-react-table:apps/material-react-table-docs/examples/enable-row-ordering/sandbox/src/TS.tsx'
18
- - 'lminii/material-react-table:apps/material-react-table-docs/examples/enable-row-dragging/sandbox/src/TS.tsx'
19
- - 'lminii/material-react-table:packages/material-react-table/src/components/body/MRT_TableBodyRowGrabHandle.tsx'
20
- - 'lminii/material-react-table:packages/material-react-table/src/utils/column.utils.ts'
21
- ---
22
-
23
- This skill builds on `getting-started`, `state-and-server-data`, and `@tanstack/table-core#column-ordering`. MRT uses native HTML drag events with a grab handle button. Column ordering is fully built in; row ordering exposes the drag state and leaves the reorder of `data` to you.
24
-
25
- ## Setup
26
-
27
- ```tsx
28
- const table = useMaterialReactTable({
29
- columns,
30
- data,
31
- enableColumnOrdering: true, // drag handles in every header
32
- enableRowOrdering: true, // adds the mrt-row-drag display column
33
- enableSorting: false, // manual order and sorting fight each other
34
- muiRowDragHandleProps: ({ table }) => ({
35
- onDragEnd: () => {
36
- const { draggingRow, hoveredRow } = table.getState()
37
- if (draggingRow && hoveredRow) {
38
- setData((old) => {
39
- const next = [...old]
40
- next.splice(hoveredRow.index!, 0, next.splice(draggingRow.index, 1)[0])
41
- return next
42
- })
43
- }
44
- },
45
- }),
46
- })
47
- ```
48
-
49
- Column-level `enableColumnOrdering: false` removes the handle from one column. `hoveredRow` and `hoveredColumn` are typed as partial objects because a drop target may be a placeholder; read `index` or `id` and guard for undefined.
50
-
51
- ## Core Patterns
52
-
53
- ### Set or persist the column order
54
-
55
- ```tsx
56
- const [columnOrder, setColumnOrder] = useState<MRT_ColumnOrderState>([
57
- 'mrt-row-select',
58
- 'name',
59
- 'email',
60
- 'mrt-row-actions',
61
- ])
62
-
63
- const table = useMaterialReactTable({
64
- columns,
65
- data,
66
- enableRowSelection: true,
67
- enableRowActions: true,
68
- enableColumnOrdering: true,
69
- state: { columnOrder },
70
- onColumnOrderChange: setColumnOrder,
71
- })
72
- ```
73
-
74
- `columnOrder` is an array of column ids (`accessorKey` or `id`). Include the display column ids you enable (`mrt-row-select`, `mrt-row-actions`, `mrt-row-expand`, `mrt-row-numbers`, `mrt-row-drag`, `mrt-row-pin`) at the position you want; missing ones are appended at the start or end. If the array length does not match the column count MRT regenerates it, so `initialState.columnOrder` is enough when you only need a default. A dropped column is also pinned to the target column's pin position.
75
-
76
- ### Move rows between tables
77
-
78
- ```tsx
79
- const [draggingRow, setDraggingRow] = useState<MRT_Row<Person> | null>(null)
80
- const [hoveredTable, setHoveredTable] = useState<string | null>(null)
81
-
82
- const common = {
83
- columns,
84
- enableRowDragging: true, // handle only; no in-table reorder
85
- onDraggingRowChange: setDraggingRow,
86
- state: { draggingRow },
87
- }
88
-
89
- const table1 = useMaterialReactTable({
90
- ...common,
91
- data: data1,
92
- getRowId: (row) => `t1-${row.id}`,
93
- muiRowDragHandleProps: {
94
- onDragEnd: () => {
95
- if (hoveredTable === 'table-2' && draggingRow) {
96
- setData2((rows) => [...rows, draggingRow.original])
97
- setData1((rows) => rows.filter((r) => r !== draggingRow.original))
98
- }
99
- setHoveredTable(null)
100
- },
101
- },
102
- muiTablePaperProps: {
103
- onDragEnter: () => setHoveredTable('table-1'),
104
- sx: { outline: hoveredTable === 'table-1' ? '2px dashed' : undefined },
105
- },
106
- })
107
- ```
108
-
109
- `enableRowDragging` shows the handle and maintains `draggingRow` without the in-table hover targets that `enableRowOrdering` adds. Any element can be a drop target through its own `onDragEnter`. Share `draggingRow` across tables by controlling it. Use `enableColumnDragging` the same way for columns that should be draggable onto external targets, for example a group-by drop zone.
110
-
111
- ### Style the drag interaction
112
-
113
- ```tsx
114
- muiRowDragHandleProps: ({ row }) => ({
115
- 'aria-label': `Move ${row.original.name}`,
116
- sx: { cursor: 'grab' },
117
- }),
118
- muiTableBodyRowProps: ({ row, table }) => ({
119
- sx: {
120
- opacity: table.getState().draggingRow?.id === row.id ? 0.5 : 1,
121
- outline: table.getState().hoveredRow?.id === row.id ? '2px dashed' : undefined,
122
- },
123
- }),
124
- ```
125
-
126
- `muiRowDragHandleProps` takes `IconButtonProps`; your `onDragStart` and `onDragEnd` run before MRT sets or clears `draggingRow`. `muiColumnDragHandleProps` is the column counterpart, and `hoveredColumn` / `draggingColumn` drive header styling. Replace the handle icon through `icons.DragHandleIcon`.
127
-
128
- ### Combine with virtualization
129
-
130
- ```tsx
131
- enableRowOrdering: true,
132
- enableRowVirtualization: true,
133
- enablePagination: false,
134
- ```
135
-
136
- The row virtualizer keeps the dragging row mounted while it is scrolled out of view, so drops still resolve. Keep `autoResetPageIndex: false` when paginated so a drop does not jump back to page one.
137
-
138
- ## Common Mistakes
139
-
140
- ### HIGH Reordering with the wrong row index
141
-
142
- Wrong:
143
-
144
- ```tsx
145
- onDragEnd: () => {
146
- const { draggingRow, hoveredRow } = table.getState()
147
- data.splice(hoveredRow.index, 0, data.splice(draggingRow.index, 1)[0])
148
- setData(data) // same array reference, no re-render
149
- },
150
- ```
151
-
152
- Correct:
153
-
154
- ```tsx
155
- onDragEnd: () => {
156
- const { draggingRow, hoveredRow } = table.getState()
157
- if (!draggingRow || hoveredRow?.index === undefined) return
158
- setData((old) => {
159
- const next = [...old]
160
- next.splice(hoveredRow.index!, 0, next.splice(draggingRow.index, 1)[0])
161
- return next
162
- })
163
- },
164
- ```
165
-
166
- `row.index` is the index in the original `data` array, so it is only meaningful when no client-side sort or filter is applied; and a mutated array with the same reference does not trigger a render.
167
-
168
- Source: `docs/examples/enable-row-ordering`
169
-
170
- ### HIGH Expecting a drop callback
171
-
172
- Wrong:
173
-
174
- ```tsx
175
- onHoveredRowChange: (row) => moveRow(row), // fires on every hover, not on drop
176
- ```
177
-
178
- Correct:
179
-
180
- ```tsx
181
- muiRowDragHandleProps: { onDragEnd: () => moveRow(table.getState().hoveredRow) },
182
- ```
183
-
184
- There is no `onRowDrop`; the drop point is the handle's `onDragEnd`, after which MRT clears `draggingRow` and `hoveredRow`.
185
-
186
- Source: `packages/material-react-table/src/components/body/MRT_TableBodyRowGrabHandle.tsx`
187
-
188
- ### MEDIUM Leaving sorting on with row ordering
189
-
190
- Wrong:
191
-
192
- ```tsx
193
- enableRowOrdering: true,
194
- ```
195
-
196
- Correct:
197
-
198
- ```tsx
199
- enableRowOrdering: true,
200
- enableSorting: false,
201
- ```
202
-
203
- A sorted view re-sorts after every drop, so the user's arrangement disappears and `row.index` no longer maps to the visible position.
204
-
205
- Source: `docs/guides/row-ordering-dnd.mdx`
206
-
207
- ### MEDIUM Column order array that ignores display columns
208
-
209
- Wrong:
210
-
211
- ```tsx
212
- initialState: { columnOrder: ['name', 'email'] }, // with enableRowSelection: true
213
- ```
214
-
215
- Correct:
216
-
217
- ```tsx
218
- initialState: { columnOrder: ['mrt-row-select', 'name', 'email'] },
219
- ```
220
-
221
- The length mismatch makes MRT regenerate the order from the column definitions, discarding the custom arrangement.
222
-
223
- Source: `docs/guides/column-ordering-dnd.mdx`
224
-
225
- ## API Discovery
226
-
227
- Search `node_modules/@lminii/material-react-table/dist/index.d.ts` for `enableRowOrdering`, `enableRowDragging`, `muiRowDragHandleProps`, `muiColumnDragHandleProps`, `draggingRow`, and `hoveredColumn`. The Column Ordering and Row Ordering guides at `/docs/guides/column-ordering-dnd` and `/docs/guides/row-ordering-dnd` list the related options, and `/docs/examples/enable-column-ordering`, `enable-row-ordering`, and `enable-row-dragging` are runnable examples.
1
+ ---
2
+ name: drag-and-drop-ordering
3
+ description: >
4
+ Reorder columns and rows by drag and drop in Material React Table V4: enableColumnOrdering with the columnOrder state and mrt-row-* display column ids, enableRowOrdering with muiRowDragHandleProps onDragEnd reading draggingRow and hoveredRow, enableRowDragging for dropping rows onto other tables or UI, enableColumnDragging for column drags without reordering, and the draggingColumn, hoveredColumn, draggingRow, hoveredRow state. Load for user-arranged columns, sortable lists, moving rows between tables, or a drop that never fires.
5
+ metadata:
6
+ type: framework
7
+ library: '@lminii/material-react-table'
8
+ library_version: '4.0.0'
9
+ framework: react
10
+ requires:
11
+ - getting-started
12
+ - state-and-server-data
13
+ - '@tanstack/table-core#column-ordering'
14
+ sources:
15
+ - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/column-ordering-dnd.mdx'
16
+ - 'lminii/material-react-table:apps/material-react-table-docs/pages/docs/guides/row-ordering-dnd.mdx'
17
+ - 'lminii/material-react-table:apps/material-react-table-docs/examples/enable-row-ordering/sandbox/src/TS.tsx'
18
+ - 'lminii/material-react-table:apps/material-react-table-docs/examples/enable-row-dragging/sandbox/src/TS.tsx'
19
+ - 'lminii/material-react-table:packages/material-react-table/src/components/body/MRT_TableBodyRowGrabHandle.tsx'
20
+ - 'lminii/material-react-table:packages/material-react-table/src/utils/column.utils.ts'
21
+ ---
22
+
23
+ This skill builds on `getting-started`, `state-and-server-data`, and `@tanstack/table-core#column-ordering`. MRT uses native HTML drag events with a grab handle button. Column ordering is fully built in; row ordering exposes the drag state and leaves the reorder of `data` to you.
24
+
25
+ ## Setup
26
+
27
+ ```tsx
28
+ const table = useMaterialReactTable({
29
+ columns,
30
+ data,
31
+ enableColumnOrdering: true, // drag handles in every header
32
+ enableRowOrdering: true, // adds the mrt-row-drag display column
33
+ enableSorting: false, // manual order and sorting fight each other
34
+ muiRowDragHandleProps: ({ table }) => ({
35
+ onDragEnd: () => {
36
+ const { draggingRow, hoveredRow } = table.getState()
37
+ if (draggingRow && hoveredRow) {
38
+ setData((old) => {
39
+ const next = [...old]
40
+ next.splice(hoveredRow.index!, 0, next.splice(draggingRow.index, 1)[0])
41
+ return next
42
+ })
43
+ }
44
+ },
45
+ }),
46
+ })
47
+ ```
48
+
49
+ Column-level `enableColumnOrdering: false` removes the handle from one column. `hoveredRow` and `hoveredColumn` are typed as partial objects because a drop target may be a placeholder; read `index` or `id` and guard for undefined.
50
+
51
+ ## Core Patterns
52
+
53
+ ### Set or persist the column order
54
+
55
+ ```tsx
56
+ const [columnOrder, setColumnOrder] = useState<MRT_ColumnOrderState>([
57
+ 'mrt-row-select',
58
+ 'name',
59
+ 'email',
60
+ 'mrt-row-actions',
61
+ ])
62
+
63
+ const table = useMaterialReactTable({
64
+ columns,
65
+ data,
66
+ enableRowSelection: true,
67
+ enableRowActions: true,
68
+ enableColumnOrdering: true,
69
+ state: { columnOrder },
70
+ onColumnOrderChange: setColumnOrder,
71
+ })
72
+ ```
73
+
74
+ `columnOrder` is an array of column ids (`accessorKey` or `id`). Include the display column ids you enable (`mrt-row-select`, `mrt-row-actions`, `mrt-row-expand`, `mrt-row-numbers`, `mrt-row-drag`, `mrt-row-pin`) at the position you want; missing ones are appended at the start or end. If the array length does not match the column count MRT regenerates it, so `initialState.columnOrder` is enough when you only need a default. A dropped column is also pinned to the target column's pin position.
75
+
76
+ ### Move rows between tables
77
+
78
+ ```tsx
79
+ const [draggingRow, setDraggingRow] = useState<MRT_Row<Person> | null>(null)
80
+ const [hoveredTable, setHoveredTable] = useState<string | null>(null)
81
+
82
+ const common = {
83
+ columns,
84
+ enableRowDragging: true, // handle only; no in-table reorder
85
+ onDraggingRowChange: setDraggingRow,
86
+ state: { draggingRow },
87
+ }
88
+
89
+ const table1 = useMaterialReactTable({
90
+ ...common,
91
+ data: data1,
92
+ getRowId: (row) => `t1-${row.id}`,
93
+ muiRowDragHandleProps: {
94
+ onDragEnd: () => {
95
+ if (hoveredTable === 'table-2' && draggingRow) {
96
+ setData2((rows) => [...rows, draggingRow.original])
97
+ setData1((rows) => rows.filter((r) => r !== draggingRow.original))
98
+ }
99
+ setHoveredTable(null)
100
+ },
101
+ },
102
+ muiTablePaperProps: {
103
+ onDragEnter: () => setHoveredTable('table-1'),
104
+ sx: { outline: hoveredTable === 'table-1' ? '2px dashed' : undefined },
105
+ },
106
+ })
107
+ ```
108
+
109
+ `enableRowDragging` shows the handle and maintains `draggingRow` without the in-table hover targets that `enableRowOrdering` adds. Any element can be a drop target through its own `onDragEnter`. Share `draggingRow` across tables by controlling it. Use `enableColumnDragging` the same way for columns that should be draggable onto external targets, for example a group-by drop zone.
110
+
111
+ ### Style the drag interaction
112
+
113
+ ```tsx
114
+ muiRowDragHandleProps: ({ row }) => ({
115
+ 'aria-label': `Move ${row.original.name}`,
116
+ sx: { cursor: 'grab' },
117
+ }),
118
+ muiTableBodyRowProps: ({ row, table }) => ({
119
+ sx: {
120
+ opacity: table.getState().draggingRow?.id === row.id ? 0.5 : 1,
121
+ outline: table.getState().hoveredRow?.id === row.id ? '2px dashed' : undefined,
122
+ },
123
+ }),
124
+ ```
125
+
126
+ `muiRowDragHandleProps` takes `IconButtonProps`; your `onDragStart` and `onDragEnd` run before MRT sets or clears `draggingRow`. `muiColumnDragHandleProps` is the column counterpart, and `hoveredColumn` / `draggingColumn` drive header styling. Replace the handle icon through `icons.DragHandleIcon`.
127
+
128
+ ### Combine with virtualization
129
+
130
+ ```tsx
131
+ enableRowOrdering: true,
132
+ enableRowVirtualization: true,
133
+ enablePagination: false,
134
+ ```
135
+
136
+ The row virtualizer keeps the dragging row mounted while it is scrolled out of view, so drops still resolve. Keep `autoResetPageIndex: false` when paginated so a drop does not jump back to page one.
137
+
138
+ ## Common Mistakes
139
+
140
+ ### HIGH Reordering with the wrong row index
141
+
142
+ Wrong:
143
+
144
+ ```tsx
145
+ onDragEnd: () => {
146
+ const { draggingRow, hoveredRow } = table.getState()
147
+ data.splice(hoveredRow.index, 0, data.splice(draggingRow.index, 1)[0])
148
+ setData(data) // same array reference, no re-render
149
+ },
150
+ ```
151
+
152
+ Correct:
153
+
154
+ ```tsx
155
+ onDragEnd: () => {
156
+ const { draggingRow, hoveredRow } = table.getState()
157
+ if (!draggingRow || hoveredRow?.index === undefined) return
158
+ setData((old) => {
159
+ const next = [...old]
160
+ next.splice(hoveredRow.index!, 0, next.splice(draggingRow.index, 1)[0])
161
+ return next
162
+ })
163
+ },
164
+ ```
165
+
166
+ `row.index` is the index in the original `data` array, so it is only meaningful when no client-side sort or filter is applied; and a mutated array with the same reference does not trigger a render.
167
+
168
+ Source: `docs/examples/enable-row-ordering`
169
+
170
+ ### HIGH Expecting a drop callback
171
+
172
+ Wrong:
173
+
174
+ ```tsx
175
+ onHoveredRowChange: (row) => moveRow(row), // fires on every hover, not on drop
176
+ ```
177
+
178
+ Correct:
179
+
180
+ ```tsx
181
+ muiRowDragHandleProps: { onDragEnd: () => moveRow(table.getState().hoveredRow) },
182
+ ```
183
+
184
+ There is no `onRowDrop`; the drop point is the handle's `onDragEnd`, after which MRT clears `draggingRow` and `hoveredRow`.
185
+
186
+ Source: `packages/material-react-table/src/components/body/MRT_TableBodyRowGrabHandle.tsx`
187
+
188
+ ### MEDIUM Leaving sorting on with row ordering
189
+
190
+ Wrong:
191
+
192
+ ```tsx
193
+ enableRowOrdering: true,
194
+ ```
195
+
196
+ Correct:
197
+
198
+ ```tsx
199
+ enableRowOrdering: true,
200
+ enableSorting: false,
201
+ ```
202
+
203
+ A sorted view re-sorts after every drop, so the user's arrangement disappears and `row.index` no longer maps to the visible position.
204
+
205
+ Source: `docs/guides/row-ordering-dnd.mdx`
206
+
207
+ ### MEDIUM Column order array that ignores display columns
208
+
209
+ Wrong:
210
+
211
+ ```tsx
212
+ initialState: { columnOrder: ['name', 'email'] }, // with enableRowSelection: true
213
+ ```
214
+
215
+ Correct:
216
+
217
+ ```tsx
218
+ initialState: { columnOrder: ['mrt-row-select', 'name', 'email'] },
219
+ ```
220
+
221
+ The length mismatch makes MRT regenerate the order from the column definitions, discarding the custom arrangement.
222
+
223
+ Source: `docs/guides/column-ordering-dnd.mdx`
224
+
225
+ ## API Discovery
226
+
227
+ Search `node_modules/@lminii/material-react-table/dist/index.d.ts` for `enableRowOrdering`, `enableRowDragging`, `muiRowDragHandleProps`, `muiColumnDragHandleProps`, `draggingRow`, and `hoveredColumn`. The Column Ordering and Row Ordering guides at `/docs/guides/column-ordering-dnd` and `/docs/guides/row-ordering-dnd` list the related options, and `/docs/examples/enable-column-ordering`, `enable-row-ordering`, and `enable-row-dragging` are runnable examples.