-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathTabs.tsx
65 lines (56 loc) · 1.52 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
import { useState } from 'react';
import cx from 'classnames';
type TabComponentProps = {
label: string;
content: React.ReactNode;
};
export type TabProps = {
id: string;
label: string;
title?: React.ReactNode;
withWarning?: boolean;
content: React.ReactNode;
};
type TabsProps = {
tabs: TabProps[];
pills?: boolean;
fullWidth?: boolean;
fullHeight?: boolean;
};
const Tab = ({ label, content }: TabComponentProps) => (
<div className="tab-pane active" aria-labelledby={label}>
{content}
</div>
);
const Tabs = ({ tabs, pills = false, fullWidth = false, fullHeight = false }: TabsProps) => {
const [activeTabIndex, setActiveTabIndex] = useState(0);
const handleTabClick = (index: number) => {
setActiveTabIndex(index);
};
return (
<div className={cx('tabs-container', { 'full-width': fullWidth, 'full-height': fullHeight })}>
<div className={cx('tabs', pills && 'pills')}>
{tabs.map((tab, index) => (
<div
data-testid={`tab-${tab.id}`}
className={cx(
'tab',
index === activeTabIndex && 'active',
tab.withWarning && 'warning'
)}
key={`tab-${tab.label}-${index}}`}
role="button"
tabIndex={0}
onClick={() => handleTabClick(index)}
>
{tab.title || tab.label}
</div>
))}
</div>
<div className="tab-content">
<Tab {...tabs[activeTabIndex]} />
</div>
</div>
);
};
export default Tabs;