Skip to content

Commit

Permalink
[PageContainer] Remove leading slash from patterns for PageContainer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Oct 7, 2024
1 parent 329bb7e commit 8154415
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const NAVIGATION = [
{
segment: 'inbox',
title: 'Orders',
pattern: '/inbox/:id',
pattern: 'inbox/:id',
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const NAVIGATION = [
{
segment: 'inbox',
title: 'Orders',
pattern: '/inbox/:id',
pattern: 'inbox/:id',
},
];

Expand Down
30 changes: 29 additions & 1 deletion packages/toolpad-core/src/PageContainer/PageContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('PageContainer', () => {
<AppProvider
navigation={[
{ segment: '', title: 'Home' },
{ segment: 'orders', title: 'Orders', pattern: '/orders/:id' },
{ segment: 'orders', title: 'Orders', pattern: 'orders/:id' },
]}
router={router}
>
Expand All @@ -112,4 +112,32 @@ describe('PageContainer', () => {

expect(screen.getByText('Orders', { ignore: 'nav *' }));
});

test('renders nested dynamic correctly', async () => {
const router = {
pathname: '/users/invoices/123',
searchParams: new URLSearchParams(),
navigate: vi.fn(),
};
render(
<AppProvider
navigation={[
{
segment: 'users',
title: 'Users',
children: [{ segment: 'invoices', title: 'Invoices', pattern: 'invoices/:id' }],
},
]}
router={router}
>
<PageContainer />
</AppProvider>,
);

const breadcrumbs = screen.getByRole('navigation', { name: 'breadcrumb' });

const homeLink = within(breadcrumbs).getByRole('link', { name: 'Users' });
expect(homeLink.getAttribute('href')).toBe('/users');
expect(within(breadcrumbs).getByText('Invoices')).toBeTruthy();
});
});
3 changes: 2 additions & 1 deletion packages/toolpad-core/src/shared/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ function buildItemLookup(navigation: Navigation) {
}
map.set(path, item);
if (item.pattern) {
map.set(pathToRegexp(item.pattern), item);
const basePath = item.segment ? path.slice(0, -item.segment.length) : path;
map.set(pathToRegexp(basePath + item.pattern), item);
}
if (item.children) {
for (const child of item.children) {
Expand Down

0 comments on commit 8154415

Please sign in to comment.