Skip to content

Commit

Permalink
fix(Tabs): add possibility to define tab header through children
Browse files Browse the repository at this point in the history
  • Loading branch information
sjschlapbach committed Feb 17, 2024
1 parent 7c84339 commit d87420d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/Tabs.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ export const Default = () => {
)
}

export const Children = () => {
return (
<Tabs defaultValue="leaderboard">
<Tabs.TabList>
<Tabs.Tab key="leaderboard" value="leaderboard">
<div className="italic">Leaderboard</div>
</Tabs.Tab>
<Tabs.Tab key="create" value="create">
<div className="font-bold">Child Content</div>
</Tabs.Tab>
</Tabs.TabList>
<Tabs.TabContent
key="leaderboard"
value="leaderboard"
className={{ root: 'md:px-4' }}
>
Course Tab
</Tabs.TabContent>
<Tabs.TabContent
key="create"
value="create"
className={{ root: 'md:px-4' }}
>
Create Tab
</Tabs.TabContent>
</Tabs>
)
}

export const Controlled = () => {
const [selectedTab, setSelectedTab] = useState('leaderboard')
return (
Expand Down
19 changes: 16 additions & 3 deletions src/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ interface TabProps {
}
key?: string
value: string
label: string
label?: string
children?: React.ReactNode
disabled?: boolean
className?: {
override?: string
Expand All @@ -20,6 +21,16 @@ interface TabProps {
}
}

interface TabPropsWithLabel extends TabProps {
label: string
children?: never
}

interface TabPropsWithChildren extends TabProps {
label?: never
children: React.ReactNode
}

/**
* This function returns a pre-styled Tab trigger component to be used inside a Tabs.Tablist.
* The value of this tab is required for both the internally and externally controlled state.
Expand All @@ -29,6 +40,7 @@ interface TabProps {
* @param key - The key of the tab.
* @param value - The value of the tab. This is required for the internal and external state.
* @param label - The label of the tab.
* @param children - A child component of the tab header, which can optionally replace the label
* @param disabled - The optional disabled property allows you to disable the tab.
* @param className - The optional className object allows you to override the default styling.
* @returns Tab trigger component
Expand All @@ -39,9 +51,10 @@ export function Tab({
key,
value,
label,
children,
disabled,
className,
}: TabProps) {
}: TabPropsWithLabel | TabPropsWithChildren) {
return (
<TabsPrimitive.Trigger
id={id}
Expand All @@ -64,7 +77,7 @@ export function Tab({
className?.label
)}
>
{label}
{label ?? children}
</span>
</TabsPrimitive.Trigger>
)
Expand Down

0 comments on commit d87420d

Please sign in to comment.