Skip to content

Commit

Permalink
allow overriding the selectedTab on Tabs component
Browse files Browse the repository at this point in the history
  • Loading branch information
tbehailu committed Jan 22, 2024
1 parent adb81aa commit 49fd73e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ function parseTabList(children: ReactNode): Tab[] {
export type TabsProps = {
children: ReactNode[];
className?: string;
/**
* If specified, the index of the selected tab is controlled by the parent component rather than the internal state.
*/
selectedTabIndex?: number;
onChange?: (index: number) => void;
/**
* The orientation of the tabs. Defaults to horizontal
Expand All @@ -127,6 +131,7 @@ export type TabsProps = {
export function Tabs({
children,
className,
selectedTabIndex,
onChange,
orientation = 'horizontal',
extra,
Expand All @@ -135,9 +140,11 @@ export function Tabs({
children = Children.toArray(children).filter(child => child);
const tabs = parseTabList(children);
// Initialize the selected tab to the first non-hidden tab
const [selectedIndex, setSelectedIndex] = useState<number>(
const [selectedStateIndex, setSelectedIndex] = useState<number>(
tabs.findIndex(tab => !tab.hidden)
);
const selectedIndex =
typeof selectedTabIndex == "number" ? selectedTabIndex : selectedStateIndex;
return (
<div
className={`ac-tabs ${className}`}
Expand Down

0 comments on commit 49fd73e

Please sign in to comment.