forked from carbon-design-system/carbon-components-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeView.svx
120 lines (69 loc) · 4.02 KB
/
TreeView.svx
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
<script>
import { InlineNotification, UnorderedList, ListItem, Link, } from "carbon-components-svelte";
import Preview from "../../components/Preview.svelte";
</script>
## Default
The `nodes` prop accepts an array of child nodes. Each node should contain `id` and `text` properties.
Optional properties include `disabled`, `icon`, and `nodes`.
A parent node contains `nodes` while a leaf node does not.
<InlineNotification svx-ignore lowContrast title="Note:" kind="info" hideCloseButton>
<div class="body-short-01">
In version 0.86.0, the <strong>children</strong> prop was renamed to <strong>nodes</strong> for <Link target="_blank" href="https://svelte.dev/docs/svelte/v5-migration-guide#The-children-prop-is-reserved">Svelte 5 compatibility</Link>.
</div>
</InlineNotification>
<InlineNotification svx-ignore lowContrast title="Note:" kind="info" hideCloseButton>
<div class="body-short-01">Every node must have a unique id.</div>
</InlineNotification>
<FileSource src="/framed/TreeView/TreeView" />
## Slottable node
By default, each item renders the value of `node.text`. Use the data from `let:node` directive to override the default slot.
The destructured `let:node` contains the following properties:
<UnorderedList svx-ignore style="margin-bottom: var(--cds-spacing-08)">
<ListItem><strong>id</strong>: the node id</ListItem>
<ListItem><strong>text</strong>: the node text</ListItem>
<ListItem><strong>expanded</strong>: true if the node is expanded</ListItem>
<ListItem><strong>leaf</strong>: true if the node does not have child nodes</ListItem>
<ListItem><strong>disabled</strong>: true if the node is disabled</ListItem>
<ListItem><strong>selected</strong>: true if the node is selected</ListItem>
</UnorderedList>
<FileSource src="/framed/TreeView/TreeViewSlot" />
## Initial active node
The active node can be set through `activeId`.
<FileSource src="/framed/TreeView/TreeViewActive" />
## Compact size
Set `size` to `"compact"` to use the compact variant.
<FileSource src="/framed/TreeView/TreeViewCompact" />
## With icons
To render a node with an icon, define an `icon` property with a Carbon Svelte icon as its value.
<FileSource src="/framed/TreeView/TreeViewIcons" />
## Initial expanded nodes
Expanded nodes can be set using `expandedIds`.
<FileSource src="/framed/TreeView/TreeViewExpanded" />
## Initial multiple selected nodes
Initial multiple selected nodes can be set using `selectedIds`.
<FileSource src="/framed/TreeView/TreeViewMultiselect" />
## Expand all nodes
To programmatically expand all nodes, access the component instance using the [bind:this](https://svelte.dev/docs#bind_element) directive and invoke the `TreeView.expandAll()` accessor.
<FileSource src="/framed/TreeView/TreeViewExpandAll" />
## Collapse all nodes
Similarly, invoke `TreeView.collapseAll()` to collapse all nodes.
<FileSource src="/framed/TreeView/TreeViewCollapseAll" />
## Expand a subset of nodes
Use the `TreeView.expandNodes` method to expand only a subset of nodes.
The method accepts an argument that takes a node and returns a boolean.
If no argument is provided, all nodes will be expanded.
<FileSource src="/framed/TreeView/TreeViewExpandNodes" />
## Collapse a subset of nodes
Use the `TreeView.collapseNodes` method to collapse a subset of nodes.
If no argument is provided, all nodes will be collapsed.
<FileSource src="/framed/TreeView/TreeViewCollapseNodes" />
## Show a specific node
Use the `TreeView.showNode` method to show a specific node.
If a matching node is found, it will be expanded, selected, and focused.
<FileSource src="/framed/TreeView/TreeViewShowNode" />
## Flat data structure
Use the `toHierarchy` method to provide a flat data structure to the `nodes` property.
This method will transform a flat array of objects into the hierarchical array as expected by `nodes`.
The child objects in the flat array need to have a `pid` property to reference its parent.
When `pid` is not provided the object is assumed to be at the root of the tree.
<FileSource src="/framed/TreeView/TreeViewFlatArray" />