3
3
import classNames from 'classnames' ;
4
4
import React from 'react' ;
5
5
6
+ interface InteractiveSectionTab {
7
+ key : string ;
8
+ label : string ;
9
+ body : React . ReactNode ;
10
+ }
11
+
6
12
/**
7
13
* To optimize rendering, most of the components are server-components,
8
14
* and the interactiveness is mainly handled by a few key components like this one.
@@ -19,11 +25,7 @@ export function InteractiveSection(props: {
19
25
toggleOpenIcon ?: React . ReactNode ;
20
26
toggleCloseIcon ?: React . ReactNode ;
21
27
/** Tabs of content to display */
22
- tabs ?: Array < {
23
- key : string ;
24
- label : string ;
25
- body : React . ReactNode ;
26
- } > ;
28
+ tabs ?: Array < InteractiveSectionTab > ;
27
29
/** Default tab to have opened */
28
30
defaultTab ?: string ;
29
31
/** Content of the header */
@@ -48,9 +50,9 @@ export function InteractiveSection(props: {
48
50
} = props ;
49
51
50
52
const [ opened , setOpened ] = React . useState ( defaultOpened ) ;
51
- const [ selectedTab , setSelectedTab ] = React . useState ( defaultTab ) ;
52
-
53
- const tabBody = tabs . find ( ( tab ) => tab . key === selectedTab ) ?. body ;
53
+ const [ selectedTabKey , setSelectedTab ] = React . useState ( defaultTab ) ;
54
+ const selectedTab : InteractiveSectionTab | undefined =
55
+ tabs . find ( ( tab ) => tab . key === selectedTabKey ) ?? tabs [ 0 ] ;
54
56
55
57
return (
56
58
< div
@@ -94,7 +96,7 @@ export function InteractiveSection(props: {
94
96
'openapi-select' ,
95
97
`${ className } -tabs-select` ,
96
98
) }
97
- value = { selectedTab }
99
+ value = { selectedTab . key }
98
100
onChange = { ( event ) => {
99
101
setSelectedTab ( event . target . value ) ;
100
102
setOpened ( true ) ;
@@ -107,7 +109,7 @@ export function InteractiveSection(props: {
107
109
) ) }
108
110
</ select >
109
111
) : null }
110
- { ( children || tabBody ) && toggeable ? (
112
+ { ( children || selectedTab ?. body ) && toggeable ? (
111
113
< button
112
114
className = { classNames ( 'openapi-section-toggle' , `${ className } -toggle` ) }
113
115
onClick = { ( ) => setOpened ( ! opened ) }
@@ -117,10 +119,10 @@ export function InteractiveSection(props: {
117
119
) : null }
118
120
</ div >
119
121
</ div >
120
- { ( ! toggeable || opened ) && ( children || tabBody ) ? (
122
+ { ( ! toggeable || opened ) && ( children || selectedTab ?. body ) ? (
121
123
< div className = { classNames ( 'openapi-section-body' , `${ className } -body` ) } >
122
124
{ children }
123
- { tabBody }
125
+ { selectedTab ?. body }
124
126
</ div >
125
127
) : null }
126
128
{ overlay }
0 commit comments