Skip to content

Commit

Permalink
fix logo click & item collapse issue (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
ct-jahidhasan authored Feb 11, 2023
1 parent cfed7d5 commit 1fa10ae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/layout/MainLayout/LogoSection/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Link } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';

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

// project imports
import config from 'config';
import Logo from 'ui-component/Logo';
import { MENU_OPEN } from 'store/actions';

// ==============================|| MAIN LOGO ||============================== //

const LogoSection = () => (
<ButtonBase disableRipple component={Link} to={config.defaultPath}>
<Logo />
</ButtonBase>
);
const LogoSection = () => {
const defaultId = useSelector((state) => state.customization.defaultId);
const dispatch = useDispatch();
return (
<ButtonBase disableRipple onClick={() => dispatch({ type: MENU_OPEN, id: defaultId })} component={Link} to={config.defaultPath}>
<Logo />
</ButtonBase>
);
};

export default LogoSection;
32 changes: 31 additions & 1 deletion src/layout/MainLayout/Sidebar/MenuList/NavCollapse/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { useLocation } from 'react-router';

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

const { pathname } = useLocation();
const checkOpenForParent = (child, id) => {
child.forEach((item) => {
if (item.url === pathname) {
setOpen(true);
setSelected(id);
}
});
};

// menu collapse for sub-levels
useEffect(() => {
setOpen(false);
setSelected(null);
if (menu.children) {
menu.children.forEach((item) => {
if (item.children?.length) {
checkOpenForParent(item.children, menu.id);
}
if (item.url === pathname) {
setSelected(menu.id);
setOpen(true);
}
});
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pathname, menu.children]);

// menu collapse & item
const menus = menu.children?.map((item) => {
switch (item.type) {
Expand Down
1 change: 1 addition & 0 deletions src/store/customizationReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as actionTypes from './actions';

export const initialState = {
isOpen: [], // for active default menu
defaultId: 'default',
fontFamily: config.fontFamily,
borderRadius: config.borderRadius,
opened: true
Expand Down

0 comments on commit 1fa10ae

Please sign in to comment.