Skip to content

Commit d87420d

Browse files
committed
fix(Tabs): add possibility to define tab header through children
1 parent 7c84339 commit d87420d

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/Tabs.stories.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,35 @@ export const Default = () => {
3030
)
3131
}
3232

33+
export const Children = () => {
34+
return (
35+
<Tabs defaultValue="leaderboard">
36+
<Tabs.TabList>
37+
<Tabs.Tab key="leaderboard" value="leaderboard">
38+
<div className="italic">Leaderboard</div>
39+
</Tabs.Tab>
40+
<Tabs.Tab key="create" value="create">
41+
<div className="font-bold">Child Content</div>
42+
</Tabs.Tab>
43+
</Tabs.TabList>
44+
<Tabs.TabContent
45+
key="leaderboard"
46+
value="leaderboard"
47+
className={{ root: 'md:px-4' }}
48+
>
49+
Course Tab
50+
</Tabs.TabContent>
51+
<Tabs.TabContent
52+
key="create"
53+
value="create"
54+
className={{ root: 'md:px-4' }}
55+
>
56+
Create Tab
57+
</Tabs.TabContent>
58+
</Tabs>
59+
)
60+
}
61+
3362
export const Controlled = () => {
3463
const [selectedTab, setSelectedTab] = useState('leaderboard')
3564
return (

src/Tabs.tsx

+16-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ interface TabProps {
1010
}
1111
key?: string
1212
value: string
13-
label: string
13+
label?: string
14+
children?: React.ReactNode
1415
disabled?: boolean
1516
className?: {
1617
override?: string
@@ -20,6 +21,16 @@ interface TabProps {
2021
}
2122
}
2223

24+
interface TabPropsWithLabel extends TabProps {
25+
label: string
26+
children?: never
27+
}
28+
29+
interface TabPropsWithChildren extends TabProps {
30+
label?: never
31+
children: React.ReactNode
32+
}
33+
2334
/**
2435
* This function returns a pre-styled Tab trigger component to be used inside a Tabs.Tablist.
2536
* The value of this tab is required for both the internally and externally controlled state.
@@ -29,6 +40,7 @@ interface TabProps {
2940
* @param key - The key of the tab.
3041
* @param value - The value of the tab. This is required for the internal and external state.
3142
* @param label - The label of the tab.
43+
* @param children - A child component of the tab header, which can optionally replace the label
3244
* @param disabled - The optional disabled property allows you to disable the tab.
3345
* @param className - The optional className object allows you to override the default styling.
3446
* @returns Tab trigger component
@@ -39,9 +51,10 @@ export function Tab({
3951
key,
4052
value,
4153
label,
54+
children,
4255
disabled,
4356
className,
44-
}: TabProps) {
57+
}: TabPropsWithLabel | TabPropsWithChildren) {
4558
return (
4659
<TabsPrimitive.Trigger
4760
id={id}
@@ -64,7 +77,7 @@ export function Tab({
6477
className?.label
6578
)}
6679
>
67-
{label}
80+
{label ?? children}
6881
</span>
6982
</TabsPrimitive.Trigger>
7083
)

0 commit comments

Comments
 (0)