2023-02-02 10:54:13 +08:00
import { MenuOutlined } from '@ant-design/icons' ;
import type { DragEndEvent } from '@dnd-kit/core' ;
import { DndContext } from '@dnd-kit/core' ;
2023-05-31 13:36:20 +08:00
import { restrictToVerticalAxis } from '@dnd-kit/modifiers' ;
2023-02-02 10:54:13 +08:00
import {
arrayMove ,
SortableContext ,
useSortable ,
verticalListSortingStrategy ,
} from '@dnd-kit/sortable' ;
import { CSS } from '@dnd-kit/utilities' ;
2023-07-28 16:17:43 +08:00
import React , { useState } from 'react' ;
2023-02-02 10:54:13 +08:00
import { Table } from 'antd' ;
import type { ColumnsType } from 'antd/es/table' ;
interface DataType {
key : string ;
name : string ;
age : number ;
address : string ;
}
const columns : ColumnsType < DataType > = [
{
key : 'sort' ,
} ,
{
title : 'Name' ,
dataIndex : 'name' ,
} ,
{
title : 'Age' ,
dataIndex : 'age' ,
} ,
{
title : 'Address' ,
dataIndex : 'address' ,
} ,
] ;
interface RowProps extends React . HTMLAttributes < HTMLTableRowElement > {
'data-row-key' : string ;
}
const Row = ( { children , . . . props } : RowProps ) = > {
const {
attributes ,
listeners ,
setNodeRef ,
setActivatorNodeRef ,
transform ,
transition ,
isDragging ,
} = useSortable ( {
id : props [ 'data-row-key' ] ,
} ) ;
const style : React.CSSProperties = {
. . . props . style ,
2023-05-31 13:36:20 +08:00
transform : CSS.Transform.toString ( transform && { . . . transform , scaleY : 1 } ) ,
2023-02-02 10:54:13 +08:00
transition ,
. . . ( isDragging ? { position : 'relative' , zIndex : 9999 } : { } ) ,
} ;
return (
< tr { ...props } ref = { setNodeRef } style = { style } { ...attributes } >
{ React . Children . map ( children , ( child ) = > {
if ( ( child as React . ReactElement ) . key === 'sort' ) {
return React . cloneElement ( child as React . ReactElement , {
children : (
< MenuOutlined
ref = { setActivatorNodeRef }
style = { { touchAction : 'none' , cursor : 'move' } }
{ . . . listeners }
/ >
) ,
} ) ;
}
return child ;
} ) }
< / tr >
) ;
} ;
const App : React.FC = ( ) = > {
const [ dataSource , setDataSource ] = useState ( [
{
key : '1' ,
name : 'John Brown' ,
age : 32 ,
address :
'Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text' ,
} ,
{
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 onDragEnd = ( { active , over } : DragEndEvent ) = > {
if ( active . id !== over ? . id ) {
setDataSource ( ( previous ) = > {
const activeIndex = previous . findIndex ( ( i ) = > i . key === active . id ) ;
const overIndex = previous . findIndex ( ( i ) = > i . key === over ? . id ) ;
return arrayMove ( previous , activeIndex , overIndex ) ;
} ) ;
}
} ;
return (
2023-05-31 13:36:20 +08:00
< DndContext modifiers = { [ restrictToVerticalAxis ] } onDragEnd = { onDragEnd } >
2023-02-02 10:54:13 +08:00
< SortableContext
// rowKey array
items = { dataSource . map ( ( i ) = > i . key ) }
strategy = { verticalListSortingStrategy }
>
< Table
components = { {
body : {
row : Row ,
} ,
} }
rowKey = "key"
columns = { columns }
dataSource = { dataSource }
/ >
< / SortableContext >
< / DndContext >
) ;
} ;
export default App ;