Skip to content

Commit 44685ed

Browse files
committed
BsListNavItem, BsListTile, BsTab, BsTabItem: update routeMatcher to indicate the element is active or not
1 parent 726e610 commit 44685ed

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/mixins/CommonApi.ts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import {
1818
renderSlot,
1919
resolveComponent,
2020
Transition,
21+
unref,
2122
withCtx,
2223
} from 'vue';
23-
import type { RouteLocationNormalizedLoaded } from 'vue-router';
24+
import type { RouteLocationNormalizedLoaded, RouteRecordRaw } from 'vue-router';
25+
import { useRouter } from 'vue-router';
2426
import type {
2527
IHttpService,
2628
INotificationProvider,
@@ -253,8 +255,33 @@ export function useCurrentRoute(): Ref<RouteLocationNormalizedLoaded> | undefine
253255
return vm?.appContext.config.globalProperties.$router?.currentRoute;
254256
}
255257

258+
function findRouteByName(search: TRouterOptionProps) {
259+
const router = useRouter();
260+
const routes = router.getRoutes();
261+
262+
const findMatches = (children: RouteRecordRaw[]) =>
263+
children.find((it): boolean => {
264+
if (it.name === search.pathName) {
265+
return true;
266+
} else if (it.children && it.children.length > 0) {
267+
return !!findMatches(it.children);
268+
}
269+
270+
return false;
271+
});
272+
273+
return routes.find((it): boolean => {
274+
if (it.name === search.pathName) {
275+
return true;
276+
} else if (it.children.length > 0) {
277+
return !!findMatches(it.children);
278+
}
279+
return false;
280+
});
281+
}
282+
256283
/**
257-
* Check if the given route match the navigation element.
284+
* Check if the given route match the navigation component.
258285
*
259286
* @param route The current route to check
260287
* @param navTarget The navigation element
@@ -263,11 +290,25 @@ export function useIsRouteMatch(
263290
route: Ref<RouteLocationNormalizedLoaded>,
264291
navTarget: TRouterOptionProps
265292
): boolean {
293+
const _route = unref(route);
294+
295+
if (navTarget.pathName) {
296+
if (_route.name === navTarget.pathName) {
297+
return true;
298+
}
299+
300+
const result = findRouteByName(navTarget);
301+
if (result) {
302+
return result.path === _route.path || _route.path.startsWith(`${result.path}/`);
303+
}
304+
}
305+
266306
return (
267-
navTarget.path === route.value.path ||
268-
`${navTarget.path}/` === route.value.path ||
269-
route.value.name === navTarget.pathName ||
270-
route.value.name === navTarget.location?.name
307+
navTarget.path === _route.path ||
308+
(navTarget.path && _route.path.startsWith(`${navTarget.path}/`)) ||
309+
navTarget.location?.path === _route.path ||
310+
(navTarget.location?.path && _route.path.startsWith(`${navTarget.location.path}/`)) ||
311+
navTarget.location?.name === _route.name
271312
);
272313
}
273314

0 commit comments

Comments
 (0)