|
| 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