2022-11-09 12:28:04 +08:00
|
|
|
import React, { useRef, useState } from 'react';
|
|
|
|
import type { TabsProps } from 'antd';
|
|
|
|
import { Tabs } from 'antd';
|
|
|
|
import { DndProvider, useDrag, useDrop } from 'react-dnd';
|
|
|
|
import { HTML5Backend } from 'react-dnd-html5-backend';
|
|
|
|
|
|
|
|
const type = 'DraggableTabNode';
|
|
|
|
interface DraggableTabPaneProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
|
|
index: React.Key;
|
|
|
|
moveNode: (dragIndex: React.Key, hoverIndex: React.Key) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DraggableTabNode = ({ index, children, moveNode }: DraggableTabPaneProps) => {
|
|
|
|
const ref = useRef<HTMLDivElement>(null);
|
2022-12-23 12:12:24 +08:00
|
|
|
|
|
|
|
const [{ isOver }, drop] = useDrop({
|
2022-11-09 12:28:04 +08:00
|
|
|
accept: type,
|
2022-11-19 13:47:33 +08:00
|
|
|
collect: (monitor) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
const { index: dragIndex } = monitor.getItem() || {};
|
|
|
|
if (dragIndex === index) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
isOver: monitor.isOver(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
drop: (item: { index: React.Key }) => {
|
|
|
|
moveNode(item.index, index);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const [, drag] = useDrag({
|
|
|
|
type,
|
|
|
|
item: { index },
|
2022-11-19 13:47:33 +08:00
|
|
|
collect: (monitor) => ({
|
2022-11-09 12:28:04 +08:00
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
drop(drag(ref));
|
|
|
|
|
2022-12-23 12:12:24 +08:00
|
|
|
// Style
|
|
|
|
const style: React.CSSProperties = { marginRight: 24 };
|
|
|
|
if (isOver) {
|
|
|
|
style.transition = 'all 0.3s';
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
return (
|
2022-12-23 12:12:24 +08:00
|
|
|
<div ref={ref} style={style}>
|
2022-11-09 12:28:04 +08:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const DraggableTabs: React.FC<TabsProps> = (props) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
const { items = [] } = props;
|
|
|
|
const [order, setOrder] = useState<React.Key[]>([]);
|
|
|
|
|
|
|
|
const moveTabNode = (dragKey: React.Key, hoverKey: React.Key) => {
|
|
|
|
const newOrder = order.slice();
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
items.forEach((item) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
if (item.key && newOrder.indexOf(item.key) === -1) {
|
|
|
|
newOrder.push(item.key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const dragIndex = newOrder.indexOf(dragKey);
|
|
|
|
const hoverIndex = newOrder.indexOf(hoverKey);
|
|
|
|
|
|
|
|
newOrder.splice(dragIndex, 1);
|
|
|
|
newOrder.splice(hoverIndex, 0, dragKey);
|
|
|
|
|
|
|
|
setOrder(newOrder);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderTabBar: TabsProps['renderTabBar'] = (tabBarProps, DefaultTabBar) => (
|
|
|
|
<DefaultTabBar {...tabBarProps}>
|
2022-11-19 13:47:33 +08:00
|
|
|
{(node) => (
|
2022-11-09 12:28:04 +08:00
|
|
|
<DraggableTabNode key={node.key} index={node.key!} moveNode={moveTabNode}>
|
|
|
|
{node}
|
|
|
|
</DraggableTabNode>
|
|
|
|
)}
|
|
|
|
</DefaultTabBar>
|
|
|
|
);
|
|
|
|
|
|
|
|
const orderItems = [...items].sort((a, b) => {
|
|
|
|
const orderA = order.indexOf(a.key!);
|
|
|
|
const orderB = order.indexOf(b.key!);
|
|
|
|
|
|
|
|
if (orderA !== -1 && orderB !== -1) {
|
|
|
|
return orderA - orderB;
|
|
|
|
}
|
|
|
|
if (orderA !== -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (orderB !== -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ia = items.indexOf(a);
|
|
|
|
const ib = items.indexOf(b);
|
|
|
|
|
|
|
|
return ia - ib;
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DndProvider backend={HTML5Backend}>
|
|
|
|
<Tabs renderTabBar={renderTabBar} {...props} items={orderItems} />
|
|
|
|
</DndProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const App: React.FC = () => (
|
|
|
|
<DraggableTabs
|
|
|
|
items={new Array(3).fill(null).map((_, i) => {
|
|
|
|
const id = String(i + 1);
|
|
|
|
return {
|
|
|
|
label: `tab ${id}`,
|
|
|
|
key: id,
|
|
|
|
children: `Content of Tab Pane ${id}`,
|
|
|
|
};
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default App;
|