Skip to content

Commit f11a8e7

Browse files
committed
feat(nav): click on item to expand it
1 parent e4d7a12 commit f11a8e7

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/runtime/components/StoriesNav.vue

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
2+
import { ref, computed, watch, onMounted, onBeforeUnmount } from 'vue'
33
import { useRoute, useRouter } from 'vue-router'
44
import { useStories } from '../composables/use-stories'
55
@@ -71,7 +71,9 @@ const filteredItemList = computed((): TreeNode[] => {
7171
})
7272
7373
// Only expand folders that are ancestors of the currently selected leaf
74-
const expandedKeys = computed((): Record<string, boolean> => {
74+
const expandedKeys = ref<Record<string, boolean>>({})
75+
76+
function computeExpandedKeys(): Record<string, boolean> {
7577
const keys: Record<string, boolean> = {}
7678
function findPath(nodes: TreeNode[]): boolean {
7779
for (const node of nodes) {
@@ -88,13 +90,25 @@ const expandedKeys = computed((): Record<string, boolean> => {
8890
}
8991
findPath(itemList.value)
9092
return keys
91-
})
93+
}
94+
95+
watch(
96+
() => route.path,
97+
() => {
98+
expandedKeys.value = { ...expandedKeys.value, ...computeExpandedKeys() }
99+
},
100+
{ immediate: true },
101+
)
92102
93103
// Highlight the active route — leaf key === route.path
94104
const selectedKey = computed((): Record<string, boolean> => ({ [route.path]: true }))
95105
96106
function onNodeSelect(node: TreeNode) {
97107
if (node.data?.to) router.push(node.data.to)
108+
else if (node.children) {
109+
// Toggle folder expansion on click
110+
expandedKeys.value = { ...expandedKeys.value, [node.key]: !expandedKeys.value[node.key] }
111+
}
98112
}
99113
100114
function onKeyUp(event: KeyboardEvent) {
@@ -113,10 +127,10 @@ onBeforeUnmount(() => {
113127
<template>
114128
<div class="stories-nav">
115129
<PvTree
130+
v-model:expanded-keys="expandedKeys"
116131
:value="filteredItemList"
117132
selection-mode="single"
118133
:selection-keys="selectedKey"
119-
:expanded-keys="expandedKeys"
120134
filter
121135
class="stories-nav"
122136
@node-select="onNodeSelect"

0 commit comments

Comments
 (0)