Skip to content

Fix OpenAPI tabs selection #2914

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 1 commit into from
Mar 3, 2025
Merged
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
17 changes: 14 additions & 3 deletions packages/react-openapi/src/OpenAPITabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { type Key, Tab, TabList, TabPanel, Tabs, type TabsProps } from 'react-aria-components';
import { useEventCallback } from 'usehooks-ts';
import { Markdown } from './Markdown';
import { getOrCreateTabStoreByKey } from './useSyncedTabsGlobalState';

Expand Down Expand Up @@ -44,6 +45,16 @@ export function OpenAPITabs(
}
return items[0]?.key ?? null;
});
const selectTab = useEventCallback((key: Key | null) => {
if (!key || key === tabKey) {
return;
}
const tab = items.find((item) => item.key === key);
if (!tab) {
return;
}
setTabKey(key);
});
const selectedTab = items.find((item) => item.key === tabKey) ?? items[0] ?? null;
const cancelDeferRef = useRef<(() => void) | null>(null);
useEffect(() => {
Expand All @@ -53,9 +64,9 @@ export function OpenAPITabs(
const store = getOrCreateTabStoreByKey(stateKey);
return store.subscribe((state) => {
cancelDeferRef.current?.();
cancelDeferRef.current = defer(() => setTabKey(state.tabKey));
cancelDeferRef.current = defer(() => selectTab(state.tabKey));
});
}, [stateKey]);
}, [stateKey, selectTab]);
useEffect(() => {
return () => cancelDeferRef.current?.();
}, []);
Expand All @@ -65,7 +76,7 @@ export function OpenAPITabs(
<Tabs
className="openapi-tabs"
onSelectionChange={(tabKey) => {
setTabKey(tabKey);
selectTab(tabKey);
if (stateKey) {
const store = getOrCreateTabStoreByKey(stateKey);
store.setState({ tabKey });
Expand Down
Loading