This repository has been archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathTabs.tsx
73 lines (63 loc) · 1.96 KB
/
Tabs.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
import React, { useEffect, useState } from "react";
import Tab from "./Tab";
import { ScrollMenu, VisibilityContext } from "react-horizontal-scrolling-menu";
import { LeftArrow, RightArrow } from "./Arrows";
interface Props {
children: React.ReactNode;
defaultActive: string;
showArrows?: boolean;
activeColor?: string;
}
type scrollVisibilityApiType = React.ContextType<typeof VisibilityContext>;
function Tabs(props: Props) {
const [activeTab, setActiveTab] = useState<string>(props.defaultActive);
useEffect(() => {
setActiveTab(props.defaultActive);
}, [props.defaultActive]);
const onClickTabItem = (tab: string) => {
setActiveTab(tab);
};
return (
<div className="tabs">
<ScrollMenu
LeftArrow={props.showArrows && LeftArrow}
RightArrow={props.showArrows && RightArrow}
onWheel={onWheel}
wrapperClassName="border-base-lighter border-bottom margin-top-1"
>
{React.Children.map(props.children, (child) => {
return (
<Tab
id={(child as any).props.id}
itemId={(child as any).props.id}
activeTab={activeTab}
key={(child as any).props.id}
label={(child as any).props.label}
onClick={onClickTabItem}
activeColor={props.activeColor}
/>
);
})}
</ScrollMenu>
<div className="tab-content">
{React.Children.map(props.children, (child) => {
if ((child as any).props.id !== activeTab) return undefined;
return (child as any).props.children;
})}
</div>
</div>
);
}
function onWheel(apiObj: scrollVisibilityApiType, ev: React.WheelEvent): void {
const isThouchpad = Math.abs(ev.deltaX) !== 0 || Math.abs(ev.deltaY) < 15;
if (isThouchpad) {
ev.stopPropagation();
return;
}
if (ev.deltaY < 0) {
apiObj.scrollNext();
} else if (ev.deltaY > 0) {
apiObj.scrollPrev();
}
}
export default Tabs;