@@ -77,23 +77,28 @@ const filteredItemList = computed((): TreeNode[] => {
7777 return filterTree (itemList .value , search .value )
7878})
7979
80- // All folder nodes are expanded by default
80+ // Only expand folders that are ancestors of the currently selected leaf
8181const expandedKeys = computed ((): Record <string , boolean > => {
8282 const keys: Record <string , boolean > = {}
83- function collect(nodes : TreeNode []) {
84- nodes .forEach ((node ) => {
85- if (node .children ) {
86- keys [node .key as string ] = true
87- collect (node .children )
83+ function findPath(nodes : TreeNode []): boolean {
84+ for (const node of nodes ) {
85+ if (node .leaf ) {
86+ if (node .key === route .path ) return true
87+ } else if (node .children ) {
88+ if (findPath (node .children )) {
89+ keys [node .key as string ] = true
90+ return true
91+ }
8892 }
89- })
93+ }
94+ return false
9095 }
91- collect (itemList .value )
96+ findPath (itemList .value )
9297 return keys
9398})
9499
95100// Highlight the active route — leaf key === route.path
96- const selectionKeys = computed ((): Record <string , boolean > => ({ [route .path ]: true }))
101+ const selectedKey = computed ((): Record <string , boolean > => ({ [route .path ]: true }))
97102
98103function onNodeSelect(node : TreeNode ) {
99104 if (node .data ?.to ) router .push (node .data .to )
@@ -202,15 +207,12 @@ onBeforeUnmount(() => {
202207 <PvButton class="stories-nav__toggle" aria-label="Toggle nav" @click =" navIsOpen = ! navIsOpen " />
203208 </div >
204209 <div class =" stories-nav__main" >
205- <div class =" stories-nav__search" >
206- <PvInputText v-model =" search " type="text" class="stories-nav__search__input" placeholder="Search…" />
207- <PvButton v-if =" search " class="stories-nav__search__clear" aria-label="Clear search" @click =" search = ' ' " />
208- </div >
209210 <PvTree
210211 :value =" filteredItemList "
211212 selection-mode="single"
212- :selection-keys =" selectionKeys "
213+ :selection-keys =" selectedKey "
213214 :expanded-keys =" expandedKeys "
215+ filter
214216 class="stories-nav__tree"
215217 @node-select =" onNodeSelect "
216218 />
0 commit comments