carbon-components-svelte 0.86.2 → 0.87.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/TooltipIcon/TooltipIcon.svelte +1 -0
- package/src/TreeView/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/utils/toHierarchy.d.ts +21 -0
- package/src/utils/toHierarchy.js +49 -0
- package/types/TreeView/index.d.ts +1 -0
- package/types/index.d.ts +1 -0
- package/types/utils/toHierarchy.d.ts +21 -0
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ The Carbon Svelte portfolio also includes:
|
|
|
15
15
|
|
|
16
16
|
- **[Carbon Icons Svelte](https://github.com/carbon-design-system/carbon-icons-svelte)**: 2,400+ Carbon icons as Svelte components
|
|
17
17
|
- **[Carbon Pictograms Svelte](https://github.com/carbon-design-system/carbon-pictograms-svelte)**: 1,100+ Carbon pictograms as Svelte components
|
|
18
|
-
- **[Carbon Charts Svelte](https://github.com/carbon-design-system/carbon-charts/tree/master/packages/svelte)**:
|
|
18
|
+
- **[Carbon Charts Svelte](https://github.com/carbon-design-system/carbon-charts/tree/master/packages/svelte)**: 25+ charts, powered by d3
|
|
19
19
|
- **[Carbon Preprocess Svelte](https://github.com/carbon-design-system/carbon-preprocess-svelte)**: Collection of Svelte preprocessors for Carbon
|
|
20
20
|
|
|
21
21
|
## [Documentation](https://svelte.carbondesignsystem.com)
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as TreeView } from "./TreeView.svelte";
|
package/src/index.js
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type NodeLike = {
|
|
2
|
+
id: string | number;
|
|
3
|
+
nodes?: NodeLike[];
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/** Create a hierarchical tree from a flat array. */
|
|
8
|
+
export function toHierarchy<
|
|
9
|
+
T extends NodeLike,
|
|
10
|
+
K extends keyof Omit<T, "id" | "nodes">,
|
|
11
|
+
>(
|
|
12
|
+
flatArray: T[] | readonly T[],
|
|
13
|
+
/**
|
|
14
|
+
* Function that returns the parent ID for a given node.
|
|
15
|
+
* @example
|
|
16
|
+
* toHierarchy(flatArray, (node) => node.parentId);
|
|
17
|
+
*/
|
|
18
|
+
getParentId: (node: T) => T[K] | null,
|
|
19
|
+
): (T & { nodes?: (T & { nodes?: T[] })[] })[];
|
|
20
|
+
|
|
21
|
+
export default toHierarchy;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Create a nested array from a flat array.
|
|
4
|
+
* @typedef {Object} NodeLike
|
|
5
|
+
* @property {string | number} id - Unique identifier for the node
|
|
6
|
+
* @property {NodeLike[]} [nodes] - Optional array of child nodes
|
|
7
|
+
* @property {Record<string, any>} [additionalProperties] - Any additional properties
|
|
8
|
+
*
|
|
9
|
+
* @param {NodeLike[]} flatArray - Array of flat nodes to convert
|
|
10
|
+
* @param {function(NodeLike): (string|number|null)} getParentId - Function to get parent ID for a node
|
|
11
|
+
* @returns {NodeLike[]} Hierarchical tree structure
|
|
12
|
+
*/
|
|
13
|
+
export function toHierarchy(flatArray, getParentId) {
|
|
14
|
+
/** @type {NodeLike[]} */
|
|
15
|
+
const tree = [];
|
|
16
|
+
const childrenOf = new Map();
|
|
17
|
+
const itemsMap = new Map(flatArray.map((item) => [item.id, item]));
|
|
18
|
+
|
|
19
|
+
flatArray.forEach((item) => {
|
|
20
|
+
const parentId = getParentId(item);
|
|
21
|
+
|
|
22
|
+
// Only create nodes array if we have children.
|
|
23
|
+
const children = childrenOf.get(item.id);
|
|
24
|
+
if (children) {
|
|
25
|
+
item.nodes = children;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Check if parentId exists using Map instead of array lookup.
|
|
29
|
+
const parentExists = parentId && itemsMap.has(parentId);
|
|
30
|
+
|
|
31
|
+
if (parentId && parentExists) {
|
|
32
|
+
if (!childrenOf.has(parentId)) {
|
|
33
|
+
childrenOf.set(parentId, []);
|
|
34
|
+
}
|
|
35
|
+
childrenOf.get(parentId).push(item);
|
|
36
|
+
|
|
37
|
+
const parent = itemsMap.get(parentId);
|
|
38
|
+
if (parent) {
|
|
39
|
+
parent.nodes = childrenOf.get(parentId);
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
tree.push(item);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return tree;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default toHierarchy;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as TreeView } from "./TreeView.svelte";
|
package/types/index.d.ts
CHANGED
|
@@ -166,3 +166,4 @@ export { default as SkipToContent } from "./UIShell/SkipToContent.svelte";
|
|
|
166
166
|
export { default as HeaderGlobalAction } from "./UIShell/HeaderGlobalAction.svelte";
|
|
167
167
|
export { default as HeaderSearch } from "./UIShell/HeaderSearch.svelte";
|
|
168
168
|
export { default as UnorderedList } from "./UnorderedList/UnorderedList.svelte";
|
|
169
|
+
export { default as toHierarchy } from "./utils/toHierarchy";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type NodeLike = {
|
|
2
|
+
id: string | number;
|
|
3
|
+
nodes?: NodeLike[];
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/** Create a hierarchical tree from a flat array. */
|
|
8
|
+
export function toHierarchy<
|
|
9
|
+
T extends NodeLike,
|
|
10
|
+
K extends keyof Omit<T, "id" | "nodes">,
|
|
11
|
+
>(
|
|
12
|
+
flatArray: T[] | readonly T[],
|
|
13
|
+
/**
|
|
14
|
+
* Function that returns the parent ID for a given node.
|
|
15
|
+
* @example
|
|
16
|
+
* toHierarchy(flatArray, (node) => node.parentId);
|
|
17
|
+
*/
|
|
18
|
+
getParentId: (node: T) => T[K] | null,
|
|
19
|
+
): (T & { nodes?: (T & { nodes?: T[] })[] })[];
|
|
20
|
+
|
|
21
|
+
export default toHierarchy;
|