-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathTreeViewNode.svelte
More file actions
159 lines (140 loc) · 4.33 KB
/
TreeViewNode.svelte
File metadata and controls
159 lines (140 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<script context="module">
/**
* Computes the depth of a tree leaf node relative to <ul role="tree" />.
* Returns the depth of the node (0-based, where 0 is the root level).
* @type {(node: HTMLLIElement | null) => number}
* @example
* ```svelte
* import { computeTreeLeafDepth } from 'carbon-components-svelte/TreeView/TreeViewNode.svelte';
* let nodeElement;
* $: depth = computeTreeLeafDepth(nodeElement);
*
* <li bind:this={nodeElement}>Node at depth {depth}</li>
* ```
*/
export function computeTreeLeafDepth(node) {
let depth = 0;
if (node == null) return depth;
// Count the node itself if it's an LI
if (node instanceof HTMLElement && node.tagName === "LI") {
depth++;
}
let parentNode = node.parentNode;
while (
parentNode != null &&
parentNode instanceof HTMLElement &&
parentNode.getAttribute("role") !== "tree"
) {
if (parentNode.tagName === "LI") depth++;
parentNode = parentNode.parentNode;
}
return depth;
}
/**
* Finds the nearest parent tree node
* @param {HTMLElement | null} node
* @returns {null | HTMLElement}
*/
function findParentTreeNode(node) {
if (node == null || !(node instanceof HTMLElement)) return null;
if (node.classList.contains("bx--tree-parent-node")) return node;
if (node.classList.contains("bx--tree")) return null;
if (node.parentNode instanceof HTMLElement) {
return findParentTreeNode(node.parentNode);
}
return null;
}
</script>
<script>
/**
* @generics {Node extends TreeNode<any> = TreeNode<any>, Icon = any} Node,Icon
* @template {TreeNode<any>} Node
* @typedef {import('./TreeView.svelte').TreeNode<Id>} TreeNode<Id=(string|number)>
* @slot {{ node: Node & { expanded: false; leaf: boolean; selected: boolean; } }}
*/
export let leaf = false;
/** @type {Node["id"]} */
export let id = "";
export let text = "";
export let disabled = false;
/**
* Specify the icon to render.
* @type {Icon}
*/
export let icon = /** @type {Icon} */ (undefined);
import { afterUpdate, getContext } from "svelte";
let ref = null;
let refLabel = null;
let prevActiveId = undefined;
const {
activeNodeId,
selectedIdsSetStore,
clickNode,
selectNode,
focusNode,
} = getContext("carbon:TreeView");
const offset = () =>
computeTreeLeafDepth(refLabel) - 1 + (leaf && icon ? 2 : 2.5);
afterUpdate(() => {
if (id === $activeNodeId && prevActiveId !== $activeNodeId) {
if (!$selectedIdsSetStore.has(id)) selectNode(node);
}
prevActiveId = $activeNodeId;
});
$: selected = $selectedIdsSetStore.has(id);
// Merge all props (including custom properties) with computed properties
// Explicitly include disabled to ensure it's always present (has default value)
$: node = {
...$$props,
disabled, // Ensure disabled is always included (has default value)
expanded: false, // A node cannot be expanded.
leaf,
selected,
};
$: if (refLabel) {
refLabel.style.marginLeft = `-${offset()}rem`;
refLabel.style.paddingLeft = `${offset()}rem`;
}
</script>
<!-- svelte-ignore a11y-no-noninteractive-element-to-interactive-role -->
<li
bind:this={ref}
role="treeitem"
{id}
tabindex={disabled ? undefined : -1}
aria-current={id === $activeNodeId || undefined}
aria-selected={disabled ? undefined : selected}
aria-disabled={disabled}
class:bx--tree-node={true}
class:bx--tree-leaf-node={true}
class:bx--tree-node--active={id === $activeNodeId}
class:bx--tree-node--selected={selected}
class:bx--tree-node--disabled={disabled}
class:bx--tree-node--with-icon={icon}
on:click|stopPropagation={() => {
if (disabled) return;
clickNode(node);
}}
on:keydown={(e) => {
if (e.key === "ArrowLeft" || e.key === "ArrowRight" || e.key === "Enter") {
e.stopPropagation();
}
if (e.key === "ArrowLeft") {
const parentNode = findParentTreeNode(ref.parentNode);
if (parentNode) parentNode.focus();
}
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
if (disabled) return;
clickNode(node);
}
}}
on:focus={() => {
focusNode(node);
}}
>
<div bind:this={refLabel} class:bx--tree-node__label={true}>
<svelte:component this={icon} class="bx--tree-node__icon" />
<slot {node}> {text} </slot>
</div>
</li>