Skip to content

Commit

Permalink
feat(tabs): add a default index
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeldking committed Jun 11, 2024
1 parent 14016e7 commit 02bc672
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export type TabsProps = {
* If specified, the index of the selected tab is controlled by the parent component rather than the internal state.
*/
index?: number;
/**
* default index of the selected tab.
*/
defaultIndex?: number;
onChange?: (index: number) => void;
/**
* The orientation of the tabs. Defaults to horizontal
Expand All @@ -132,16 +136,20 @@ export function Tabs({
children,
className,
index,
defaultIndex,
onChange,
orientation = 'horizontal',
extra,
}: TabsProps) {
// Filter out the nulls from the children so that tabs can be mounted conditionally
children = Children.toArray(children).filter(child => child);
const tabs = parseTabList(children);

// Initialize the selected tab to the first non-hidden tab if there is no controlled value provided
const defaultValue = tabs.findIndex(tab => !tab.hidden);
const defaultValue =
typeof defaultIndex === 'number'
? defaultIndex
: tabs.findIndex(tab => !tab.hidden);
const [selectedIndex, setSelectedIndex] = useControlledState(
index,
defaultValue,
Expand Down

0 comments on commit 02bc672

Please sign in to comment.