-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
29 changed files
with
414 additions
and
273 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
docs/src/pages/framed/RecursiveList/RecursiveListFlatArray.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script> | ||
import { RecursiveList, toHierarchy } from "carbon-components-svelte"; | ||
const nodesFlat = [ | ||
{ id: 1, text: "Item 1" }, | ||
{ id: 2, text: "Item 1a", pid: 1 }, | ||
{ id: 3, html: "<h5>HTML content</h5>", pid: 2 }, | ||
{ id: 4, text: "Item 2" }, | ||
{ id: 5, href: "https://svelte.dev/", pid: 4 }, | ||
{ | ||
id: 6, | ||
href: "https://svelte.dev/", | ||
text: "Link with custom text", | ||
pid: 4, | ||
}, | ||
{ id: 7, text: "Item 3" }, | ||
]; | ||
</script> | ||
|
||
<RecursiveList nodes={toHierarchy(nodesFlat, (node) => node.pid)} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,28 @@ | ||
<script> | ||
import { TreeView, toHierarchy } from "carbon-components-svelte"; | ||
import WatsonMachineLearning from "carbon-icons-svelte/lib/WatsonMachineLearning.svelte"; | ||
import Analytics from "carbon-icons-svelte/lib/Analytics.svelte"; | ||
import Blockchain from "carbon-icons-svelte/lib/Blockchain.svelte"; | ||
import DataBase from "carbon-icons-svelte/lib/DataBase.svelte"; | ||
import SignalStrength from "carbon-icons-svelte/lib/SignalStrength.svelte"; | ||
let activeId = ""; | ||
let selectedIds = []; | ||
let nodesFlat = [ | ||
{ id: 0, text: "AI / Machine learning", icon: WatsonMachineLearning }, | ||
{ id: 1, text: "Analytics", icon: Analytics }, | ||
{ id: 2, text: "IBM Analytics Engine", pid: 1, icon: Analytics }, | ||
{ id: 3, text: "Apache Spark", pid: 2, icon: Analytics }, | ||
{ id: 4, text: "Hadoop", icon: Analytics, pid: 2 }, | ||
{ id: 5, text: "IBM Cloud SQL Query", icon: Analytics, pid: 1 }, | ||
{ id: 6, text: "IBM Db2 Warehouse on Cloud", icon: Analytics, pid: 1 }, | ||
{ id: 7, text: "Blockchain", icon: Blockchain }, | ||
{ id: 8, text: "IBM Blockchain Platform", icon: Blockchain, pid: 7 }, | ||
{ id: 9, text: "Databases", icon: DataBase }, | ||
{ | ||
id: 10, | ||
text: "IBM Cloud Databases for Elasticsearch", | ||
icon: DataBase, | ||
pid: 9, | ||
}, | ||
{ | ||
id: 11, | ||
text: "IBM Cloud Databases for Enterprise DB", | ||
icon: DataBase, | ||
pid: 9, | ||
}, | ||
{ id: 12, text: "IBM Cloud Databases for MongoDB", icon: DataBase, pid: 9 }, | ||
{ | ||
id: 13, | ||
text: "IBM Cloud Databases for PostgreSQL", | ||
icon: DataBase, | ||
pid: 9, | ||
}, | ||
{ id: 14, text: "Integration", icon: SignalStrength, disabled: true }, | ||
{ | ||
id: 15, | ||
text: "IBM API Connect", | ||
icon: SignalStrength, | ||
disabled: true, | ||
pid: 14, | ||
}, | ||
{ id: 0, text: "AI / Machine learning", icon: Analytics }, | ||
{ id: 1, text: "Analytics" }, | ||
{ id: 2, text: "IBM Analytics Engine", pid: 1 }, | ||
{ id: 3, text: "Apache Spark", pid: 2 }, | ||
{ id: 4, text: "Hadoop", pid: 2 }, | ||
{ id: 5, text: "IBM Cloud SQL Query", pid: 1 }, | ||
{ id: 6, text: "IBM Db2 Warehouse on Cloud", pid: 1 }, | ||
{ id: 7, text: "Blockchain" }, | ||
{ id: 8, text: "IBM Blockchain Platform", pid: 7 }, | ||
{ id: 9, text: "Databases" }, | ||
{ id: 10, text: "IBM Cloud Databases for Elasticsearch", pid: 9 }, | ||
{ id: 11, text: "IBM Cloud Databases for Enterprise DB", pid: 9 }, | ||
{ id: 12, text: "IBM Cloud Databases for MongoDB", pid: 9 }, | ||
{ id: 13, text: "IBM Cloud Databases for PostgreSQL", pid: 9 }, | ||
{ id: 14, text: "Integration", disabled: true }, | ||
{ id: 15, text: "IBM API Connect", disabled: true, pid: 14 }, | ||
]; | ||
</script> | ||
|
||
<TreeView | ||
labelText="Cloud Products" | ||
nodes={toHierarchy(nodesFlat)} | ||
bind:activeId | ||
bind:selectedIds | ||
on:select={({ detail }) => console.log("select", detail)} | ||
on:toggle={({ detail }) => console.log("toggle", detail)} | ||
on:focus={({ detail }) => console.log("focus", detail)} | ||
nodes={toHierarchy(nodesFlat, (node) => node.pid)} | ||
/> | ||
|
||
<div>Active node id: {activeId}</div> | ||
<div>Selected ids: {JSON.stringify(selectedIds)}</div> | ||
|
||
<style> | ||
div { | ||
margin-top: var(--cds-spacing-05); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { default as TreeView } from "./TreeView.svelte"; | ||
export { toHierarchy } from "./treeview"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { default as TreeView } from "./TreeView.svelte"; | ||
export { toHierarchy } from "./treeview"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
type NodeLike = { | ||
id: string | number; | ||
nodes?: NodeLike[]; | ||
[key: string]: any; | ||
}; | ||
|
||
/** Create a hierarchical tree from a flat array. */ | ||
export function toHierarchy< | ||
T extends NodeLike, | ||
K extends keyof Omit<T, "id" | "nodes">, | ||
>( | ||
flatArray: T[] | readonly T[], | ||
/** | ||
* Function that returns the parent ID for a given node. | ||
* @example | ||
* toHierarchy(flatArray, (node) => node.parentId); | ||
*/ | ||
getParentId: (node: T) => T[K] | null, | ||
): (T & { nodes?: (T & { nodes?: T[] })[] })[]; | ||
|
||
export default toHierarchy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// @ts-check | ||
/** | ||
* Create a nested array from a flat array. | ||
* @typedef {Object} NodeLike | ||
* @property {string | number} id - Unique identifier for the node | ||
* @property {NodeLike[]} [nodes] - Optional array of child nodes | ||
* @property {Record<string, any>} [additionalProperties] - Any additional properties | ||
* | ||
* @param {NodeLike[]} flatArray - Array of flat nodes to convert | ||
* @param {function(NodeLike): (string|number|null)} getParentId - Function to get parent ID for a node | ||
* @returns {NodeLike[]} Hierarchical tree structure | ||
*/ | ||
export function toHierarchy(flatArray, getParentId) { | ||
/** @type {NodeLike[]} */ | ||
const tree = []; | ||
const childrenOf = new Map(); | ||
const itemsMap = new Map(flatArray.map((item) => [item.id, item])); | ||
|
||
flatArray.forEach((item) => { | ||
const parentId = getParentId(item); | ||
|
||
// Only create nodes array if we have children. | ||
const children = childrenOf.get(item.id); | ||
if (children) { | ||
item.nodes = children; | ||
} | ||
|
||
// Check if parentId exists using Map instead of array lookup. | ||
const parentExists = parentId && itemsMap.has(parentId); | ||
|
||
if (parentId && parentExists) { | ||
if (!childrenOf.has(parentId)) { | ||
childrenOf.set(parentId, []); | ||
} | ||
childrenOf.get(parentId).push(item); | ||
|
||
const parent = itemsMap.get(parentId); | ||
if (parent) { | ||
parent.nodes = childrenOf.get(parentId); | ||
} | ||
} else { | ||
tree.push(item); | ||
} | ||
}); | ||
|
||
return tree; | ||
} | ||
|
||
export default toHierarchy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.