From 02bc672248fb6effa3a99a4ca4a9e1cb7189de85 Mon Sep 17 00:00:00 2001 From: Mikyo King Date: Tue, 11 Jun 2024 11:58:52 -0600 Subject: [PATCH] feat(tabs): add a default index --- src/tabs/Tabs.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/tabs/Tabs.tsx b/src/tabs/Tabs.tsx index 501d73e..0fb128a 100644 --- a/src/tabs/Tabs.tsx +++ b/src/tabs/Tabs.tsx @@ -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 @@ -132,6 +136,7 @@ export function Tabs({ children, className, index, + defaultIndex, onChange, orientation = 'horizontal', extra, @@ -139,9 +144,12 @@ export function Tabs({ // 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,