Skip to content

Commit 1505ddb

Browse files
authored
Fix multiple request examples selector not showing (#3039)
1 parent cd99ed5 commit 1505ddb

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

.changeset/mighty-rats-love.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
'gitbook': patch
4+
---
5+
6+
Fix multiple request examples selector not showing

packages/gitbook/src/components/DocumentView/OpenAPI/style.css

+1-5
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,9 @@
467467

468468
/* Common Elements */
469469
.openapi-select {
470-
@apply max-w-60 rounded font-mono text-xs leading-6 px-1 py-0.5 border border-tint-subtle bg-tint;
470+
@apply max-w-60 rounded font-mono text-xs leading-6 px-1 py-0.5 truncate border border-tint-subtle bg-tint;
471471
}
472472

473-
.openapi-select {
474-
min-width: fit-content;
475-
max-width: fit-content;
476-
}
477473
.openapi-select:focus {
478474
width: auto;
479475
}

packages/react-openapi/src/OpenAPICodeSample.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ function OpenAPICodeSampleFooter(props: {
195195
const { method, path } = data;
196196
const { specUrl } = context;
197197
const hideTryItPanel = data['x-hideTryItPanel'] || data.operation['x-hideTryItPanel'];
198-
const hasMultipleMediaTypes = renderers.length > 1;
198+
const hasMultipleMediaTypes =
199+
renderers.length > 1 || renderers.some((renderer) => renderer.examples.length > 0);
199200

200201
if (hideTryItPanel && !hasMultipleMediaTypes) {
201202
return null;

packages/react-openapi/src/OpenAPICodeSampleInteractive.tsx

+35-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import { useStore } from 'zustand';
55
import type { MediaTypeRenderer } from './OpenAPICodeSample';
66
import { getOrCreateTabStoreByKey } from './useSyncedTabsGlobalState';
77

8-
function useMediaTypeState(data: { method: string; path: string }, defaultKey: string) {
8+
type MediaTypeState = {
9+
mediaType: string;
10+
setMediaType: (mediaType: string) => void;
11+
};
12+
13+
function useMediaTypeState(
14+
data: { method: string; path: string },
15+
defaultKey: string
16+
): MediaTypeState {
917
const { method, path } = data;
1018
const store = useStore(getOrCreateTabStoreByKey(`media-type-${method}-${path}`, defaultKey));
1119
if (typeof store.tabKey !== 'string') {
@@ -45,22 +53,37 @@ export function OpenAPIMediaTypeExamplesSelector(props: {
4553

4654
return (
4755
<div className="openapi-codesample-selectors">
48-
<select
49-
className={clsx('openapi-select')}
50-
value={state.mediaType}
51-
onChange={(e) => state.setMediaType(e.target.value)}
52-
>
53-
{renderers.map((renderer) => (
54-
<option key={renderer.mediaType} value={renderer.mediaType}>
55-
{renderer.mediaType}
56-
</option>
57-
))}
58-
</select>
56+
<MediaTypeSelector state={state} renderers={renderers} />
5957
<ExamplesSelector method={method} path={path} renderer={selected} />
6058
</div>
6159
);
6260
}
6361

62+
function MediaTypeSelector(props: {
63+
state: MediaTypeState;
64+
renderers: MediaTypeRenderer[];
65+
}) {
66+
const { renderers, state } = props;
67+
68+
if (renderers.length < 2) {
69+
return null;
70+
}
71+
72+
return (
73+
<select
74+
className={clsx('openapi-select')}
75+
value={state.mediaType}
76+
onChange={(e) => state.setMediaType(e.target.value)}
77+
>
78+
{renderers.map((renderer) => (
79+
<option key={renderer.mediaType} value={renderer.mediaType}>
80+
{renderer.mediaType}
81+
</option>
82+
))}
83+
</select>
84+
);
85+
}
86+
6487
function ExamplesSelector(props: {
6588
method: string;
6689
path: string;

0 commit comments

Comments
 (0)