Skip to content

Commit

Permalink
🐛 bug: fix path remove params error
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Aug 6, 2020
1 parent 290ec5e commit 825c352
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 69 deletions.
5 changes: 4 additions & 1 deletion src/getFlatMenus/getFlatMenus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MenuDataItem } from '../types';
import { stripQueryStringAndHashFromPath } from '../transformRoute/transformRoute';

/**
* 获取打平的 menuData
Expand All @@ -18,7 +19,9 @@ export const getFlatMenus = (
return;
}

menus[item.path || item.key || '/'] = { ...item };
menus[stripQueryStringAndHashFromPath(item.path || item.key || '/')] = {
...item,
};
menus[item.key || item.path || '/'] = { ...item };

if (item.children) {
Expand Down
10 changes: 7 additions & 3 deletions src/getMatchMenu/getMatchMenu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { pathToRegexp } from '@qixian.cs/path-to-regexp';
import { MenuDataItem } from '../types';
import getFlatMenu from '../getFlatMenus/getFlatMenus';
import { isUrl } from '../transformRoute/transformRoute';
import {
isUrl,
stripQueryStringAndHashFromPath,
} from '../transformRoute/transformRoute';

export const getMenuMatches = (
flatMenuKeys: string[] = [],
Expand All @@ -13,13 +16,14 @@ export const getMenuMatches = (
return true;
}
if (item !== '/' && item !== '/*' && item && !isUrl(item)) {
const pathKey = stripQueryStringAndHashFromPath(item);
try {
// /a
if (pathToRegexp(`${item}`, []).test(path)) {
if (pathToRegexp(`${pathKey}`, []).test(path)) {
return true;
}
// /a/b/b
if (pathToRegexp(`${item}(.*)`).test(path)) {
if (pathToRegexp(`${pathKey}(.*)`).test(path)) {
return true;
}
} catch (error) {
Expand Down
32 changes: 18 additions & 14 deletions src/transformRoute/transformRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { pathToRegexp } from '@qixian.cs/path-to-regexp';

import { MenuDataItem, Route, MessageDescriptor } from '../types';

function stripQueryStringAndHashFromPath(url: string) {
export function stripQueryStringAndHashFromPath(url: string) {
return url.split('?')[0].split('#')[0];
}

Expand All @@ -25,7 +25,7 @@ export const getKeyByPath = (item: MenuDataItem) => {
}
}

return path;
return path ? stripQueryStringAndHashFromPath(path) : path;
};

/**
Expand Down Expand Up @@ -177,9 +177,7 @@ function formatter(
return true;
})
.map((item = { path: '/' }) => {
const path = stripQueryStringAndHashFromPath(
mergePath(item.path, parent ? parent.path : '/'),
);
const path = mergePath(item.path, parent ? parent.path : '/');
const { name } = item;
const locale = getItemLocaleName(item, parentName || 'menu');

Expand Down Expand Up @@ -279,16 +277,22 @@ const defaultFilterMenuData = (menuData: MenuDataItem[] = []): MenuDataItem[] =>
class RoutesMap<V> extends Map<string, V> {
get(pathname: string) {
let routeValue;
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of this.entries()) {
if (
!isUrl(key as string) &&
pathToRegexp(key as any, []).test(pathname as any)
) {
routeValue = value;
break;
try {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of this.entries()) {
const path = stripQueryStringAndHashFromPath(key);
if (
!isUrl(key as string) &&
pathToRegexp(path as any, []).test(pathname as any)
) {
routeValue = value;
break;
}
}
} catch (error) {
routeValue = undefined;
}

return routeValue;
}
}
Expand All @@ -308,7 +312,7 @@ const getBreadcrumbNameMap = (
}
// Reduce memory usage
const path = mergePath(menuItem.path, parent ? parent.path : '/');
routerMap.set(path, menuItem);
routerMap.set(stripQueryStringAndHashFromPath(path), menuItem);
});
};
flattenMenuData(menuData);
Expand Down
Loading

0 comments on commit 825c352

Please sign in to comment.