/
calcite-tree-item.tsx
356 lines (303 loc) · 10.6 KB
/
calcite-tree-item.tsx
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import {
Component,
Element,
Prop,
Host,
Event,
EventEmitter,
State,
Listen,
Watch,
h,
VNode
} from "@stencil/core";
import { TreeItemSelectDetail } from "./interfaces";
import { TreeSelectionMode } from "../calcite-tree/interfaces";
import { nodeListToArray, getElementDir, filterDirectChildren, getSlotted } from "../../utils/dom";
import { getKey } from "../../utils/key";
@Component({
tag: "calcite-tree-item",
styleUrl: "calcite-tree-item.scss",
shadow: true
})
export class CalciteTreeItem {
//--------------------------------------------------------------------------
//
// Element
//
//--------------------------------------------------------------------------
@Element() el: HTMLCalciteTreeItemElement;
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/** Is the item currently selected */
@Prop({ mutable: true, reflect: true }) selected = false;
/** True if the item is in an expanded state */
@Prop({ mutable: true, reflect: true }) expanded = false;
@Watch("expanded")
expandedHandler(newValue: boolean): void {
const items = getSlotted<HTMLCalciteTreeItemElement>(this.el, "children", {
all: true,
selector: "calcite-tree-item"
});
items.forEach((item) => (item.parentExpanded = newValue));
}
/** @internal Is the parent of this item expanded? */
@Prop() parentExpanded = false;
/** @internal What level of depth is this item at? */
@Prop({ reflect: true, mutable: true }) depth = -1;
/** @internal Does this tree item have a tree inside it? */
@Prop({ reflect: true, mutable: true }) hasChildren: boolean = null;
/** @internal Draw lines (set on parent) */
@Prop({ reflect: true, mutable: true }) lines: boolean;
/** @internal Display checkboxes (set on parent) */
@Prop({ reflect: true, mutable: true }) inputEnabled: boolean;
/** @internal Scale of the parent tree, defaults to m */
@Prop({ reflect: true, mutable: true }) scale: "s" | "m";
/**
* @internal
* In ancestor selection mode using inputEnabled,
* show as indeterminate when only some children are selected
**/
@Prop({ reflect: true }) indeterminate: boolean;
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback(): void {
this.expandedHandler(this.expanded);
}
componentWillRender(): void {
this.hasChildren = !!this.el.querySelector("calcite-tree");
this.depth = 0;
this.el.dir = getElementDir(this.el);
let parentTree = this.el.closest("calcite-tree");
if (!parentTree) {
return;
}
this.selectionMode = parentTree.selectionMode;
this.scale = parentTree.scale || "m";
this.lines = parentTree.lines;
this.inputEnabled = parentTree.inputEnabled;
let nextParentTree;
while (parentTree) {
nextParentTree = parentTree.parentElement.closest("calcite-tree");
if (nextParentTree === parentTree) {
break;
} else {
parentTree = nextParentTree;
this.depth = this.depth + 1;
}
}
}
render(): VNode {
const icon = this.hasChildren ? (
<calcite-icon
class="calcite-tree-chevron"
data-test-id="icon"
icon="chevron-right"
onClick={this.iconClickHandler}
scale="s"
/>
) : null;
const checkbox = this.inputEnabled ? (
<label class="calcite-tree-label">
<calcite-checkbox
checked={this.selected}
class="calcite-tree-checkbox"
data-test-id="checkbox"
indeterminate={this.hasChildren && this.indeterminate}
scale={this.scale}
tabIndex={-1}
/>
<slot />
</label>
) : null;
const hidden = !(this.parentExpanded || this.depth === 1);
return (
<Host
aria-expanded={this.hasChildren ? this.expanded.toString() : undefined}
aria-hidden={hidden.toString()}
aria-selected={
this.selected
? "true"
: this.selectionMode === TreeSelectionMode.Multi ||
this.selectionMode === TreeSelectionMode.MultiChildren
? "false"
: undefined
}
calcite-hydrated-hidden={hidden}
role="treeitem"
tabindex={this.parentExpanded || this.depth === 1 ? "0" : "-1"}
>
<div class="calcite-tree-node" ref={(el) => (this.defaultSlotWrapper = el as HTMLElement)}>
{icon}
{checkbox ? checkbox : <slot />}
</div>
<div
class="calcite-tree-children"
data-test-id="calcite-tree-children"
onClick={this.childrenClickHandler}
ref={(el) => (this.childrenSlotWrapper = el as HTMLElement)}
role={this.hasChildren ? "group" : undefined}
>
<slot name="children" />
</div>
</Host>
);
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
@Listen("click") onClick(e: Event): void {
// Solve for if the item is clicked somewhere outside the slotted anchor.
// Anchor is triggered anywhere you click
const [link] = filterDirectChildren<HTMLAnchorElement>(this.el, "a");
if (link && (e.composedPath()[0] as any).tagName.toLowerCase() !== "a") {
const target = link.target === "" ? "_self" : link.target;
window.open(link.href, target);
}
this.expanded = !this.expanded;
this.calciteTreeItemSelect.emit({
modifyCurrentSelection: (e as any).shiftKey || this.inputEnabled,
forceToggle: false
});
}
iconClickHandler = (event: MouseEvent): void => {
event.stopPropagation();
this.expanded = !this.expanded;
if (!this.inputEnabled) {
this.calciteTreeItemSelect.emit({
modifyCurrentSelection: event.shiftKey,
forceToggle: true
});
}
};
childrenClickHandler = (event: MouseEvent): void => event.stopPropagation();
@Listen("keydown") keyDownHandler(e: KeyboardEvent): void {
let root;
switch (getKey(e.key)) {
case " ":
this.calciteTreeItemSelect.emit({
modifyCurrentSelection: e.shiftKey,
forceToggle: false
});
e.preventDefault();
e.stopPropagation();
break;
case "Enter":
// activates a node, i.e., performs its default action. For parent nodes, one possible default action is to open or close the node. In single-select trees where selection does not follow focus (see note below), the default action is typically to select the focused node.
const link = nodeListToArray(this.el.children).find((e) =>
e.matches("a")
) as HTMLAnchorElement;
if (link) {
link.click();
this.selected = true;
} else {
this.calciteTreeItemSelect.emit({
modifyCurrentSelection: e.shiftKey,
forceToggle: false
});
}
e.preventDefault();
e.stopPropagation();
break;
case "ArrowLeft":
// When focus is on an open node, closes the node.
if (this.hasChildren && this.expanded) {
this.expanded = false;
e.preventDefault();
e.stopPropagation();
break;
}
// When focus is on a child node that is also either an end node or a closed node, moves focus to its parent node.
const parentItem = this.el.parentElement.closest("calcite-tree-item");
if (parentItem && (!this.hasChildren || this.expanded === false)) {
parentItem.focus();
e.preventDefault();
e.stopPropagation();
break;
}
// When focus is on a root node that is also either an end node or a closed node, does nothing.
break;
case "ArrowRight":
// When focus is on a closed node, opens the node; focus does not move.
if (this.hasChildren && this.expanded === false) {
this.expanded = true;
e.preventDefault();
e.stopPropagation();
break;
}
// When focus is on a open node, moves focus to the first child node.
if (this.hasChildren && this.expanded) {
this.el.querySelector("calcite-tree-item").focus();
break;
}
// When focus is on an end node, does nothing.
break;
case "ArrowUp":
const previous = this.el.previousElementSibling as HTMLCalciteTreeItemElement;
if (previous && previous.matches("calcite-tree-item")) {
previous.focus();
}
break;
case "ArrowDown":
const next = this.el.nextElementSibling as HTMLCalciteTreeItemElement;
if (next && next.matches("calcite-tree-item")) {
next.focus();
}
break;
case "Home":
root = this.el.closest("calcite-tree:not([child])") as HTMLCalciteTreeElement;
const firstNode = root.querySelector("calcite-tree-item");
firstNode.focus();
break;
case "End":
root = this.el.closest("calcite-tree:not([child])");
let currentNode = root.children[root.children.length - 1]; // last child
let currentTree = nodeListToArray(currentNode.children).find((e) =>
e.matches("calcite-tree")
);
while (currentTree) {
currentNode = currentTree.children[root.children.length - 1];
currentTree = nodeListToArray(currentNode.children).find((e) =>
e.matches("calcite-tree")
);
}
currentNode.focus();
break;
}
}
//--------------------------------------------------------------------------
//
// Events
//
//--------------------------------------------------------------------------
/**
* @internal
*/
@Event() calciteTreeItemSelect: EventEmitter<TreeItemSelectDetail>;
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// Private State/Props
//
//--------------------------------------------------------------------------
@State() private selectionMode: TreeSelectionMode;
childrenSlotWrapper!: HTMLElement;
defaultSlotWrapper!: HTMLElement;
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
}