-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathcolumn-sortable.tsx
More file actions
97 lines (89 loc) · 2.79 KB
/
column-sortable.tsx
File metadata and controls
97 lines (89 loc) · 2.79 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
import type { DragEndEvent } from '@dnd-kit/core';
import { DndContext, closestCenter } from '@dnd-kit/core';
import { SortableContext, arrayMove, useSortable, rectSwappingStrategy } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import type { ColumnType } from 'rc-table';
import Table from 'rc-table';
import React, { useCallback, useMemo, useState } from 'react';
import 'react-resizable/css/styles.css';
import '../../assets/index.less';
import { restrictToHorizontalAxis } from '@dnd-kit/modifiers';
const SortableHeaderCell = props => {
const { width, style: styleProps, ...restProps } = props;
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({
id: props.id as string,
});
const style = {
...styleProps,
transform: CSS.Transform.toString(transform),
transition,
cursor: 'move',
};
return <th ref={setNodeRef} style={style} {...attributes} {...listeners} {...restProps} />;
};
const data = [
{ a: '123', key: '1' },
{ a: 'cdd', b: 'edd', key: '2' },
{ a: '1333', c: 'eee', d: 2, key: '3' },
] as const;
const Demo = () => {
const [columns, setColumns] = useState([
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
{
title: 'Operations',
dataIndex: 's',
key: 'd',
render() {
return <a href="#">Operations</a>;
},
},
]);
const columnsWithSortable = useMemo(() => {
return columns.map(col => ({
...col,
onHeaderCell: () => ({
id: col.dataIndex?.toString(),
}),
onCell: () => ({
id: col.dataIndex?.toString(),
}),
}));
}, [columns]);
const tableProps = useMemo(() => {
return {
components: { header: { cell: SortableHeaderCell } },
columns: columnsWithSortable,
data,
};
}, [columnsWithSortable]);
const handleDragEnd = useCallback((e: DragEndEvent) => {
const { active, over } = e;
if (over) {
setColumns(prevColumns => {
const activeIndex = prevColumns.findIndex(col => col.key === active.id);
const overIndex = prevColumns.findIndex(col => col.key === over.id);
if (activeIndex === -1 || overIndex === -1) {
return prevColumns;
}
return arrayMove([...prevColumns], activeIndex, overIndex);
});
}
}, []);
return (
<DndContext
collisionDetection={closestCenter}
onDragEnd={handleDragEnd}
modifiers={[restrictToHorizontalAxis]}
>
<SortableContext
items={columns.map(col => col.dataIndex?.toString())}
strategy={rectSwappingStrategy}
>
<Table {...tableProps} />;
</SortableContext>
</DndContext>
);
};
export default Demo;