Skip to content

Commit

Permalink
fix(nx-dev): fix tabs logic (#28470)
Browse files Browse the repository at this point in the history
Fix tabs being hidden when selecting a tab in a different tab group
  • Loading branch information
isaacplmann authored Oct 16, 2024
1 parent 5dbea2e commit 4947159
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions nx-dev/ui-markdoc/src/lib/tags/tabs.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,35 @@ export function Tabs({
const [currentTab, setCurrentTab] = useState<string>(labels[0]);

useEffect(() => {
const handleTabSelectedEvent = (defaultTab?: string) => {
const handleTabSelectedEvent = (
tabSelectedEvent: CustomEvent<{ defaultTab?: string }>
) => {
const selectedTab = localStorage.getItem(SELECTED_TAB_KEY);
const defaultTab = tabSelectedEvent.detail.defaultTab;
if (selectedTab && labels.includes(selectedTab)) {
setCurrentTab(selectedTab);
} else if (defaultTab) {
setCurrentTab(defaultTab);
}
};

handleTabSelectedEvent(labels[0]);
window.addEventListener(TAB_SELECTED_EVENT, handleTabSelectedEvent);
handleTabSelectedEvent(
new CustomEvent(TAB_SELECTED_EVENT, { detail: { defaultTab: labels[0] } })
);
window.addEventListener(
TAB_SELECTED_EVENT,
handleTabSelectedEvent as EventListener
);
return () =>
window.removeEventListener(TAB_SELECTED_EVENT, handleTabSelectedEvent);
window.removeEventListener(
TAB_SELECTED_EVENT,
handleTabSelectedEvent as EventListener
);
}, [labels]);

const handleTabClick = (label: string) => {
localStorage.setItem(SELECTED_TAB_KEY, label);
window.dispatchEvent(new Event(TAB_SELECTED_EVENT));
window.dispatchEvent(new CustomEvent(TAB_SELECTED_EVENT, { detail: {} }));
setCurrentTab(label);
};

Expand Down

0 comments on commit 4947159

Please sign in to comment.