-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathindex.tsx
134 lines (116 loc) · 3.45 KB
/
index.tsx
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
import { useRouter } from "@/router/hooks";
import { replaceDynamicParams } from "@/router/hooks/use-current-route-meta";
import { Tabs } from "antd";
import { useEffect, useRef } from "react";
import styled from "styled-components";
import SortableContainer from "./components/sortable-container";
import { SortableItem } from "./components/sortable-item";
import { TabItem } from "./components/tab-item";
import { useMultiTabsStyle } from "./hooks/use-tab-style";
import { useMultiTabsContext } from "./providers/multi-tabs-provider";
import type { KeepAliveTab } from "./types";
export default function MultiTabs() {
const scrollContainer = useRef<HTMLUListElement>(null);
const { tabs, activeTabRoutePath, setTabs } = useMultiTabsContext();
const style = useMultiTabsStyle();
const { push } = useRouter();
const handleTabClick = ({ key, params = {} }: KeepAliveTab) => {
console.log("handleTabClick", key, params);
const tabKey = replaceDynamicParams(key, params);
push(tabKey);
};
useEffect(() => {
if (!scrollContainer.current) return;
const tab = tabs.find((item) => item.key === activeTabRoutePath);
const currentTabElement = scrollContainer.current.querySelector(`#tab${tab?.key.split("/").join("-")}`);
if (currentTabElement) {
currentTabElement.scrollIntoView({
block: "nearest",
behavior: "smooth",
});
}
}, [tabs, activeTabRoutePath]);
useEffect(() => {
const container = scrollContainer.current;
if (!container) return;
const handleWheel = (e: WheelEvent) => {
e.preventDefault();
container.scrollLeft += e.deltaY;
};
container.addEventListener("mouseenter", () => {
container.addEventListener("wheel", handleWheel);
});
container.addEventListener("mouseleave", () => {
container.removeEventListener("wheel", handleWheel);
});
return () => {
container.removeEventListener("wheel", handleWheel);
};
}, []);
const handleDragEnd = (oldIndex: number, newIndex: number) => {
const newTabs = Array.from(tabs);
const [movedTab] = newTabs.splice(oldIndex, 1);
newTabs.splice(newIndex, 0, movedTab);
setTabs([...newTabs]);
};
const renderOverlay = (id: string | number) => {
const tab = tabs.find((tab) => tab.key === id);
if (!tab) return null;
return <TabItem tab={tab} />;
};
return (
<StyledMultiTabs>
<Tabs
size="small"
type="card"
tabBarGutter={4}
activeKey={activeTabRoutePath}
items={tabs.map((tab) => ({
...tab,
children: <div key={tab.timeStamp}>{tab.children}</div>,
}))}
renderTabBar={() => {
return (
<div style={style}>
<SortableContainer items={tabs} onSortEnd={handleDragEnd} renderOverlay={renderOverlay}>
<ul ref={scrollContainer} className="flex overflow-x-auto w-full px-2 h-[32px] hide-scrollbar">
{tabs.map((tab) => (
<SortableItem tab={tab} key={tab.key} onClick={() => handleTabClick(tab)} />
))}
</ul>
</SortableContainer>
</div>
);
}}
/>
</StyledMultiTabs>
);
}
const StyledMultiTabs = styled.div`
height: 100%;
margin-top: 2px;
.anticon {
margin: 0px !important;
}
.ant-tabs {
height: 100%;
.ant-tabs-content {
height: 100%;
}
.ant-tabs-tabpane {
height: 100%;
& > div {
height: 100%;
}
}
}
.hide-scrollbar {
overflow: scroll;
scrollbar-width: none;
-ms-overflow-style: none;
will-change: transform;
&::-webkit-scrollbar {
display: none;
}
}
`;