@@ -73,7 +73,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
7373 const childrenToProcess = (
7474 Array . isArray ( myChildren ) ? [ ...myChildren ] : [ myChildren ]
7575 ) as JSXNode [ ] ;
76- let tabListElement : JSXNode | undefined ;
76+ let tabListComponent : JSXNode | undefined ;
7777 const tabComponents : JSXNode [ ] = [ ] ;
7878 const panelComponents : JSXNode [ ] = [ ] ;
7979 const tabInfoList : TabInfo [ ] = [ ] ;
@@ -93,7 +93,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
9393
9494 switch ( child . type ) {
9595 case TabList : {
96- tabListElement = child ;
96+ tabListComponent = child ;
9797 const tabListChildren = Array . isArray ( child . props . children )
9898 ? child . props . children
9999 : [ child . props . children ] ;
@@ -103,7 +103,8 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
103103 }
104104 case Tab : {
105105 if ( child . props . selected ) {
106- selectedIndex = tabComponents . length ;
106+ const currentTabIndex = tabComponents . length ;
107+ selectedIndex = currentTabIndex ;
107108 child . props . selected = undefined ;
108109 }
109110 tabComponents . push ( child ) ;
@@ -112,7 +113,9 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
112113 case TabPanel : {
113114 const { label, selected } = child . props ;
114115 // The consumer must provide a key if they change the order
115- const tabIdFromTabMaybe = tabComponents [ panelIndex ] ?. key ;
116+ const matchedTabComponent = tabComponents [ panelIndex ] ;
117+ const tabIdFromTabMaybe =
118+ matchedTabComponent ?. props . tabId || matchedTabComponent ?. key ;
116119 const tabId = tabIdFromTabMaybe || child . key || `${ panelIndex } ` ;
117120
118121 if ( label ) {
@@ -159,12 +162,12 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
159162 tabInfoList [ index ] . tabProps = tab . props ;
160163 } ) ;
161164
162- if ( tabListElement ) {
163- tabListElement . children = tabComponents ;
164- tabListElement . props . children = tabComponents ;
165+ if ( tabListComponent ) {
166+ tabListComponent . children = tabComponents ;
167+ tabListComponent . props . children = tabComponents ;
165168 } else {
166169 // Creating it as <TabList /> and adding children later doesn't work
167- tabListElement = < TabList > { tabComponents } </ TabList > ;
170+ tabListComponent = < TabList > { tabComponents } </ TabList > ;
168171 }
169172
170173 if ( typeof selectedIndex === 'number' ) {
@@ -173,7 +176,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
173176
174177 return (
175178 < TabsImpl tabInfoList = { tabInfoList } { ...rest } >
176- { tabListElement }
179+ { tabListComponent }
177180 { panelComponents }
178181 </ TabsImpl >
179182 ) ;
0 commit comments