-
Notifications
You must be signed in to change notification settings - Fork 24
/
Table.jsx
130 lines (116 loc) · 2.83 KB
/
Table.jsx
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
import { MenuOutlined } from '@ant-design/icons';
import { Table } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { arrayMoveImmutable } from 'array-move';
import React, { useState } from 'react';
import type { SortableContainerProps, SortEnd } from 'react-sortable-hoc';
import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
interface DataType {
key: string;
year: string;
faculty: number;
department: string;
index: number;
}
const DragHandle = SortableHandle(() => <MenuOutlined style={{ cursor: 'grab', color: '#999999' }} />);
const columns: ColumnsType<DataType> = [
{
title: 'Sort',
dataIndex: 'sort',
width: 30,
className: 'drag-visible',
render: () => <DragHandle />,
},
{
title: 'Year',
dataIndex: 'year',
className: 'drag-visible',
},
{
title: 'Faculty',
dataIndex: 'faculty',
},
{
title: 'Department',
dataIndex: 'department',
},
];
const data: DataType[] = [
{
key: '1',
year: '2016/2017',
faculty: "FAS",
department: 'New York No. 1 Lake Park',
index: 0,
},
{
key: '2',
year: '2015/2016',
faculty: "Management",
department: 'CIS',
index: 1,
},
{
key: '3',
year: '2017/2018',
faculty: "Agriculture",
department: 'NR',
index: 2,
},
];
const SortableItem = SortableElement((props: React.HTMLAttributes<HTMLTableRowElement>) => (
<tr {...props} />
));
const SortableBody = SortableContainer((props: React.HTMLAttributes<HTMLTableSectionElement>) => (
<tbody {...props} />
));
const App: React.FC = () => {
const [dataSource, setDataSource] = useState(data);
const onSortEnd = ({ oldIndex, newIndex }: SortEnd) => {
if (oldIndex !== newIndex) {
const newData = arrayMoveImmutable(dataSource.slice(), oldIndex, newIndex).filter(
(el: DataType) => !!el,
);
console.log('Sorted items: ', newData);
setDataSource(newData);
}
};
const DraggableContainer = (props: SortableContainerProps) => (
<SortableBody
useDragHandle
disableAutoscroll
helperClass="row-dragging"
onSortEnd={onSortEnd}
{...props}
/>
);
const DraggableBodyRow: React.FC<any> = ({ className, style, ...restProps }) => {
const index = dataSource.findIndex(x => x.index === restProps['data-row-key']);
return <SortableItem index={index} {...restProps} />;
};
return (
<Table
pagination={false}
dataSource={dataSource}
columns={columns}
rowKey="index"
components={{
body: {
wrapper: DraggableContainer,
row: DraggableBodyRow,
},
}}
/>
);
};
export default App;
.row-dragging {
background: #fafafa;
border: 1px solid #cccccc;
}
.row-dragging td {
padding: 15px;
}
.row-dragging .drag-visible {
visibility: visible;
}