This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathtree.stories.ts
477 lines (437 loc) · 12.6 KB
/
tree.stories.ts
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import '@cds/core/alert/register.js';
import '@cds/core/icon/register.js';
import '@cds/core/tree-view/register.js';
import { CdsTreeItem } from '@cds/core/tree-view';
import { html } from 'lit';
export default {
title: 'Stories/Tree View',
component: 'cds-tree',
parameters: {
options: { showPanel: true },
design: {
type: 'figma',
url: 'https://www.figma.com/file/v2mkhzKQdhECXOx8BElgdA/Clarity-UI-Library---light-2.2.0?node-id=51%3A668',
},
},
};
// export function API(args: any) {
// return html`
// <cds-tree ...="${spreadProps(getElementStorybookArgs(args))}">
// <cds-tree-item expanded>
// 1
// <cds-tree-item>
// 1-1
// <cds-tree-item>
// 1-1-1
// </cds-tree-item>
// <cds-tree-item>
// 1-1-2
// </cds-tree-item>
// </cds-tree-item>
// <cds-tree-item>
// 1-2
// </cds-tree-item>
// <cds-tree-item>
// 1-3
// </cds-tree-item>
// </cds-tree-item>
// <cds-tree-item>
// 2
// <cds-tree-item>
// 2-1
// </cds-tree-item>
// <cds-tree-item>
// 2-2
// </cds-tree-item>
// </cds-tree-item>
// <cds-tree-item>3</cds-tree-item>
// </cds-tree>
// `;
// }
/** @website */
export function basic() {
return html`
<cds-tree>
<cds-tree-item expanded>
David Wallace (CFO)
<cds-tree-item expanded>
Michael Scott (Regional Manager)
<cds-tree-item>Dwight K. Schrute (Assistant to the Regional Manager)</cds-tree-item>
<cds-tree-item>
Jim Halpert (Head of Sales)
<cds-tree-item>Andy Bernard</cds-tree-item>
<cds-tree-item>Stanley Hudson</cds-tree-item>
<cds-tree-item>Phyllis Vance</cds-tree-item>
<cds-tree-item>Todd Packer</cds-tree-item>
</cds-tree-item>
<cds-tree-item>
Angela Martin (Head of Accounting)
<cds-tree-item>Kevin Malone</cds-tree-item>
<cds-tree-item>Oscar Martinez</cds-tree-item>
</cds-tree-item>
<cds-tree-item>
Kelly Kapoor (Head of Customer Service)
<cds-tree-item>Ryan Howard (Temp)</cds-tree-item>
</cds-tree-item>
<cds-tree-item>
Creed Bratton (Quality Assurance)
</cds-tree-item>
<cds-tree-item>
Meredith Palmer (Supplier Relations)
</cds-tree-item>
<cds-tree-item>
Toby Flenderson (Human Resources)
</cds-tree-item>
<cds-tree-item>
Pam Beesly (Reception)
</cds-tree-item>
<cds-tree-item>
Darryl Philbin (Warehouse)
</cds-tree-item>
</cds-tree-item>
</cds-tree-item>
</cds-tree>
`;
}
export function navigation() {
return html`
<cds-tree>
<cds-tree-item expanded>
The Beatles
<cds-tree-item selected>
<a cds-text="link" href="https://en.wikipedia.org/wiki/Abbey_Road">Abbey Road</a>
</cds-tree-item>
<cds-tree-item>
<a cds-text="link" href="https://en.wikipedia.org/wiki/Revolver_(Beatles_album)">Revolver</a>
</cds-tree-item>
<cds-tree-item>
<a cds-text="link" href="https://en.wikipedia.org/wiki/Rubber_Soul">Rubber Soul</a>
</cds-tree-item>
</cds-tree-item>
</cds-tree>
`;
}
/** @website */
export function multiSelect() {
return html`
<cds-tree multi-select>
<cds-tree-item expanded selected>
1
<cds-tree-item disabled>
1-1
<cds-tree-item>
1-1-1
</cds-tree-item>
<cds-tree-item>
1-1-2
</cds-tree-item>
</cds-tree-item>
<cds-tree-item selected>
1-2
</cds-tree-item>
<cds-tree-item>
1-3
</cds-tree-item>
</cds-tree-item>
<cds-tree-item disabled selected>
2
<cds-tree-item>
2-1
</cds-tree-item>
<cds-tree-item>
2-2
</cds-tree-item>
</cds-tree-item>
<cds-tree-item>3</cds-tree-item>
</cds-tree>
`;
}
export const async = () => {
function renderTreeInfo(message: string) {
document.getElementById('loadingInfo').textContent = message;
}
function onExpandedChange(e: any, prefix: string) {
const parentNode = e.target as CdsTreeItem;
parentNode.expanded = !parentNode.expanded;
parentNode.loading = true;
if (parentNode.expanded) {
renderTreeInfo('loading children for item ' + prefix);
setTimeout(() => {
parentNode.loading = false;
renderTreeInfo('Finished loading children for item ' + prefix);
parentNode.innerHTML = `
${prefix}
<cds-tree-item>
${prefix}-1
</cds-tree-item>
<cds-tree-item>
${prefix}-2
</cds-tree-item>
<cds-tree-item>
${prefix}-3
</cds-tree-item>
`;
}, 3000);
} else {
parentNode.loading = false;
parentNode.innerHTML = `${prefix}`;
}
}
// I have set the aria-live attribute to assertive below because there's a VO bug that
// announces it twice in polite mode if inside an iframe (which all storybook stories are).
// In reality applications should be using polite in most cases. Below code snippet is just included
// as an example and not part of the component.
return html`
<div role="region" id="treeInfo" aria-live="assertive" cds-layout="display:screen-reader-only">
<p id="loadingInfo"></p>
</div>
<cds-tree>
<cds-tree-item expandable @expandedChange="${(e: any) => onExpandedChange(e, '1')}">
1
</cds-tree-item>
<cds-tree-item expandable @expandedChange="${(e: any) => onExpandedChange(e, '2')}">
2
</cds-tree-item>
</cds-tree>
`;
};
export const recursive = () => {
const level1 = 3;
const level2 = 3;
const level3 = 3;
const treeData: any = [];
for (let i = 1; i <= level1; i++) {
const children = [];
for (let j = 1; j <= level2; j++) {
const grandchildren = [];
for (let k = 1; k <= level3; k++) {
grandchildren.push({
display: `${i}-${j}-${k}`,
children: [],
});
}
children.push({
display: `${i}-${j}`,
children: grandchildren,
});
}
treeData.push({
display: i,
children: children,
});
}
const recursiveTemplate = (data: any) => {
return html`
<cds-tree-item expanded>
${data.display} ${data.children.map((i: any) => recursiveTemplate(i))}
</cds-tree-item>
`;
};
return html`
<cds-tree multi-select>
${treeData.map((node: any) => html` ${recursiveTemplate(node)} `)}
</cds-tree>
`;
};
export const interactive = () => {
const onExpandedChange = {
// handleEvent method is required.
handleEvent(e: any) {
const myTreeItem = e.target as any;
myTreeItem.expanded = e.detail;
},
// event listener objects can also define zero or more of the event
// listener options: capture, passive, and once.
capture: true,
};
function updateBasedOnChildren(node: any) {
if (node && node.tagName === 'CDS-TREE-ITEM') {
let oneSelected = false;
let oneUnselected = false;
node.indeterminate = false;
Array.from(node.children).forEach((c: any) => {
if (c.indeterminate) {
node.indeterminate = true;
}
if (c.selected) {
oneSelected = true;
if (oneUnselected) {
node.indeterminate = true;
}
}
if (!c.selected) {
oneUnselected = true;
if (oneSelected) {
node.indeterminate = true;
}
}
});
if (!oneSelected) {
node.selected = false;
} else if (!oneUnselected) {
node.selected = true;
} else {
node.selected = false;
}
updateBasedOnChildren(node.parentNode);
}
}
function updateChildren(node: any) {
Array.from(node.children).forEach((c: any) => {
c.indeterminate = false;
c.selected = node.selected;
updateChildren(c);
});
}
const onSelectedChange = {
// handleEvent method is required.
handleEvent(e: any) {
const currentNode = e.target as any;
if (!currentNode.disabled) {
currentNode.selected = e.detail;
currentNode.indeterminate = false;
// update children's selected/indeterminate state
updateChildren(currentNode);
// update ancestors' selected/indeterminate state
updateBasedOnChildren(currentNode.parentNode);
}
},
// event listener objects can also define zero or more of the event
// listener options: capture, passive, and once.
capture: true,
};
return html`
<a href="#" cds-text="link">link</a>
<cds-tree multi-select @expandedChange="${onExpandedChange}" @selectedChange="${onSelectedChange}">
<cds-tree-item indeterminate expanded>
1
<cds-tree-item>
1-1
<cds-tree-item>
1-1-1
</cds-tree-item>
<cds-tree-item>
1-1-2
</cds-tree-item>
</cds-tree-item>
<cds-tree-item selected>
1-2
</cds-tree-item>
<cds-tree-item>
1-3
</cds-tree-item>
</cds-tree-item>
<cds-tree-item indeterminate>
2
<cds-tree-item selected>
2-1
</cds-tree-item>
<cds-tree-item>
2-2
</cds-tree-item>
</cds-tree-item>
<cds-tree-item disabled>3</cds-tree-item>
<cds-tree-item selected>4</cds-tree-item>
</cds-tree>
<a href="#" cds-text="link">link</a>
`;
};
/** @website */
export function customStyles() {
return html`
<style>
.app-custom cds-tree-item {
--focus-width: var(--cds-alias-object-border-width-200);
--icon-transform: rotate(45deg);
--cds-alias-object-interaction-background-selected: var(--cds-global-color-jade-50);
--cds-alias-object-interaction-background-hover: var(--cds-global-color-jade-50);
--cds-alias-object-interaction-background-active: var(--cds-global-color-jade-100);
--cds-alias-object-interaction-background-highlight: var(--cds-global-color-jade-700);
}
.app-custom cds-tree-item[expanded] {
--icon-transform: rotate(135deg);
}
.app-custom cds-tree-item::part(checkbox) {
--color: var(--cds-global-color-jade-700);
--check-color: var(--cds-global-color-jade-50);
}
.app-custom cds-tree-item::part(expand-collapse-icon) {
--color: var(--cds-global-color-jade-700);
}
</style>
<div>
<cds-tree class="app-custom" multi-select>
<cds-tree-item expanded selected>
1
<cds-tree-item disabled>
1-1
<cds-tree-item>
1-1-1
</cds-tree-item>
<cds-tree-item>
1-1-2
</cds-tree-item>
</cds-tree-item>
<cds-tree-item selected>
1-2
</cds-tree-item>
<cds-tree-item>
1-3
</cds-tree-item>
</cds-tree-item>
<cds-tree-item disabled selected>
2
<cds-tree-item>
2-1
</cds-tree-item>
<cds-tree-item>
2-2
</cds-tree-item>
</cds-tree-item>
<cds-tree-item>3</cds-tree-item>
</cds-tree>
</div>
`;
}
/** @website */
export function darkTheme() {
return html`
<div cds-theme="dark">
<cds-tree multi-select>
<cds-tree-item expanded selected>
1
<cds-tree-item disabled>
1-1
<cds-tree-item>
1-1-1
</cds-tree-item>
<cds-tree-item>
1-1-2
</cds-tree-item>
</cds-tree-item>
<cds-tree-item selected>
1-2
</cds-tree-item>
<cds-tree-item>
1-3
</cds-tree-item>
</cds-tree-item>
<cds-tree-item disabled selected>
2
<cds-tree-item>
2-1
</cds-tree-item>
<cds-tree-item>
2-2
</cds-tree-item>
</cds-tree-item>
<cds-tree-item>3</cds-tree-item>
</cds-tree>
</div>
`;
}