-
-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathtabBarRender-draggable.tsx
More file actions
141 lines (119 loc) · 3.03 KB
/
tabBarRender-draggable.tsx
File metadata and controls
141 lines (119 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React from 'react';
import { DndProvider, DragSource, DropTarget } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import Tabs from '../../src';
import type { TabsProps } from '../../src';
import '../../assets/index.less';
// Drag & Drop node
class TabNode extends React.Component<any, any> {
render() {
const { connectDragSource, connectDropTarget, children } = this.props;
return connectDragSource(connectDropTarget(children));
}
}
const cardTarget = {
drop(props, monitor) {
const dragKey = monitor.getItem().index;
const hoverKey = props.index;
if (dragKey === hoverKey) {
return;
}
props.moveTabNode(dragKey, hoverKey);
monitor.getItem().index = hoverKey;
},
};
const cardSource = {
beginDrag(props) {
return {
id: props.id,
index: props.index,
};
},
};
const WrapTabNode = DropTarget('DND_NODE', cardTarget, connect => ({
connectDropTarget: connect.dropTarget(),
}))(
DragSource('DND_NODE', cardSource, (connect, monitor) => ({
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging(),
}))(TabNode),
);
class DraggableTabs extends React.Component<TabsProps> {
state = {
order: [],
};
moveTabNode = (dragKey, hoverKey) => {
const newOrder = this.state.order.slice();
const { items } = this.props;
items.forEach(item => {
if (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);
this.setState({
order: newOrder,
});
};
tabBarRender = (props, DefaultTabBar) => (
<DefaultTabBar {...props}>
{node => {
return (
<WrapTabNode key={node.key} index={node.key} moveTabNode={this.moveTabNode}>
{node}
</WrapTabNode>
);
}}
</DefaultTabBar>
);
render() {
const { order } = this.state;
const { items } = this.props;
const tabs = [...items];
const orderTabs = tabs.slice().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 = tabs.indexOf(a);
const ib = tabs.indexOf(b);
return ia - ib;
});
return (
<DndProvider backend={HTML5Backend}>
<Tabs tabBarRender={this.tabBarRender} {...this.props} items={orderTabs} />
</DndProvider>
);
}
}
export default () => (
<DraggableTabs
items={[
{
key: '1',
label: 'tab 1',
children: 'Content of Tab Pane 1',
},
{
key: '2',
label: 'tab 2',
children: 'Content of Tab Pane 2',
},
{
key: '3',
label: 'tab 3',
children: 'Content of Tab Pane 3',
},
]}
/>
);