This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTabbedMenu.tsx
More file actions
65 lines (58 loc) · 1.77 KB
/
TabbedMenu.tsx
File metadata and controls
65 lines (58 loc) · 1.77 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
import { useEffect, useState } from 'react';
export interface TabbedPaneProps {
tabs: string[];
activeTab?: number;
/**
* Tracks to see if this component needs to be refreshed
* Useful if the tab needs to be updated but this component
* is already mounted
*/
id?: string;
children: React.ReactNode | React.ReactNode[];
onChange?: (index: number) => void;
className?: string;
}
const TabbedMenu = (props: TabbedPaneProps) => {
const tabKey = props.id || 'default';
const [id, setId] = useState(props.id);
const [activeTab, setActiveTab] = useState({
[tabKey]: props.activeTab || 0,
});
useEffect(() => {
if (id !== tabKey) {
setId(tabKey);
if (activeTab[tabKey] === undefined) {
setActiveTab({ ...activeTab, [tabKey]: props.activeTab || 0 });
}
}
}, [tabKey, id, activeTab, setActiveTab, setId, props.activeTab]);
return (
<div className="h-full">
<div className="flex border-b border-circle-gray-300 pl-6">
{props.tabs.map((tab, index) => (
<button
type="button"
key={index}
className={`text-sm tracking-wide px-3 py-3 font-bold text-center ${
index === activeTab[tabKey]
? 'border-circle-blue border-b-4 text-circle-black'
: 'text-circle-gray-600 mb-1 hover:text-circle-black'
}`}
onClick={() => {
if (props.onChange) {
props.onChange(index);
}
setActiveTab({ ...activeTab, [tabKey]: index });
}}
>
{tab}
</button>
))}
</div>
{Array.isArray(props.children)
? props.children[activeTab[tabKey]]
: props.children}
</div>
);
};
export default TabbedMenu;