Skip to content

Commit 0426312

Browse files
authored
Reset to first tab when tabs change in an InteractiveSection (#2407)
1 parent ede2335 commit 0426312

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
---
4+
5+
Fix tabs being empty for code samples when they are updated dynamically

packages/react-openapi/src/InteractiveSection.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import classNames from 'classnames';
44
import React from 'react';
55

6+
interface InteractiveSectionTab {
7+
key: string;
8+
label: string;
9+
body: React.ReactNode;
10+
}
11+
612
/**
713
* To optimize rendering, most of the components are server-components,
814
* and the interactiveness is mainly handled by a few key components like this one.
@@ -19,11 +25,7 @@ export function InteractiveSection(props: {
1925
toggleOpenIcon?: React.ReactNode;
2026
toggleCloseIcon?: React.ReactNode;
2127
/** Tabs of content to display */
22-
tabs?: Array<{
23-
key: string;
24-
label: string;
25-
body: React.ReactNode;
26-
}>;
28+
tabs?: Array<InteractiveSectionTab>;
2729
/** Default tab to have opened */
2830
defaultTab?: string;
2931
/** Content of the header */
@@ -48,9 +50,9 @@ export function InteractiveSection(props: {
4850
} = props;
4951

5052
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];
5456

5557
return (
5658
<div
@@ -94,7 +96,7 @@ export function InteractiveSection(props: {
9496
'openapi-select',
9597
`${className}-tabs-select`,
9698
)}
97-
value={selectedTab}
99+
value={selectedTab.key}
98100
onChange={(event) => {
99101
setSelectedTab(event.target.value);
100102
setOpened(true);
@@ -107,7 +109,7 @@ export function InteractiveSection(props: {
107109
))}
108110
</select>
109111
) : null}
110-
{(children || tabBody) && toggeable ? (
112+
{(children || selectedTab?.body) && toggeable ? (
111113
<button
112114
className={classNames('openapi-section-toggle', `${className}-toggle`)}
113115
onClick={() => setOpened(!opened)}
@@ -117,10 +119,10 @@ export function InteractiveSection(props: {
117119
) : null}
118120
</div>
119121
</div>
120-
{(!toggeable || opened) && (children || tabBody) ? (
122+
{(!toggeable || opened) && (children || selectedTab?.body) ? (
121123
<div className={classNames('openapi-section-body', `${className}-body`)}>
122124
{children}
123-
{tabBody}
125+
{selectedTab?.body}
124126
</div>
125127
) : null}
126128
{overlay}

0 commit comments

Comments
 (0)