Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

material ui sidebar without hooks

import React, {  useState, forwardRef } from 'react';
import {List, ListItem, Collapse, Button, Drawer } from '@material-ui/core';
import clsx from 'clsx';
import { ExpandLess, ExpandMore } from "@material-ui/icons";
import menuItems from './sideBarItems';
import { NavLink as RouterLink } from 'react-router-dom';
import useStyles from './menuBarStyles';const MenuBar = (props) => {
    const [ menu, setMenu ] = useState({});
    const { className, ...rest } = props;
    const classes  = useStyles();
    const handleClick = (item) => {
        let newData = {...menu, [item] : !menu[item]};
        setMenu(newData);
    }
    const CustomRouterLink = forwardRef((props, ref) => (
      <div ref={ref} style={{ flexGrow: 1 }}>
        <RouterLink {...props} />
      </div>
    ));
    const handleMenu = ( children, level=0 ) => {
        return children.map(({children, name, url, links }) => {
            if ( !children ) {
              return (
                <List component="div" disablePadding key={ name }>
                  <ListItem
                    className={classes.item}
                    disableGutters
                    style={{padding:"0px"}}
                    key={name}
                  >
                    <Button
                      className={clsx({
                        [classes.btnRoot] : true,
                        [classes.button] : true,
                        [classes.subMenu] : level
                      })}
                      component={CustomRouterLink}
                      to={url}
                    >
                      {name}
                    </Button>
                  </ListItem>
                </List>
              )
            }
            return (
              <div key={ name }>
              <ListItem
                className={classes.item}
                disableGutters
                key={name}
                onClick={() => handleClick(name)}
              >
                <Button
                className={clsx({
                  [classes.btnRoot] : true,
                  [classes.button] : true,
                  [classes.subMenu] : level
                })}>
                  { name } { menu[ name ] ? <ExpandLess /> : <ExpandMore />}
                  </Button>
                </ListItem>
                <Collapse
                  in={ (menu[name]) ? true : false }
                  timeout="auto"
                  unmountOnExit
                >
                  { handleMenu( children, 1) }
                </Collapse>
              </div>
            )
        })
    }return (
      <Drawer
          anchor="left"
          classes={{ paper: classes.drawer }}
          open={true}
          variant="persistent"
        >
          <List {...rest} className={clsx(classes.root, className)} >
              { handleMenu(menuItems.data) }
          </List>
      </Drawer>
   )
}export default MenuBar;
Comment

PREVIOUS NEXT
Code Example
Javascript :: Find Largest Number by function by javascript 
Javascript :: javascript start 
Javascript :: arrays 
Javascript :: js await 
Javascript :: sequelize migration limit 
Javascript :: typescript base64 from file 
Javascript :: javascript infinity 
Javascript :: how to delete object in array 
Javascript :: map.set javascript 
Javascript :: rename files in folder 
Javascript :: ReferenceError: document is not defined 
Javascript :: using mongoose with node js 
Javascript :: Sets can be used to store __________. in js 
Javascript :: convert json data into html table 
Javascript :: if is a string javascript 
Javascript :: redux form 
Javascript :: react variable component 
Javascript :: js function definition 
Javascript :: working with multiple db in single query mongodb 
Javascript :: setimmediate javascript 
Javascript :: Force users to update your application in React Native 
Javascript :: how to declare 3d array in javascript 
Javascript :: react router refreshes page 
Javascript :: reactjs change favicon 
Javascript :: javascript block link action 
Javascript :: move li to bottom of list jquery selected value 
Javascript :: dart get vfirst key value of map 
Javascript :: how to embed element in to array 
Javascript :: javascript popup canvas 
Javascript :: iterate over all check box in a div 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =