carbon-components-svelte 0.86.1 → 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 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)**: 20+ charts, powered by d3
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-components-svelte",
3
- "version": "0.86.1",
3
+ "version": "0.87.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Svelte implementation of the Carbon Design System",
6
6
  "type": "module",
@@ -243,11 +243,11 @@
243
243
  }
244
244
  });
245
245
 
246
- $: menuId = `menu-${id}`;
247
- $: inline = type === "inline";
248
- $: ariaLabel = $$props["aria-label"] || "Choose an item";
249
- $: sortedItems = (() => {
250
- if (selectionFeedback === "top" && selectedIds.length > 0) {
246
+ function sort() {
247
+ if (
248
+ selectionFeedback === "top" ||
249
+ selectionFeedback === "top-after-reopen"
250
+ ) {
251
251
  const checkedItems = items
252
252
  .filter((item) => selectedIds.includes(item.id))
253
253
  .map((item) => ({ ...item, checked: true }));
@@ -269,7 +269,20 @@
269
269
  checked: selectedIds.includes(item.id),
270
270
  }))
271
271
  .sort(sortItem);
272
- })();
272
+ }
273
+
274
+ let sortedItems = sort();
275
+
276
+ $: menuId = `menu-${id}`;
277
+ $: inline = type === "inline";
278
+ $: ariaLabel = $$props["aria-label"] || "Choose an item";
279
+ $: if (
280
+ selectedIds &&
281
+ (selectionFeedback === "top" ||
282
+ (selectionFeedback === "top-after-reopen" && open === false))
283
+ ) {
284
+ sortedItems = sort();
285
+ }
273
286
  $: checked = sortedItems.filter(({ checked }) => checked);
274
287
  $: unchecked = sortedItems.filter(({ checked }) => !checked);
275
288
  $: filteredItems = sortedItems.filter((item) => filterItem(item, value));
@@ -46,6 +46,7 @@
46
46
  <button
47
47
  bind:this={ref}
48
48
  {disabled}
49
+ type="button"
49
50
  aria-describedby={id}
50
51
  class:bx--tooltip__trigger={true}
51
52
  class:bx--tooltip--a11y={true}
@@ -139,7 +139,6 @@
139
139
  ref?.querySelector(`[id="${lastId}"]`)?.focus();
140
140
  });
141
141
 
142
- // Break out of the loop if the node is found.
143
142
  break;
144
143
  }
145
144
  }
@@ -0,0 +1 @@
1
+ export { default as TreeView } from "./TreeView.svelte";
package/src/index.js CHANGED
@@ -152,3 +152,4 @@ export {
152
152
  HeaderSearch,
153
153
  } from "./UIShell";
154
154
  export { UnorderedList } from "./UnorderedList";
155
+ export { 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;
@@ -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;