Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

I'm new to reactjs and material ui I would like to create a drop down in my nav bar

The following below are my codes

I would like to put a dropdown on my UI Elements and Tables on my navbar having trouble to do it

My current Output

Ideal Output

LayoutMain.js

          {links.map(({ icon: Icon, ...link }, index) => (
            <ListItem
              button
              key={link.name}
              onClick={() => gotoRoute(link.route)}
              selected={link.route === location.pathname}
            >
              <ListItemIcon>
                <Icon />
              </ListItemIcon>
              <ListItemText primary={link.name} />
            </ListItem>
          ))}
        </List>

Links.js


export const links = [
  {
    name: 'Dashboard',
    icon: Dashboard,
    route: '/',

  },
  {
    name: 'UI Elements',
    icon: ListAlt,
    route: '/UIElements',
  },
  {
    name: 'General',
    icon: RadioButtonUnchecked,
    route: '/General',

  },
  {
    name: 'Tables',
    icon: TableChart,
    route: '/Tables'
  }
];
question from:https://stackoverflow.com/questions/66048634/im-new-to-reactjs-and-material-ui-i-would-like-to-create-a-drop-down-in-my-nav

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way you could do this is by using an Accordion component from MUI

{links.map(({ icon: Icon, ...link }, index) => {
   if (link.name !== 'UI Elements') {
     return 
       <ListItem
         button
         key={link.name}
         onClick={() => gotoRoute(link.route)}
         selected={link.route === location.pathname}
       >
       <ListItemIcon>
         <Icon />
       </ListItemIcon>
       <ListItemText primary={link.name} />
      </ListItem>
   } else return
     <Accordion>
        <AccordionSummary
          expandIcon={<ExpandMoreIcon />}
          aria-controls="panel1a-content"
          id="panel1a-header"
        >
          <Typography>{link.name}</Typography>
        </AccordionSummary>
        <AccordionDetails>
          {uiElements.map((uiElement) => (
            <ListItem>
             <ListItemIcon>
              <Icon />
             </ListItemIcon>
             <ListItemText primary={link.name} />
            </ListItem>
          )}
        </AccordionDetails>
      </Accordion>
 })}
</List>

You would need another array of uiElements inside your uiElements object to map through with the additional information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...