@@ -73,7 +73,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
73
73
const childrenToProcess = (
74
74
Array . isArray ( myChildren ) ? [ ...myChildren ] : [ myChildren ]
75
75
) as JSXNode [ ] ;
76
- let tabListElement : JSXNode | undefined ;
76
+ let tabListComponent : JSXNode | undefined ;
77
77
const tabComponents : JSXNode [ ] = [ ] ;
78
78
const panelComponents : JSXNode [ ] = [ ] ;
79
79
const tabInfoList : TabInfo [ ] = [ ] ;
@@ -93,7 +93,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
93
93
94
94
switch ( child . type ) {
95
95
case TabList : {
96
- tabListElement = child ;
96
+ tabListComponent = child ;
97
97
const tabListChildren = Array . isArray ( child . props . children )
98
98
? child . props . children
99
99
: [ child . props . children ] ;
@@ -103,7 +103,8 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
103
103
}
104
104
case Tab : {
105
105
if ( child . props . selected ) {
106
- selectedIndex = tabComponents . length ;
106
+ const currentTabIndex = tabComponents . length ;
107
+ selectedIndex = currentTabIndex ;
107
108
child . props . selected = undefined ;
108
109
}
109
110
tabComponents . push ( child ) ;
@@ -112,7 +113,9 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
112
113
case TabPanel : {
113
114
const { label, selected } = child . props ;
114
115
// 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 ;
116
119
const tabId = tabIdFromTabMaybe || child . key || `${ panelIndex } ` ;
117
120
118
121
if ( label ) {
@@ -159,12 +162,12 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
159
162
tabInfoList [ index ] . tabProps = tab . props ;
160
163
} ) ;
161
164
162
- if ( tabListElement ) {
163
- tabListElement . children = tabComponents ;
164
- tabListElement . props . children = tabComponents ;
165
+ if ( tabListComponent ) {
166
+ tabListComponent . children = tabComponents ;
167
+ tabListComponent . props . children = tabComponents ;
165
168
} else {
166
169
// Creating it as <TabList /> and adding children later doesn't work
167
- tabListElement = < TabList > { tabComponents } </ TabList > ;
170
+ tabListComponent = < TabList > { tabComponents } </ TabList > ;
168
171
}
169
172
170
173
if ( typeof selectedIndex === 'number' ) {
@@ -173,7 +176,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
173
176
174
177
return (
175
178
< TabsImpl tabInfoList = { tabInfoList } { ...rest } >
176
- { tabListElement }
179
+ { tabListComponent }
177
180
{ panelComponents }
178
181
</ TabsImpl >
179
182
) ;
0 commit comments