Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom menu #6635

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/Theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -779,20 +779,20 @@ You can create a custom menu component using the `<DashboardMenuItem>` and `<Men
```jsx
// in src/Menu.js
import * as React from 'react';
import { DashboardMenuItem, MenuItemLink } from 'react-admin';
import { DashboardMenuItem, Menu, MenuItemLink } from 'react-admin';
import BookIcon from '@material-ui/icons/Book';
import ChatBubbleIcon from '@material-ui/icons/ChatBubble';
import PeopleIcon from '@material-ui/icons/People';
import LabelIcon from '@material-ui/icons/Label';

export const Menu = () => (
<div>
<Menu {...props}>
<DashboardMenuItem />
<MenuItemLink to="/posts" primaryText="Posts" leftIcon={<BookIcon />}/>
<MenuItemLink to="/comments" primaryText="Comments" leftIcon={<ChatBubbleIcon />}/>
<MenuItemLink to="/users" primaryText="Users" leftIcon={<PeopleIcon />}/>
<MenuItemLink to="/custom-route" primaryText="Miscellaneous" leftIcon={<LabelIcon />}/>
</div>
</Menu>
);
```

Expand Down Expand Up @@ -827,14 +827,14 @@ import * as React from 'react';
import { createElement } from 'react';
import { useSelector } from 'react-redux';
import { useMediaQuery } from '@material-ui/core';
import { DashboardMenuItem, MenuItemLink, getResources } from 'react-admin';
import { DashboardMenuItem, Menu, MenuItemLink, getResources } from 'react-admin';
import DefaultIcon from '@material-ui/icons/ViewList';
import LabelIcon from '@material-ui/icons/Label';

export const Menu = () => {
export const Menu = (props) => {
const resources = useSelector(getResources);
return (
<div>
<Menu {...props}>
<DashboardMenuItem />
{resources.map(resource => (
<MenuItemLink
Expand All @@ -852,7 +852,7 @@ export const Menu = () => {
/>
))}
{/* add your custom menus here */}
</div>
</Menu>
);
};
```
Expand Down
53 changes: 32 additions & 21 deletions packages/ra-ui-materialui/src/layout/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,45 @@ const useStyles = makeStyles(
);

const Menu = (props: MenuProps) => {
const resources = useSelector(getResources, shallowEqual) as Array<any>;
const getResourceLabel = useGetResourceLabel();
const {
hasDashboard,
dense,
children = (
<>
{hasDashboard && <DashboardMenuItem dense={dense} />}
{resources
.filter(r => r.hasList)
.map(resource => (
<MenuItemLink
key={resource.name}
to={{
pathname: `/${resource.name}`,
state: { _scrollToTop: true },
}}
primaryText={getResourceLabel(resource.name, 2)}
leftIcon={
resource.icon ? (
<resource.icon />
) : (
<DefaultIcon />
)
}
dense={dense}
/>
))}
</>
),
classes: classesOverride,
className,
dense,
hasDashboard,
onMenuClick,
logout,
...rest
} = props;
const classes = useStyles(props);
const open = useSelector((state: ReduxState) => state.admin.ui.sidebarOpen);
const resources = useSelector(getResources, shallowEqual) as Array<any>;
const getResourceLabel = useGetResourceLabel();

return (
<div
className={classnames(
Expand All @@ -67,28 +93,13 @@ const Menu = (props: MenuProps) => {
)}
{...rest}
>
{hasDashboard && <DashboardMenuItem dense={dense} />}
{resources
.filter(r => r.hasList)
.map(resource => (
<MenuItemLink
key={resource.name}
to={{
pathname: `/${resource.name}`,
state: { _scrollToTop: true },
}}
primaryText={getResourceLabel(resource.name, 2)}
leftIcon={
resource.icon ? <resource.icon /> : <DefaultIcon />
}
dense={dense}
/>
))}
{children}
</div>
);
};

export interface MenuProps {
children?: ReactNode;
classes?: object;
className?: string;
dense?: boolean;
Expand Down