-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathColumnsReordering.tsx
More file actions
157 lines (140 loc) · 4.16 KB
/
ColumnsReordering.tsx
File metadata and controls
157 lines (140 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { useCallback, useMemo, useState } from 'react';
import { createFileRoute } from '@tanstack/react-router';
import { DataGrid, type Column, type ColumnWidths, type SortColumn } from '../../src';
import { useDirection } from '../directionContext';
export const Route = createFileRoute('/ColumnsReordering')({
component: ColumnsReordering
});
interface Row {
readonly id: number;
readonly task: string;
readonly complete: number;
readonly priority: string;
readonly issueType: string;
}
function createRows(): Row[] {
const rows: Row[] = [];
for (let i = 1; i < 500; i++) {
rows.push({
id: i,
task: `Task ${i}`,
complete: Math.min(100, Math.round(Math.random() * 110)),
priority: ['Critical', 'High', 'Medium', 'Low'][Math.round(Math.random() * 3)],
issueType: ['Bug', 'Improvement', 'Epic', 'Story'][Math.round(Math.random() * 3)]
});
}
return rows;
}
const columns: Column<Row>[] = [
{
key: 'id',
name: 'ID',
width: 80
},
{
key: 'task',
name: 'Title',
resizable: true,
sortable: true,
draggable: true
},
{
key: 'priority',
name: 'Priority',
resizable: true,
sortable: true,
draggable: true
},
{
key: 'issueType',
name: 'Issue Type',
resizable: true,
sortable: true,
draggable: true
},
{
key: 'complete',
name: '% Complete',
resizable: true,
sortable: true,
draggable: true
}
];
const initialColumnsOrder: readonly number[] = columns.map((_, index) => index);
function ColumnsReordering() {
const direction = useDirection();
const [rows] = useState(createRows);
const [columnsOrder, setColumnsOrder] = useState(initialColumnsOrder);
const [sortColumns, setSortColumns] = useState<readonly SortColumn[]>([]);
const onSortColumnsChange = useCallback((sortColumns: SortColumn[]) => {
setSortColumns(sortColumns.slice(-1));
}, []);
const [columnWidths, setColumnWidths] = useState((): ColumnWidths => new Map());
const reorderedColumns = useMemo(() => {
return columnsOrder.map((index) => columns[index]);
}, [columnsOrder]);
const sortedRows = useMemo((): readonly Row[] => {
if (sortColumns.length === 0) return rows;
const { columnKey, direction } = sortColumns[0];
let sortedRows: Row[] = [...rows];
switch (columnKey) {
case 'task':
case 'priority':
case 'issueType':
sortedRows = sortedRows.sort((a, b) => a[columnKey].localeCompare(b[columnKey]));
break;
case 'complete':
sortedRows = sortedRows.sort((a, b) => a[columnKey] - b[columnKey]);
break;
default:
}
return direction === 'DESC' ? sortedRows.reverse() : sortedRows;
}, [rows, sortColumns]);
function onColumnsReorder(sourceKey: string, targetKey: string) {
function reorderColumns() {
setColumnsOrder((columnsOrder) => {
const sourceColumnOrderIndex = columnsOrder.findIndex(
(index) => columns[index].key === sourceKey
);
const targetColumnOrderIndex = columnsOrder.findIndex(
(index) => columns[index].key === targetKey
);
const sourceColumnOrder = columnsOrder[sourceColumnOrderIndex];
const newColumnsOrder = columnsOrder.toSpliced(sourceColumnOrderIndex, 1);
newColumnsOrder.splice(targetColumnOrderIndex, 0, sourceColumnOrder);
return newColumnsOrder;
});
}
document.startViewTransition(reorderColumns);
}
function resetOrderAndWidths() {
setColumnsOrder(initialColumnsOrder);
setColumnWidths(new Map());
}
return (
<>
<button
type="button"
onClick={resetOrderAndWidths}
style={{
width: 150,
marginBottom: 16
}}
>
Reset Columns
</button>
<DataGrid
aria-label="Columns Reordering Example"
columns={reorderedColumns}
rows={sortedRows}
sortColumns={sortColumns}
onSortColumnsChange={onSortColumnsChange}
direction={direction}
defaultColumnOptions={{ width: '1fr' }}
onColumnsReorder={onColumnsReorder}
columnWidths={columnWidths}
onColumnWidthsChange={setColumnWidths}
/>
</>
);
}