import { UploadOutlined } from '@ant-design/icons'; import type { DragEndEvent } from '@dnd-kit/core'; import { DndContext, PointerSensor, useSensor } from '@dnd-kit/core'; import { arrayMove, SortableContext, useSortable, verticalListSortingStrategy, } from '@dnd-kit/sortable'; import { CSS } from '@dnd-kit/utilities'; import { css } from '@emotion/css'; import { Button, Upload } from 'antd'; import type { UploadFile, UploadProps } from 'antd/es/upload/interface'; import React, { useState } from 'react'; interface DraggableUploadListItemProps { originNode: React.ReactElement>; file: UploadFile; } const DraggableUploadListItem = ({ originNode, file }: DraggableUploadListItemProps) => { const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: file.uid, }); const style: React.CSSProperties = { transform: CSS.Transform.toString(transform), transition, cursor: 'move', }; // prevent preview event when drag end const className = isDragging ? css` a { pointer-events: none; } ` : ''; return (
{/* hide error tooltip when dragging */} {file.status === 'error' && isDragging ? originNode.props.children : originNode}
); }; const App: React.FC = () => { const [fileList, setFileList] = useState([ { uid: '-1', name: 'image1.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, { uid: '-2', name: 'image2.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, { uid: '-3', name: 'image3.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, { uid: '-4', name: 'image4.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, { uid: '-5', name: 'image.png', status: 'error', }, ]); const sensor = useSensor(PointerSensor, { activationConstraint: { distance: 10 }, }); const onDragEnd = ({ active, over }: DragEndEvent) => { if (active.id !== over?.id) { setFileList((prev) => { const activeIndex = prev.findIndex((i) => i.uid === active.id); const overIndex = prev.findIndex((i) => i.uid === over?.id); return arrayMove(prev, activeIndex, overIndex); }); } }; const onChange: UploadProps['onChange'] = ({ fileList: newFileList }) => { setFileList(newFileList); }; return ( i.uid)} strategy={verticalListSortingStrategy}> ( )} > ); }; export default App;