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.

RFC: Support submenus in Menu `items` shorthand prop

See original GitHub issue

Problem

Currently, the Menu component allows defining items as a shorthand prop:

const items = [
  'One',
  { key: 'two', content: 'Two' },  // props object usage shown here
  'Three',
]

<Menu vertical items={items} />

image

A Menu can also have a submenu, by placing a Menu.Menu in a Menu.Item:

<Menu vertical>
  <Menu.Item>Top Level Item 1</Menu.Item>
  <Menu.Item>
     Top Level Item 2
      <Menu.Menu>
        <Menu.Item>Submenu Item 1</Menu.Item>
        <Menu.Item>Submenu Item 2</Menu.Item>
      </Menu.Menu>
    </Menu.Item>
  <Menu.Item>Top Level Item 3</Menu.Item>
</Menu>

image

However, there is no way to add a submenu when using the items prop shorthand definition.

Proposed Solution

It would be great to be able to define a submenu as a key on an item’s shorthand object:

const items = [
  'Top Level Item 1',
  {
    key: 'item-2',
    content: 'Top Level Item 2',
    menu: [
      'Sub Item 1',
      'Sub Item 2',
    ],
  },
  'Top Level Item 3',
]

<Menu vertical items={items} />

image

By default, this would enable support for <Menu.Item menu={...} />, as the menu config above would be passed to the menu item’s menu prop.

Handling Callbacks and Active Item State

What is unclear from here is how the onItemClick callback and activeItemIndex would be handled.

One idea is to use simply disregard the nested nature of the submenu’s items when traversing indices and consider the index of an item to be its natural order in the menu. The example above would have 5 menu items where:

  • index 0 = Top Level Item 1
  • index 1 = Top Level Item
  • index 2 = Sub Item 1
  • index 3 = Sub Item 2
  • index 4 = Top Level Item 3

Given the above proposed submenu shorthand config, we’d render like so:

<Menu vertical items={items} activeItemIndex={2} />

image

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
GregoryPotdevincommented, Apr 19, 2017

Started playing around with this. The version with activeIndex is actually very straightforward to build. Might be tough to have both activeIndex and activeItem at the same time, though.

Here’s a quick hack, didn’t prepare a PR yet as I just wanted to discuss the idea

  renderItems() {
    const { items } = this.props
    const { activeIndex } = this.state
    
    let globalIndex = 0

    const renderItem = (item) => {
      if (item.menu){
        return (
          <Menu.Item key={item.key || item.name}>
            <Menu.Header>{item.content || item.name}</Menu.Header>
            <Menu.Menu>
              {_.map(item.menu, renderItem)}
            </Menu.Menu>
          </Menu.Item>
        )
      } else {
        const index = globalIndex++;
        return (
          MenuItem.create(item, {
            defaultProps: {
              active: activeIndex === index,
              index,
            },
            overrideProps: this.handleItemOverrides,
          })
        )
      }
    }
    
    return _.map(items, renderItem)
  }

The main idea is to test if it’s a menu, and use a global index counter instead of the map function’s index.

It should be pretty simple to replace activeIndex with activeItem (and pass an activeIndex prefix to renderItem, and update handleItemOverrides to use it). Just not sure how you want to handle backwards compatibility for this.

0reactions
JonSilvercommented, Nov 19, 2020

So this was a highly popular suggestion, well discussed, but was eventually auto-closed as stale… was it ever implemented in any form?

Read more comments on GitHub >

github_iconTop Results From Across the Web

header menu item with submenus - ServiceNow Community
In a portal menu, I have a menu item (Personnel) with 3 submenu items. Instead I would like to have another level of...
Read more >
Embedded Menu Manager Configuration Guide, Cisco IOS ...
This feature provides a custom menu-driven application that allows user accounts to be set up to automatically run menu systems when the users...
Read more >
Menu - Semantic UI React
Menu item text can be defined with the content prop. ... A menu item may contain another menu nested inside that acts as...
Read more >
How to create a multilevel dropdown menu in React
Also in the code, we imported the Dropdown component and passed the submenu items via the prop. More great articles from LogRocket: Don't...
Read more >
c%23 creating json object
Creating JSON from an object The last option is to create a JSON object from ... First, we have to create two SQL...
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