Re-work toHierarchy utility

Refactor `toHiearchy` to be more generic, performant

- Use callback to "pick" generic parent ID property instead of requiring that `pid` be hardcoded`
- Account for edge cases of an invalid parent ID
- Use Map to store node children for lookups
- Use one pass instead of removing empty nodes at the very end
- DX: use generics to type `toHierarchy`
- Make `toHierarchy` even more generic (reusable with `RecursiveList`)

Co-Authored-By: Bram <bramhavers@gmail.com>
This commit is contained in:
Eric Liu 2024-12-07 13:54:06 -08:00
commit 5f1e8de1e1
29 changed files with 414 additions and 273 deletions

View file

@ -0,0 +1,28 @@
<script lang="ts">
import { RecursiveList } from "carbon-components-svelte";
const nodes = [
{
text: "Item 1",
nodes: [
{
text: "Item 1a",
nodes: [{ html: "<h5>HTML content</h5>" }],
},
],
},
{
text: "Item 2",
nodes: [
{ href: "https://svelte.dev/" },
{
href: "https://svelte.dev/",
text: "Link with custom text",
},
],
},
{ text: "Item 3" },
];
</script>
<RecursiveList type="ordered" {nodes} />