Skip to content

Commit 5afafaa

Browse files
committed
docs(tree-view): add filter nodes examples
1 parent 5a476aa commit 5afafaa

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

docs/src/pages/components/TreeView.svx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,28 @@ Use `TreeView.showNode` to expand, select, and focus a specific node.
113113
Convert flat data to a hierarchical structure using the `toHierarchy` utility.
114114

115115
<FileSource src="/framed/TreeView/TreeViewFlatArray" />
116+
117+
## Filter by text with search input
118+
119+
Combine a `Search` input with `filterTreeByText` to create an interactive searchable tree. Type to filter nodes by name (case-insensitive substring matching).
120+
121+
<FileSource src="/framed/TreeView/TreeViewFilter" />
122+
123+
## Filter utilities
124+
125+
Three filtering functions are available:
126+
127+
<UnorderedList svx-ignore style="margin-bottom: var(--cds-spacing-08)">
128+
<ListItem><strong>filterTreeByText(tree, text, options)</strong>: Case-insensitive substring search on node text</ListItem>
129+
<ListItem><strong>filterTreeById(tree, id, options)</strong>: Filter by single ID or array of IDs</ListItem>
130+
<ListItem><strong>filterTreeNodes(tree, predicate, options)</strong>: Custom filtering with predicate function</ListItem>
131+
</UnorderedList>
132+
133+
## Filter options
134+
135+
All filter functions accept an optional `options` object:
136+
137+
<UnorderedList svx-ignore style="margin-bottom: var(--cds-spacing-08)">
138+
<ListItem><strong>includeAncestors</strong> (default: true): Include parent chain of matched nodes</ListItem>
139+
<ListItem><strong>includeChildren</strong> (default: false): Include all descendants of matched nodes</ListItem>
140+
</UnorderedList>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<script>
2+
import { filterTreeByText, Search, TreeView } from "carbon-components-svelte";
3+
4+
const allNodes = [
5+
{
6+
id: "1",
7+
text: "Documents",
8+
nodes: [
9+
{
10+
id: "1-1",
11+
text: "Work",
12+
nodes: [
13+
{ id: "1-1-1", text: "Report.docx" },
14+
{ id: "1-1-2", text: "Presentation.pptx" },
15+
{ id: "1-1-3", text: "Budget.xlsx" },
16+
{ id: "1-1-4", text: "Meeting Notes.txt" },
17+
],
18+
},
19+
{
20+
id: "1-2",
21+
text: "Personal",
22+
nodes: [
23+
{ id: "1-2-1", text: "Resume.pdf" },
24+
{ id: "1-2-2", text: "Cover Letter.pdf" },
25+
{ id: "1-2-3", text: "Portfolio.pdf" },
26+
],
27+
},
28+
{
29+
id: "1-3",
30+
text: "Projects",
31+
nodes: [
32+
{
33+
id: "1-3-1",
34+
text: "Website",
35+
nodes: [
36+
{ id: "1-3-1-1", text: "index.html" },
37+
{ id: "1-3-1-2", text: "styles.css" },
38+
{ id: "1-3-1-3", text: "script.js" },
39+
],
40+
},
41+
{
42+
id: "1-3-2",
43+
text: "App",
44+
nodes: [
45+
{ id: "1-3-2-1", text: "main.py" },
46+
{ id: "1-3-2-2", text: "config.json" },
47+
],
48+
},
49+
],
50+
},
51+
],
52+
},
53+
{
54+
id: "2",
55+
text: "Pictures",
56+
nodes: [
57+
{ id: "2-1", text: "Vacation.jpg" },
58+
{ id: "2-2", text: "Family.jpg" },
59+
{ id: "2-3", text: "Birthday.png" },
60+
],
61+
},
62+
{
63+
id: "3",
64+
text: "Music",
65+
nodes: [
66+
{
67+
id: "3-1",
68+
text: "Rock",
69+
nodes: [
70+
{ id: "3-1-1", text: "Song1.mp3" },
71+
{ id: "3-1-2", text: "Song2.mp3" },
72+
],
73+
},
74+
{
75+
id: "3-2",
76+
text: "Jazz",
77+
nodes: [
78+
{ id: "3-2-1", text: "Song3.mp3" },
79+
{ id: "3-2-2", text: "Song4.mp3" },
80+
],
81+
},
82+
{
83+
id: "3-3",
84+
text: "Classical",
85+
nodes: [{ id: "3-3-1", text: "Symphony.mp3" }],
86+
},
87+
],
88+
},
89+
];
90+
91+
let searchValue = "";
92+
let expandedIds = [];
93+
94+
// Reactively filter nodes based on search input
95+
$: filteredNodes =
96+
searchValue.trim() === ""
97+
? allNodes
98+
: filterTreeByText(allNodes, searchValue);
99+
100+
// Auto-expand filtered nodes to show matches
101+
$: if (searchValue.trim() !== "" && filteredNodes.length > 0) {
102+
// Extract all node IDs from filtered tree for expansion
103+
const extractIds = (nodes) => {
104+
const ids = [];
105+
for (const node of nodes) {
106+
ids.push(node.id);
107+
if (node.nodes) {
108+
ids.push(...extractIds(node.nodes));
109+
}
110+
}
111+
return ids;
112+
};
113+
expandedIds = extractIds(filteredNodes);
114+
} else {
115+
expandedIds = [];
116+
}
117+
</script>
118+
119+
<Search placeholder="Search tree nodes..." bind:value={searchValue} />
120+
121+
{#if filteredNodes.length > 0}
122+
<TreeView labelText="File System" nodes={filteredNodes} {expandedIds} />
123+
{:else}
124+
<div style="padding: 1rem; color: var(--cds-text-secondary);">
125+
No matching nodes found for "{searchValue}"
126+
</div>
127+
{/if}

0 commit comments

Comments
 (0)