Skip to content

Commit

Permalink
fix: workato accordion groups (#1300)
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava authored Aug 14, 2024
1 parent 9bc0ce0 commit 8207b04
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 92 deletions.
2 changes: 2 additions & 0 deletions packages/ui/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"unist-util-visit": "^5.0.0",
"url-join": "5.0.0",
"use-memo-one": "^1.1.3",
"uuid": "^9.0.0",
"vfile": "^6.0.1",
"zod": "^3.23.8"
},
Expand Down Expand Up @@ -161,6 +162,7 @@
"@types/react-dom": "^18.2.18",
"@types/react-test-renderer": "^18.0.7",
"@types/tinycolor2": "^1.4.6",
"@types/uuid": "^9.0.1",
"@vitejs/plugin-react": "^4.2.1",
"autoprefixer": "^10.4.16",
"chromatic": "^11.3.0",
Expand Down
11 changes: 7 additions & 4 deletions packages/ui/app/src/mdx/components/accordion/AccordionGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NavArrowRight } from "iconoir-react";
import { useAtom } from "jotai";
import { NextRouter } from "next/router";
import { FC, ReactNode, useCallback, useEffect, useState } from "react";
import { v4 } from "uuid";
import { ANCHOR_ATOM } from "../../../atoms";

export interface AccordionItemProps {
Expand All @@ -16,19 +17,21 @@ export interface AccordionGroupProps {
items: AccordionItemProps[];
router: NextRouter;
toc?: boolean;
key: string;
}

export const AccordionGroup: FC<AccordionGroupProps> = ({ items = [], toc: parentToc = true }) => {
const [key, _setKey] = useState(v4());
const [activeTabs, setActiveTabs] = useState<string[]>([]);
const [anchor, setAnchor] = useAtom(ANCHOR_ATOM);
useEffect(() => {
if (anchor != null) {
const anchorTab = items.findIndex((tab) => slug(tab.title) === anchor);
const anchorTab = items.findIndex((tab) => slug(`${tab.title}-${key}`) === anchor);
if (anchorTab >= 0) {
setActiveTabs((prev) => (prev.includes(anchorTab.toString()) ? prev : [...prev, anchorTab.toString()]));
}
}
}, [anchor, items]);
}, [anchor, items, key]);

const handleValueChange = useCallback(
(nextActiveTabs: string[]) => {
Expand All @@ -37,13 +40,13 @@ export const AccordionGroup: FC<AccordionGroupProps> = ({ items = [], toc: paren
if (added[0] != null) {
const addedItem = items[parseInt(added[0])];
if (addedItem != null) {
setAnchor(slug(addedItem.title));
setAnchor(slug(`${addedItem.title}-${key}`));
}
}
return nextActiveTabs;
});
},
[items, setAnchor],
[items, setAnchor, key],
);

return (
Expand Down
100 changes: 12 additions & 88 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8207b04

Please sign in to comment.