2019-01-16 20:00:23 +08:00
|
|
|
---
|
|
|
|
order: 13
|
|
|
|
title:
|
|
|
|
zh-CN: 可拖拽标签
|
|
|
|
en-US: Draggable Tabs
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2022-02-10 17:31:38 +08:00
|
|
|
使用 `react-dnd@15+` 实现标签可拖拽。
|
2019-01-16 20:00:23 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2022-02-10 17:31:38 +08:00
|
|
|
Use `react-dnd@15+` to make tabs draggable.
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-23 14:37:16 +08:00
|
|
|
import type { TabsProps } from 'antd';
|
2019-01-16 20:00:23 +08:00
|
|
|
import { Tabs } from 'antd';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React, { useRef, useState } from 'react';
|
2022-02-10 17:31:38 +08:00
|
|
|
import { DndProvider, useDrag, useDrop } from 'react-dnd';
|
2020-05-28 23:12:04 +08:00
|
|
|
import { HTML5Backend } from 'react-dnd-html5-backend';
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-02-10 17:31:38 +08:00
|
|
|
const type = 'DraggableTabNode';
|
2022-05-19 09:46:26 +08:00
|
|
|
interface DraggableTabPaneProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
|
|
index: React.Key;
|
|
|
|
moveNode: (dragIndex: React.Key, hoverIndex: React.Key) => void;
|
|
|
|
}
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const DraggableTabNode = ({ index, children, moveNode }: DraggableTabPaneProps) => {
|
|
|
|
const ref = useRef<HTMLDivElement>(null);
|
2022-02-10 17:31:38 +08:00
|
|
|
const [{ isOver, dropClassName }, drop] = useDrop({
|
|
|
|
accept: type,
|
|
|
|
collect: monitor => {
|
|
|
|
const { index: dragIndex } = monitor.getItem() || {};
|
|
|
|
if (dragIndex === index) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
isOver: monitor.isOver(),
|
|
|
|
dropClassName: 'dropping',
|
|
|
|
};
|
|
|
|
},
|
2022-05-19 09:46:26 +08:00
|
|
|
drop: (item: { index: React.Key }) => {
|
2022-02-10 17:31:38 +08:00
|
|
|
moveNode(item.index, index);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const [, drag] = useDrag({
|
|
|
|
type,
|
|
|
|
item: { index },
|
|
|
|
collect: monitor => ({
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
drop(drag(ref));
|
2022-05-19 09:46:26 +08:00
|
|
|
|
2022-02-10 17:31:38 +08:00
|
|
|
return (
|
2022-05-19 09:46:26 +08:00
|
|
|
<div ref={ref} style={{ marginRight: 24 }} className={isOver ? dropClassName : ''}>
|
2022-02-10 17:31:38 +08:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
2019-01-16 20:00:23 +08:00
|
|
|
};
|
|
|
|
|
2022-08-05 10:49:08 +08:00
|
|
|
const DraggableTabs: React.FC<TabsProps> = props => {
|
|
|
|
const { items = [] } = props;
|
2022-05-19 09:46:26 +08:00
|
|
|
const [order, setOrder] = useState<React.Key[]>([]);
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const moveTabNode = (dragKey: React.Key, hoverKey: React.Key) => {
|
|
|
|
const newOrder = order.slice();
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-08-05 10:49:08 +08:00
|
|
|
items.forEach(item => {
|
|
|
|
if (item.key && newOrder.indexOf(item.key) === -1) {
|
|
|
|
newOrder.push(item.key);
|
2019-01-16 20:00:23 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const dragIndex = newOrder.indexOf(dragKey);
|
|
|
|
const hoverIndex = newOrder.indexOf(hoverKey);
|
|
|
|
|
|
|
|
newOrder.splice(dragIndex, 1);
|
|
|
|
newOrder.splice(hoverIndex, 0, dragKey);
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
setOrder(newOrder);
|
2019-01-16 20:00:23 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const renderTabBar: TabsProps['renderTabBar'] = (tabBarProps, DefaultTabBar) => (
|
|
|
|
<DefaultTabBar {...tabBarProps}>
|
2019-01-16 20:00:23 +08:00
|
|
|
{node => (
|
2022-05-19 09:46:26 +08:00
|
|
|
<DraggableTabNode key={node.key} index={node.key!} moveNode={moveTabNode}>
|
2019-05-07 14:57:32 +08:00
|
|
|
{node}
|
2022-02-10 17:31:38 +08:00
|
|
|
</DraggableTabNode>
|
2019-01-16 20:00:23 +08:00
|
|
|
)}
|
|
|
|
</DefaultTabBar>
|
|
|
|
);
|
|
|
|
|
2022-08-05 10:49:08 +08:00
|
|
|
const orderItems = [...items].sort((a, b) => {
|
2022-05-19 09:46:26 +08:00
|
|
|
const orderA = order.indexOf(a.key!);
|
|
|
|
const orderB = order.indexOf(b.key!);
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
if (orderA !== -1 && orderB !== -1) {
|
|
|
|
return orderA - orderB;
|
|
|
|
}
|
|
|
|
if (orderA !== -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (orderB !== -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-08-05 10:49:08 +08:00
|
|
|
const ia = items.indexOf(a);
|
|
|
|
const ib = items.indexOf(b);
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return ia - ib;
|
|
|
|
});
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return (
|
|
|
|
<DndProvider backend={HTML5Backend}>
|
2022-08-05 10:49:08 +08:00
|
|
|
<Tabs renderTabBar={renderTabBar} {...props} items={orderItems} />
|
2022-05-19 09:46:26 +08:00
|
|
|
</DndProvider>
|
|
|
|
);
|
|
|
|
};
|
2019-01-16 20:00:23 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => (
|
2022-08-05 10:49:08 +08:00
|
|
|
<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}`,
|
|
|
|
};
|
|
|
|
})}
|
|
|
|
/>
|
2019-05-07 14:57:32 +08:00
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2022-02-10 17:31:38 +08:00
|
|
|
|
|
|
|
```css
|
|
|
|
.dropping {
|
|
|
|
background: #fefefe;
|
|
|
|
transition: all 0.3s;
|
|
|
|
}
|
|
|
|
```
|