Skip to content

Commit 22ee075

Browse files
committed
fix(ContentNavigation): improve path matching and recursion with default-open
Resolves #5112
1 parent 526e676 commit 22ee075

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/runtime/components/content/ContentNavigation.vue

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.contentNavig
134134
135135
const disabled = computed(() => props.disabled || (props.type === 'multiple' && props.collapsible === false))
136136
137+
function isRouteInTree(link: ContentNavigationLink, routePath: string): boolean {
138+
if (link.children?.length) {
139+
return link.children.some(child => isRouteInTree(child, routePath))
140+
}
141+
return routePath === link.path
142+
}
143+
137144
const defaultValue = computed(() => {
138145
// When `defaultOpen` is `false`, return `undefined` to close all items
139146
if (props.defaultOpen === false) {
@@ -144,10 +151,14 @@ const defaultValue = computed(() => {
144151
return props.type === 'single' ? '0' : props.navigation?.map((link, index) => link.defaultOpen !== false && String(index)).filter(Boolean) as string[]
145152
}
146153
// When `defaultOpen` is `true`, open items based on the current route
147-
const index = props.navigation?.findIndex(link => route.path.startsWith(link.path))
148-
const tyindex = index === -1 ? 0 : index
149-
150-
return props.type === 'multiple' ? [String(tyindex)] : String(tyindex)
154+
const indices = props.navigation?.reduce((acc, link, index) => {
155+
if (isRouteInTree(link, route.path)) {
156+
acc.push(String(index))
157+
}
158+
return acc
159+
}, [] as string[]) || []
160+
161+
return props.type === 'multiple' ? indices : indices[0]
151162
})
152163
</script>
153164

0 commit comments

Comments
 (0)