Skip to content

Commit 1fa10ae

Browse files
fix logo click & item collapse issue (#57)
1 parent cfed7d5 commit 1fa10ae

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import { Link } from 'react-router-dom';
2+
import { useDispatch, useSelector } from 'react-redux';
23

34
// material-ui
45
import { ButtonBase } from '@mui/material';
56

67
// project imports
78
import config from 'config';
89
import Logo from 'ui-component/Logo';
10+
import { MENU_OPEN } from 'store/actions';
911

1012
// ==============================|| MAIN LOGO ||============================== //
1113

12-
const LogoSection = () => (
13-
<ButtonBase disableRipple component={Link} to={config.defaultPath}>
14-
<Logo />
15-
</ButtonBase>
16-
);
14+
const LogoSection = () => {
15+
const defaultId = useSelector((state) => state.customization.defaultId);
16+
const dispatch = useDispatch();
17+
return (
18+
<ButtonBase disableRipple onClick={() => dispatch({ type: MENU_OPEN, id: defaultId })} component={Link} to={config.defaultPath}>
19+
<Logo />
20+
</ButtonBase>
21+
);
22+
};
1723

1824
export default LogoSection;

src/layout/MainLayout/Sidebar/MenuList/NavCollapse/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import PropTypes from 'prop-types';
2-
import { useState } from 'react';
2+
import { useEffect, useState } from 'react';
33
import { useSelector } from 'react-redux';
4+
import { useLocation } from 'react-router';
45

56
// material-ui
67
import { useTheme } from '@mui/material/styles';
@@ -27,6 +28,35 @@ const NavCollapse = ({ menu, level }) => {
2728
setSelected(!selected ? menu.id : null);
2829
};
2930

31+
const { pathname } = useLocation();
32+
const checkOpenForParent = (child, id) => {
33+
child.forEach((item) => {
34+
if (item.url === pathname) {
35+
setOpen(true);
36+
setSelected(id);
37+
}
38+
});
39+
};
40+
41+
// menu collapse for sub-levels
42+
useEffect(() => {
43+
setOpen(false);
44+
setSelected(null);
45+
if (menu.children) {
46+
menu.children.forEach((item) => {
47+
if (item.children?.length) {
48+
checkOpenForParent(item.children, menu.id);
49+
}
50+
if (item.url === pathname) {
51+
setSelected(menu.id);
52+
setOpen(true);
53+
}
54+
});
55+
}
56+
57+
// eslint-disable-next-line react-hooks/exhaustive-deps
58+
}, [pathname, menu.children]);
59+
3060
// menu collapse & item
3161
const menus = menu.children?.map((item) => {
3262
switch (item.type) {

src/store/customizationReducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as actionTypes from './actions';
66

77
export const initialState = {
88
isOpen: [], // for active default menu
9+
defaultId: 'default',
910
fontFamily: config.fontFamily,
1011
borderRadius: config.borderRadius,
1112
opened: true

0 commit comments

Comments
 (0)