question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When a child Menu.Item was selected, its parent SubMenu should be expanded

See original GitHub issue

What problem does this feature solve?

Suppose we have a menu:

<Menu defaultSelectedKeys={['sub-1', 'sub-sub-1']}>
  <Menu.SubMenu key='sub'>
    <Menu.Item key='sub-1'>
    </Menu.Item>
    <Menu.SubMenu key='sub-sub'>
      <Menu.Item key='sub-sub-1'>
      </Menu.Item>
    </Menu.SubMenu>
  </Menu.SubMenu>
</Menu>

Since defaultSelectedKeys is [‘sub-1’, ‘sub-sub-1’], the SubMenu sub and sub-sub should be automatically expanded.

What does the proposed API look like?

FYI

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
theodorDiaconucommented, Apr 10, 2019

@afc163 why did you close the ticket? It seems like a useful feature, you can leave the default you prefer, but implementing this can be somewhat hard. Think about this scenario: you have a submenu called Users and under it “List”/“New” you would like that when you click “New” to change the route, and keep Users menu expanded, otherwise it results in weird behaviour.

11reactions
theodorDiaconucommented, Apr 10, 2019

Maybe another developer hits this and I save him 15-20 mins of his time:

import React from 'react';
import { Menu, Icon } from 'antd';
import { withRouter } from 'react-router';

const SubMenu = Menu.SubMenu;

const menu = [
  { icon: 'pie-chart', label: 'Items List', path: '/items/list' },
  {
    icon: 'pie-chart',
    label: 'Items',
    key: 'items',
    isSelected: path => path.indexOf('/items') === 0,
    items: [
      { icon: 'pie-chart', label: 'List', path: '/items/list' },
      { icon: 'pie-chart', label: 'New', path: '/items/new' },
    ],
  },
];

class MainMenu extends React.Component {
  render() {
    const { pathname } = this.props.location;
    const selectedOrOpenKeys = this.getSelectedKeys(menu, pathname);

    return (
      <Menu
        theme="dark"
        defaultSelectedKeys={['1']}
        mode="inline"
        defaultOpenKeys={selectedOrOpenKeys}
        defaultSelectedKeys={selectedOrOpenKeys}
      >
        {menu.map(this.renderMenuItem)}
      </Menu>
    );
  }

  getSelectedKeys(items, pathname) {
    let selectedKeys = [];

    items.map(item => {
      if (item.isSelected) {
        if (item.isSelected(pathname)) {
          selectedKeys.push(item.key ? item.key : item.path);
        }
      } else {
        if (item.path === pathname || item.key === pathname) {
          selectedKeys.push(item.key ? item.key : item.path);
        }
      }
      if (item.items) {
        const itemKeys = this.getSelectedKeys(item.items, pathname);
        selectedKeys.push(...itemKeys);
      }
    });

    return selectedKeys;
  }

  renderMenuItem = item => {
    const { label, path, items, icon, key, ...rest } = item;

    if (item.items) {
      const title = (
        <span>
          {icon && <Icon type={icon} />}
          <span>{label}</span>
        </span>
      );
      return (
        <SubMenu key={key || path} title={title}>
          {items.map(this.renderMenuItem)}
        </SubMenu>
      );
    }

    return (
      <Menu.Item key={key || path} {...rest}>
        {icon && <Icon type="file" />}
        <span onClick={() => this.props.history.push(path)}>{label}</span>
      </Menu.Item>
    );
  };
}

export default withRouter(MainMenu);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to open/close antd Submenu programatically
What I'm trying to do is, I need to collapse/expand the SubMenu once it's enabled via the prop. Here is my code. I...
Read more >
Menu - Ant Design
Your customized node should be wrapped by Menu.Item . Menu needs to collect its node structure, so its children should be Menu.* or...
Read more >
Is there a way for menu parent “#” itself to expand submenu ...
I have users that click the parent name (which does nothing as I set to custom url “#” to be just a menu...
Read more >
Keep submenu expanded when parent or children are active
I have a two-tiered menu system using the Nice menus module, which I've adapted to run horizontally with the children beneath the top...
Read more >
Auto-expand parent menu item(s) for active menu item - Drupal
Then, Menu Item 1 and SubMenuItem1 should both have the "open" class ... If there are submenu items we assign the parent a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found