mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 18:31:06 +00:00
feat: add toHierarchy
utility for TreeView
, RecursiveList
(#2072)
Co-authored-by: Bram <bramhavers@gmail.com>
This commit is contained in:
parent
f1a27ec855
commit
48afd18e5e
19 changed files with 413 additions and 23 deletions
1
types/TreeView/index.d.ts
vendored
Normal file
1
types/TreeView/index.d.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export { default as TreeView } from "./TreeView.svelte";
|
1
types/index.d.ts
vendored
1
types/index.d.ts
vendored
|
@ -166,3 +166,4 @@ export { default as SkipToContent } from "./UIShell/SkipToContent.svelte";
|
|||
export { default as HeaderGlobalAction } from "./UIShell/HeaderGlobalAction.svelte";
|
||||
export { default as HeaderSearch } from "./UIShell/HeaderSearch.svelte";
|
||||
export { default as UnorderedList } from "./UnorderedList/UnorderedList.svelte";
|
||||
export { default as toHierarchy } from "./utils/toHierarchy";
|
||||
|
|
21
types/utils/toHierarchy.d.ts
vendored
Normal file
21
types/utils/toHierarchy.d.ts
vendored
Normal file
|
@ -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;
|
Loading…
Add table
Add a link
Reference in a new issue