Skip to content

Commit

Permalink
feat: Added new hook useTreeFlatten
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-crowell committed Jul 3, 2024
1 parent e92e794 commit 4cc33eb
Show file tree
Hide file tree
Showing 7 changed files with 1,853 additions and 1,792 deletions.
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,46 @@
"license": "MIT",
"devDependencies": {
"@do-ob/core": "^1.1.0",
"@do-ob/eslint-config": "^2.5.0",
"@do-ob/eslint-config": "^2.5.1",
"@do-ob/ts-config": "^2.0.0",
"@do-ob/vite-lib-config": "^3.0.1",
"@eslint/compat": "^1.1.0",
"@heroicons/react": "^2.1.3",
"@storybook/addon-a11y": "^8.2.0-alpha.10",
"@storybook/addon-essentials": "^8.2.0-alpha.10",
"@storybook/addon-interactions": "^8.2.0-alpha.10",
"@storybook/addon-links": "^8.2.0-alpha.10",
"@storybook/addon-themes": "^8.2.0-alpha.10",
"@storybook/blocks": "^8.2.0-alpha.10",
"@storybook/nextjs": "^8.2.0-alpha.10",
"@storybook/react": "^8.2.0-alpha.10",
"@storybook/test": "^8.2.0-alpha.10",
"@heroicons/react": "^2.1.4",
"@storybook/addon-a11y": "8.2.0-alpha.10",
"@storybook/addon-essentials": "8.2.0-alpha.10",
"@storybook/addon-interactions": "8.2.0-alpha.10",
"@storybook/addon-links": "8.2.0-alpha.10",
"@storybook/addon-themes": "8.2.0-alpha.10",
"@storybook/blocks": "8.2.0-alpha.10",
"@storybook/nextjs": "8.2.0-alpha.10",
"@storybook/react": "8.2.0-alpha.10",
"@storybook/test": "8.2.0-alpha.10",
"@tailwindcss/container-queries": "^0.1.1",
"@tailwindcss/typography": "^0.5.13",
"@types/node": "^20.12.12",
"@types/node": "^20.14.9",
"@types/react": "npm:types-react@rc",
"@types/react-dom": "npm:types-react@rc",
"@vitejs/plugin-react": "^4.3.0",
"@vitejs/plugin-react": "^4.3.1",
"autoprefixer": "^10.4.19",
"cssnano": "^7.0.2",
"eslint": "^9.2.0",
"framer-motion": "^11.2.10",
"cssnano": "^7.0.3",
"eslint": "^9.6.0",
"framer-motion": "^11.2.12",
"glob": "^10.4.2",
"next": "15.0.0-rc.0",
"postcss": "^8.4.38",
"postcss": "^8.4.39",
"react": "19.0.0-rc-8971381549-20240625",
"react-aria": "nightly",
"react-aria-components": "nightly",
"react-dom": "19.0.0-rc-8971381549-20240625",
"react-stately": "nightly",
"storybook": "^8.2.0-alpha.10",
"storybook": "8.2.0-alpha.10",
"tailwind-merge": "^2.3.0",
"tailwindcss": "^3.4.3",
"tailwindcss": "^3.4.4",
"tailwindcss-animate": "^1.0.7",
"tailwindcss-react-aria-components": "^1.1.3",
"types-react": "19.0.0-rc.1",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"typescript": "^5.5.3",
"vite": "^5.3.3",
"vitest": "^1.6.0"
},
"eslintConfig": {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './hooks/useDialogControlButtonProps';
export * from './hooks/useDialogControl';
export * from './hooks/useDialogRegister';
export * from './hooks/useOutsidePress';
export * from './hooks/useTreeFlatten';
34 changes: 34 additions & 0 deletions packages/ui/src/hooks/useTreeFlatten.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useMemo } from 'react';

type Flattened<T extends object> = T & { level: number };

function flattenTree<T extends object>(
tree: T[],
getChildren: (node: T) => T[] | undefined,
level: number = 0
): Flattened<T>[] {
console.log('CALLED');
return tree.reduce((acc: Flattened<T>[], node: T) => {
const children = getChildren(node);
acc.push({ ...node, level });

if (children && children.length > 0) {
acc.push(...flattenTree(children, getChildren, level + 1));
}

return acc;
}, []);
}


export function useTreeFlatten<T extends object>(
tree: T[],
getChildren: (node: T) => T[] | undefined
): Flattened<T>[] {

const flattenedTree = useMemo(() => (
flattenTree(tree, getChildren)
), [ tree, getChildren ]);

return flattenedTree;
}
43 changes: 43 additions & 0 deletions packages/ui/src/widgets/Navigation/NavigationMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useListData } from 'react-stately';
import { NavigationProps } from './Navigation.types';
import { Tabs, TabList, Tab } from 'react-aria-components';
import { useTreeFlatten } from '@do-ob/ui/hooks';

export function NavigationMenu({
base,
}: { base: NavigationProps }) {

const {
links = [],
} = base;

const linksFlat = useTreeFlatten(
links,
(item) => item.links,
);

const linkList = useListData({
initialItems: linksFlat,
getKey: (item) => item.url,
});

console.log({
linksFlat,
linkList: linkList.items
});

return (
<Tabs
orientation="vertical"
keyboardActivation="manual"
>
<TabList
items={linkList.items}
>
{(link) => (
<Tab id={link.url}>{link.title}</Tab>
)}
</TabList>
</Tabs>
);
}
12 changes: 10 additions & 2 deletions packages/ui/src/widgets/Navigation/NavigationPopovers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Popover } from '@do-ob/ui/components';
import { NavigationProps } from './Navigation.types';
import { NavigationMenu } from './NavigationMenu';

export function NavigationPopovers({
base,
Expand All @@ -10,16 +11,23 @@ export function NavigationPopovers({
links = [],
} = base;

const filteredLinks = links.filter((link) => link.links && link.links.length > 0);

return (
<>
{links.map((link) => (
{filteredLinks.map((link) => (
<Popover
key={link.url}
id={`${id}-${link.url}`}
placement="bottom"
nonmodal
>
<h2>{link.title}</h2>
<NavigationMenu
base={{
...base,
links: [ link, ...link.links ?? [] ],
}}
/>
</Popover>
))}
</>
Expand Down
8 changes: 0 additions & 8 deletions packages/ui/src/widgets/Navigation/NavigationTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ export function NavigationTabs({
if(orientation === 'horizontal') {
const selectedLink = linkTree.getItem(key);

console.log({
selectedLink,
tabRefs,
ref: tabRefs[selectedLink.value.url].current
});

console.log(selectedLink.value.url);

// If the selected link has sub-items, open a dialog.
if(selectedLink.children.length > 0) {
dispatch(dialogActions.open(
Expand Down
Loading

0 comments on commit 4cc33eb

Please sign in to comment.