-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathFrameworkCodeTabs.tsx
More file actions
54 lines (45 loc) · 1.51 KB
/
FrameworkCodeTabs.tsx
File metadata and controls
54 lines (45 loc) · 1.51 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
import React from "react"
import Tabs from "@theme/Tabs"
import TabItem from "@theme/TabItem"
const DEFAULT_FRAMEWORKS = [
{ value: "javascript", label: "Express (JS)" },
{ value: "nextjs", label: "Next.js" },
{ value: "go", label: "Go" },
{ value: "java", label: "Java" },
]
export const FrameworkCodeTabs = ({ children }) => {
// Filter out children (tabs) that don't have content
const tabsWithContent = React.Children.toArray(children).filter((child) => {
if (!React.isValidElement(child)) return false
// Check if the tab has any meaningful content
const content = child.props.children
if (!content) return false
// If content is a string, check if it's not just whitespace
if (typeof content === "string") {
return content.trim().length > 0
}
// If content is an array, check if any element has content
if (Array.isArray(content)) {
return content.some((item) =>
typeof item === "string" ? item.trim().length > 0 : !!item,
)
}
return true
})
// Get available framework values from tabs with content
const availableFrameworks = DEFAULT_FRAMEWORKS.filter((framework) =>
tabsWithContent.some(
(tab) => React.isValidElement(tab) && tab.props.value === framework.value,
),
)
return availableFrameworks.length > 0 ? (
<Tabs
groupId="framework-examples"
defaultValue={availableFrameworks[0].value}
values={availableFrameworks}
>
{tabsWithContent}
</Tabs>
) : null
}
export default FrameworkCodeTabs