Skip to content

Fix multiple request examples selector not showing #3039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/mighty-rats-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@gitbook/react-openapi': patch
'gitbook': patch
---

Fix multiple request examples selector not showing
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,9 @@

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

.openapi-select {
min-width: fit-content;
max-width: fit-content;
}
.openapi-select:focus {
width: auto;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-openapi/src/OpenAPICodeSample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ function OpenAPICodeSampleFooter(props: {
const { method, path } = data;
const { specUrl } = context;
const hideTryItPanel = data['x-hideTryItPanel'] || data.operation['x-hideTryItPanel'];
const hasMultipleMediaTypes = renderers.length > 1;
const hasMultipleMediaTypes =
renderers.length > 1 || renderers.some((renderer) => renderer.examples.length > 0);

if (hideTryItPanel && !hasMultipleMediaTypes) {
return null;
Expand Down
47 changes: 35 additions & 12 deletions packages/react-openapi/src/OpenAPICodeSampleInteractive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import { useStore } from 'zustand';
import type { MediaTypeRenderer } from './OpenAPICodeSample';
import { getOrCreateTabStoreByKey } from './useSyncedTabsGlobalState';

function useMediaTypeState(data: { method: string; path: string }, defaultKey: string) {
type MediaTypeState = {
mediaType: string;
setMediaType: (mediaType: string) => void;
};

function useMediaTypeState(
data: { method: string; path: string },
defaultKey: string
): MediaTypeState {
const { method, path } = data;
const store = useStore(getOrCreateTabStoreByKey(`media-type-${method}-${path}`, defaultKey));
if (typeof store.tabKey !== 'string') {
Expand Down Expand Up @@ -45,22 +53,37 @@ export function OpenAPIMediaTypeExamplesSelector(props: {

return (
<div className="openapi-codesample-selectors">
<select
className={clsx('openapi-select')}
value={state.mediaType}
onChange={(e) => state.setMediaType(e.target.value)}
>
{renderers.map((renderer) => (
<option key={renderer.mediaType} value={renderer.mediaType}>
{renderer.mediaType}
</option>
))}
</select>
<MediaTypeSelector state={state} renderers={renderers} />
<ExamplesSelector method={method} path={path} renderer={selected} />
</div>
);
}

function MediaTypeSelector(props: {
state: MediaTypeState;
renderers: MediaTypeRenderer[];
}) {
const { renderers, state } = props;

if (renderers.length < 2) {
return null;
}

return (
<select
className={clsx('openapi-select')}
value={state.mediaType}
onChange={(e) => state.setMediaType(e.target.value)}
>
{renderers.map((renderer) => (
<option key={renderer.mediaType} value={renderer.mediaType}>
{renderer.mediaType}
</option>
))}
</select>
);
}

function ExamplesSelector(props: {
method: string;
path: string;
Expand Down
Loading