mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-14 00:41:29 +08:00
7fd093bd0a
* docs: add general components TS demo * docs: add layout components TS demo * docs: add navigation components TS demo * docs: add data entry components TS demo * chore(deps): add types for qs * docs: add data display TS demo * docs: add feedback components TS demo * docs: add other components TS demo * chore(deps): add types * docs: unified demo code style * docs: fix lint error * docs: add demo TS type * docs: fix demo TS type * test: update snapshot * docs: fix TS demo * feat: update Rate character type * docs: fix lint error * feat: update Rate character type * feat: update Rate character type
172 lines
3.5 KiB
Markdown
172 lines
3.5 KiB
Markdown
---
|
|
order: 26
|
|
title:
|
|
en-US: Drag sorting
|
|
zh-CN: 拖拽排序
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
使用自定义元素,我们可以集成 [react-dnd](https://github.com/react-dnd/react-dnd) 来实现拖拽排序。
|
|
|
|
## en-US
|
|
|
|
By using `components`, we can integrate table with [react-dnd](https://github.com/react-dnd/react-dnd) to implement drag sorting function.
|
|
|
|
```tsx
|
|
import React, { useState, useCallback, useRef } from 'react';
|
|
import { Table } from 'antd';
|
|
import { DndProvider, useDrag, useDrop } from 'react-dnd';
|
|
import { HTML5Backend } from 'react-dnd-html5-backend';
|
|
import update from 'immutability-helper';
|
|
import type { ColumnsType } from 'antd/lib/table';
|
|
|
|
interface DataType {
|
|
key: string;
|
|
name: string;
|
|
age: number;
|
|
address: string;
|
|
}
|
|
|
|
interface DraggableBodyRowProps extends React.HTMLAttributes<HTMLTableRowElement> {
|
|
index: number;
|
|
moveRow: (dragIndex: number, hoverIndex: number) => void;
|
|
}
|
|
|
|
const type = 'DraggableBodyRow';
|
|
|
|
const DraggableBodyRow = ({
|
|
index,
|
|
moveRow,
|
|
className,
|
|
style,
|
|
...restProps
|
|
}: DraggableBodyRowProps) => {
|
|
const ref = useRef<HTMLTableRowElement>(null);
|
|
const [{ isOver, dropClassName }, drop] = useDrop({
|
|
accept: type,
|
|
collect: monitor => {
|
|
const { index: dragIndex } = monitor.getItem() || {};
|
|
if (dragIndex === index) {
|
|
return {};
|
|
}
|
|
return {
|
|
isOver: monitor.isOver(),
|
|
dropClassName: dragIndex < index ? ' drop-over-downward' : ' drop-over-upward',
|
|
};
|
|
},
|
|
drop: (item: { index: number }) => {
|
|
moveRow(item.index, index);
|
|
},
|
|
});
|
|
const [, drag] = useDrag({
|
|
type,
|
|
item: { index },
|
|
collect: monitor => ({
|
|
isDragging: monitor.isDragging(),
|
|
}),
|
|
});
|
|
drop(drag(ref));
|
|
|
|
return (
|
|
<tr
|
|
ref={ref}
|
|
className={`${className}${isOver ? dropClassName : ''}`}
|
|
style={{ cursor: 'move', ...style }}
|
|
{...restProps}
|
|
/>
|
|
);
|
|
};
|
|
|
|
const columns: ColumnsType<DataType> = [
|
|
{
|
|
title: 'Name',
|
|
dataIndex: 'name',
|
|
key: 'name',
|
|
},
|
|
{
|
|
title: 'Age',
|
|
dataIndex: 'age',
|
|
key: 'age',
|
|
},
|
|
{
|
|
title: 'Address',
|
|
dataIndex: 'address',
|
|
key: 'address',
|
|
},
|
|
];
|
|
|
|
const App: React.FC = () => {
|
|
const [data, setData] = useState([
|
|
{
|
|
key: '1',
|
|
name: 'John Brown',
|
|
age: 32,
|
|
address: 'New York No. 1 Lake Park',
|
|
},
|
|
{
|
|
key: '2',
|
|
name: 'Jim Green',
|
|
age: 42,
|
|
address: 'London No. 1 Lake Park',
|
|
},
|
|
{
|
|
key: '3',
|
|
name: 'Joe Black',
|
|
age: 32,
|
|
address: 'Sidney No. 1 Lake Park',
|
|
},
|
|
]);
|
|
|
|
const components = {
|
|
body: {
|
|
row: DraggableBodyRow,
|
|
},
|
|
};
|
|
|
|
const moveRow = useCallback(
|
|
(dragIndex: number, hoverIndex: number) => {
|
|
const dragRow = data[dragIndex];
|
|
setData(
|
|
update(data, {
|
|
$splice: [
|
|
[dragIndex, 1],
|
|
[hoverIndex, 0, dragRow],
|
|
],
|
|
}),
|
|
);
|
|
},
|
|
[data],
|
|
);
|
|
|
|
return (
|
|
<DndProvider backend={HTML5Backend}>
|
|
<Table
|
|
columns={columns}
|
|
dataSource={data}
|
|
components={components}
|
|
onRow={(_, index) => {
|
|
const attr = {
|
|
index,
|
|
moveRow,
|
|
};
|
|
return attr as React.HTMLAttributes<any>;
|
|
}}
|
|
/>
|
|
</DndProvider>
|
|
);
|
|
};
|
|
|
|
export default App;
|
|
```
|
|
|
|
```css
|
|
#components-table-demo-drag-sorting tr.drop-over-downward td {
|
|
border-bottom: 2px dashed #1890ff;
|
|
}
|
|
|
|
#components-table-demo-drag-sorting tr.drop-over-upward td {
|
|
border-top: 2px dashed #1890ff;
|
|
}
|
|
```
|