-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTabs.tsx
More file actions
executable file
·62 lines (57 loc) · 1.48 KB
/
Tabs.tsx
File metadata and controls
executable file
·62 lines (57 loc) · 1.48 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
import React from "react";
import { TabItem, TabsProps } from "./Tabs.types";
const Tabs: React.FC<TabsProps> = ({
className = "",
id,
idPrefix = "",
items = [],
title = "Contents",
...attributes
}) => {
const filteredItems: TabItem[] = items.filter(Boolean); // filter out any falsy items in a cleaner way
const renderTab = (item: TabItem, index: number) => {
const tabId: string = item.id || `${idPrefix}-${index + 1}`;
return (
<li
key={tabId}
className={`govuk-tabs__list-item ${
index === 0 ? "govuk-tabs__list-item--selected" : ""
}`}
>
<a className="govuk-tabs__tab" href={`#${tabId}`} {...item}>
{item.label}
</a>
</li>
);
};
const renderPanel = (item: TabItem, index: number) => {
const panelId: string = item.id || `${idPrefix}-${index + 1}`;
return (
<div
key={panelId}
id={panelId}
className={`govuk-tabs__panel ${
index > 0 ? "govuk-tabs__panel--hidden" : ""
}`}
{...item.panel}
/>
);
};
return (
<div
id={id}
className={`govuk-tabs ${className}`}
{...attributes}
data-module="govuk-tabs"
>
<h2 className="govuk-tabs__title">{title}</h2>
{filteredItems.length > 0 && (
<>
<ul className="govuk-tabs__list">{filteredItems.map(renderTab)}</ul>
{filteredItems.map(renderPanel)}
</>
)}
</div>
);
};
export default Tabs;