Skip to content

Commit

Permalink
Add a test and final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira committed Jan 16, 2025
1 parent ee59d32 commit 9acf943
Show file tree
Hide file tree
Showing 7 changed files with 3,525 additions and 10,017 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"description": "Whether the sidebar should not be collapsible to a mini variant in desktop and tablet viewports."
},
"hideNavigation": {
"description": "Whether the navigation bar and menu icon should be hidden"
"description": "Whether the navigation bar and menu icon should be hidden."
},
"navigation": { "description": "Navigation definition for the dashboard." },
"sidebarExpandedWidth": { "description": "Width of the sidebar when expanded." },
Expand Down
21 changes: 19 additions & 2 deletions packages/toolpad-core/src/DashboardLayout/DashboardLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,18 @@ describe('DashboardLayout', () => {
icon: <ShoppingCartIcon />,
},
{
segment: 'dynamic',
segment: 'dynamic/override',
title: 'Dynamic Override',
icon: <BarChartIcon />,
},
{
segment: 'dynamicMoreOnly',
title: 'Dynamic',
icon: <BarChartIcon />,
pattern: 'dynamic/:dynamicId',
},
{
segment: 'optional',
segment: 'optionalMoreOnly',
title: 'Optional',
pattern: 'optional{/:optionalId}?',
},
Expand Down Expand Up @@ -268,11 +273,23 @@ describe('DashboardLayout', () => {
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).toHaveClass(
'Mui-selected',
);
expect(
within(desktopNavigation).getByRole('link', { name: 'Dynamic Override' }),
).not.toHaveClass('Mui-selected');
rerender(<AppWithPathname pathname="/dynamic/123/456" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).not.toHaveClass(
'Mui-selected',
);

// Does not show multiple selected items if a dynamic segment is overridden by a more specific segment
rerender(<AppWithPathname pathname="/dynamic/override" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic Override' })).toHaveClass(
'Mui-selected',
);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).not.toHaveClass(
'Mui-selected',
);

rerender(<AppWithPathname pathname="/optional" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Optional' })).toHaveClass(
'Mui-selected',
Expand Down
10 changes: 5 additions & 5 deletions packages/toolpad-core/src/DashboardLayout/DashboardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ export interface DashboardLayoutProps {
*/
navigation?: Navigation;
/**
* Whether the sidebar should not be collapsible to a mini variant in desktop and tablet viewports.
* Whether the sidebar should start collapsed in desktop size screens.
* @default false
*/
disableCollapsibleSidebar?: boolean;
defaultSidebarCollapsed?: boolean;
/**
* Whether the sidebar should start collapsed in desktop size screens.
* Whether the sidebar should not be collapsible to a mini variant in desktop and tablet viewports.
* @default false
*/
defaultSidebarCollapsed?: boolean;
disableCollapsibleSidebar?: boolean;
/**
* Whether the navigation bar and menu icon should be hidden.
* @default false
Expand Down Expand Up @@ -492,7 +492,7 @@ DashboardLayout.propTypes /* remove-proptypes */ = {
*/
disableCollapsibleSidebar: PropTypes.bool,
/**
* Whether the navigation bar and menu icon should be hidden
* Whether the navigation bar and menu icon should be hidden.
* @default false
*/
hideNavigation: PropTypes.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ function DashboardSidebarSubNavigation({
<List
sx={{
padding: 0,
mt: isPopover ? 0.5 : 0,
mt: isPopover && depth === 1 ? 0.5 : 0,
mb: depth === 0 && !isPopover ? 4 : 0.5,
pl: isPopover ? 0 : 2 * depth,
minWidth: isPopover ? 240 : 'auto',
pl: (isPopover ? 1 : 2) * (isPopover ? depth - 1 : depth),
minWidth: isPopover && depth === 1 ? 240 : 'auto',
}}
>
{subNavigation.map((navigationItem, navigationItemIndex) => {
Expand Down Expand Up @@ -189,7 +189,7 @@ function DashboardSidebarSubNavigation({
fontSize: 18,
position: 'absolute',
top: '50%',
right: '-2px',
right: '-1px',
transform: 'translateY(-50%) rotate(-90deg)',
};
} else if (!isMini && isFullyExpanded) {
Expand Down Expand Up @@ -331,6 +331,7 @@ function DashboardSidebarSubNavigation({
subNavigation={navigationItem.children}
depth={depth + 1}
onLinkClick={onLinkClick}
isPopover={isPopover}
/>
</Collapse>
) : null}
Expand Down
11 changes: 5 additions & 6 deletions packages/toolpad-core/src/shared/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ function buildItemLookup(navigation: Navigation) {
console.warn(`Duplicate path in navigation: ${path}`);
}

map.set(path, item);
if (item.pattern) {
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) {
visit(child);
}
} else {
map.set(path, item);
if (item.pattern) {
const basePath = item.segment ? path.slice(0, -item.segment.length) : path;
map.set(pathToRegexp(basePath + item.pattern), item);
}
}
}
};
Expand Down
38 changes: 0 additions & 38 deletions playground/vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,6 @@ const NAVIGATION: Navigation = [
{
title: 'Dashboard',
icon: <DashboardIcon />,
children: [
{
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'orders',
title: 'Orders',
icon: <ShoppingCartIcon />,
},
],
},
{
segment: 'orders',
title: 'Orders',
icon: <ShoppingCartIcon />,
children: [
{
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'orders',
title: 'Orders',
icon: <ShoppingCartIcon />,
children: [
{
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'orders',
title: 'Orders',
icon: <ShoppingCartIcon />,
},
],
},
],
},
{
segment: 'orders',
Expand Down
Loading

0 comments on commit 9acf943

Please sign in to comment.