drawer-stack
Version:
Drawer stack for React
40 lines (39 loc) • 1.37 kB
JavaScript
import { matchPath } from "react-router";
// Flatten nested routes for easier matching in drawers
export const flattenRoutes = (routes, parentPath = "") => {
const flattened = [];
routes.forEach((route) => {
const fullPath = route.path
? parentPath === "/"
? route.path === "/"
? "/"
: `/${route.path}`
: `${parentPath}/${route.path}`.replace(/\/+/g, "/")
: parentPath;
// Add the route itself
flattened.push({
...route,
path: fullPath,
});
// Add children recursively
if (route.children) {
flattened.push(...flattenRoutes(route.children, fullPath));
}
});
return flattened;
};
// Find a route and its match data by path in a flattened route array
export const findRouteAndMatch = (path, routes) => {
// Extract just the pathname without query parameters for route matching
const pathname = path.split("?")[0];
for (const route of routes) {
if (!route.path)
continue;
// Use end: true to ensure we match the exact path and not a partial parent path.
const match = matchPath({ path: route.path, end: true }, pathname);
if (match) {
return { route, match };
}
}
return undefined;
};