Skip to content

Commit

Permalink
[core] Refix mobile menu closing when link is clicked (#3915)
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira authored Aug 9, 2024
1 parent 3d3e447 commit 53d0ac3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/src/components/landing/ToolpadDashboardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const NAVIGATION: Navigation = [
function DashboardLayoutBasic(props: DemoProps) {
const { window } = props;
const [pathname, setPathname] = React.useState('/page');
const [pathname, setPathname] = React.useState('/dashboard');
const router = React.useMemo<Router>(() => {
return {
Expand Down Expand Up @@ -159,7 +159,7 @@ const PlaceHolder = styled('div')<{ height: number }>(({ theme, height }) => ({
function DashboardLayoutBasic(props: DemoProps) {
const { window } = props;

const [pathname, setPathname] = React.useState('/page');
const [pathname, setPathname] = React.useState('/dashboard');

const router = React.useMemo<Router>(() => {
return {
Expand Down
18 changes: 14 additions & 4 deletions packages/toolpad-core/src/DashboardLayout/DashboardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ interface DashboardSidebarSubNavigationProps {
subNavigation: Navigation;
basePath?: string;
depth?: number;
onSidebarItemClick?: (item: NavigationPageItem) => void;
onSidebarLinkClick?: (item: NavigationPageItem) => void;
validatedItemIds: Set<string>;
uniqueItemPaths: Set<string>;
}
Expand All @@ -157,7 +157,7 @@ function DashboardSidebarSubNavigation({
subNavigation,
basePath = '',
depth = 0,
onSidebarItemClick,
onSidebarLinkClick,
validatedItemIds,
uniqueItemPaths,
}: DashboardSidebarSubNavigationProps) {
Expand All @@ -183,6 +183,15 @@ function DashboardSidebarSubNavigation({
initialExpandedSidebarItemIds,
);

const handleLinkClick = React.useCallback(
(item: NavigationPageItem) => () => {
if (onSidebarLinkClick) {
onSidebarLinkClick(item);
}
},
[onSidebarLinkClick],
);

const handleOpenFolderClick = React.useCallback(
(itemId: string) => () => {
setExpandedSidebarItemIds((previousValue) =>
Expand Down Expand Up @@ -253,6 +262,7 @@ function DashboardSidebarSubNavigation({
: {
LinkComponent: Link,
href: navigationItemFullPath,
onClick: handleLinkClick(navigationItem),
})}
>
{navigationItem.icon ? (
Expand Down Expand Up @@ -298,7 +308,7 @@ function DashboardSidebarSubNavigation({
subNavigation={navigationItem.children}
basePath={navigationItemFullPath}
depth={depth + 1}
onSidebarItemClick={onSidebarItemClick}
onSidebarLinkClick={onSidebarLinkClick}
validatedItemIds={validatedItemIds}
uniqueItemPaths={uniqueItemPaths}
/>
Expand Down Expand Up @@ -371,7 +381,7 @@ function DashboardLayout(props: DashboardLayoutProps) {
<Box component="nav" sx={{ overflow: 'auto', pt: navigation[0]?.kind === 'header' ? 0 : 2 }}>
<DashboardSidebarSubNavigation
subNavigation={navigation}
onSidebarItemClick={handleNavigationItemClick}
onSidebarLinkClick={handleNavigationItemClick}
validatedItemIds={validatedItemIdsRef.current}
uniqueItemPaths={uniqueItemPathsRef.current}
/>
Expand Down

0 comments on commit 53d0ac3

Please sign in to comment.