-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tabs.tsx
44 lines (35 loc) · 1.29 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
import React, { ReactElement } from "react";
export interface TabProps {
children: Array<ReactElement>;
}
export default function Tabs(props: TabProps) {
if (Array.isArray(props.children)) {
let keys = (props.children).map((node) => {
if (node.key) {
return node.key;
}
throw new Error("Key for immediate child of Tabs cannot be null");
});
let [activeKey, setKey] = React.useState(keys[0]);
const activeIndex = keys.indexOf(activeKey);
return <div className="tabs-container">
<div className="tabs" role="group">
{keys.map((key) => {
const onClick = () => { setKey(key); }
const className = (key === activeKey) ? "tabs-button tabs-button-active" : "tabs-button";
return (
<button
className={className}
key={key}
onClick={onClick}
>{key}</button>
);
})}
</div>
<div className="tabs-children">
{props.children[activeIndex]}
</div>
</div>
}
throw new Error("Tabs has no children");
}