Skip to content

Commit

Permalink
Sort NavigationTree alphabetically (#112)
Browse files Browse the repository at this point in the history
* add tree sorting func

* add accordion

Co-authored-by: Preston Bourne <[email protected]>
  • Loading branch information
prestonbourne and prestonbourne authored Jun 21, 2023
1 parent 7d3935f commit 15e6f2c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/basic/components/accordion/docs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: 'Accordion'
description: 'UI component named after a funky instrument 🪗'
path: 'Content/Accordion'
---
import {Accordion} from '.'

<Accordion/>
15 changes: 15 additions & 0 deletions examples/basic/components/accordion/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ReactElement } from 'react'

interface AccordionProps {
color?: string
children: ReactElement
}

export function Accordion({ children }: AccordionProps) {
return (
<details>
<summary>Hello 👋</summary>
Nice meeting you! How's everything going?
</details>
)
}
33 changes: 33 additions & 0 deletions packages/swingset/src/get-navigation-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ComponentNode,
CategoryNode,
NavigationTree,
NavigationNode,
} from './types.js'

export function getNavigationTree(
Expand Down Expand Up @@ -83,5 +84,37 @@ export function getNavigationTree(
}

const tree = Array.from(categories.values())

deepSort(tree)

return tree
}

/**
*
* Sorts tree alphabetically
*/
function deepSort(nodes: (CategoryNode | NavigationNode)[]) {
nodes.sort(compareTitleSort)

for (const node of nodes) {
const hasChildren = 'children' in node

if (hasChildren) {
deepSort(node.children)
}
}
}

function compareTitleSort<T extends CategoryNode | NavigationNode>(
a: T,
b: T
): -1 | 0 | 1 {
if (a.title > b.title) {
return 1
}
if (b.title > a.title) {
return -1
}
return 0
}

0 comments on commit 15e6f2c

Please sign in to comment.