akveo / nebular

:boom: Customizable Angular UI Library based on Eva Design System :new_moon_with_face::sparkles:Dark Mode
https://akveo.github.io/nebular
MIT License
8.05k stars 1.51k forks source link

menu does not expand correctly #444

Closed ruisebastiao closed 6 years ago

ruisebastiao commented 6 years ago

Issue type

I'm submitting a ... (check one with "x")

Issue description

Current behavior:

when sub-menu items are added asynchronous the parent menu does not expand correctly if a sub-menu is selected and a page reload occurs.

peek 2018-05-24 16-04

Expected behavior:

Steps to reproduce:

Related code:


  ngAfterViewInit(): void {

    this.userService.currentUser.takeUntil(this._destroyed).subscribe(user => {
      if (user && user.units) {
        const molds_menu = [];

        user.units.forEach(unit => {
          molds_menu.push({
            title: unit.userdata.unit_description,
            link: '/pages/unit/' + unit.serialnumber,
            tag: 'unitchild'
          });
        });

        this.menu[1].children = molds_menu;

        this.changedetectorref.detectChanges();
      }
    });
  }

Other information:

npm, node, OS, Browser

<!--
Node, npm: `node --version` and `npm --version`
OS: Windows (7/8/10). Linux (incl. distribution). macOS (El Capitan? Sierra?)
Browser: Chrome/Safari/Firefox/etc?
-->

Angular, Nebular

<!--
Check your `package-lock.json` or locate a `package.json` in the `node_modules` folder.
-->
ruisebastiao commented 6 years ago

this is how i solve this problem:

when the submenu is changed at run-time and if the and the

expand property is false the submenuhigh is not calculated and gets value 0 as we can see in the function: https://github.com/akveo/nebular/blob/16314e8fb7dcebe23aca3b754d277e20a726597f/src/framework/theme/components/menu/menu.component.ts#L32

function sumSubmenuHeight(item: NbMenuItem) {
  return item.expanded
    ? (item.subMenuHeight || 0) + item.children.filter(c => c.children).reduce((acc, c) => acc + sumSubmenuHeight(c), 0)
    : 0;
}

so i changed my code and set the expanded variable to true before setting the submenu:


        this.menu[1].expanded = true;
        this.menu[1].children = molds_menu;

        setTimeout(() => {
          this.changedetectorref.detectChanges();
        }, 250);
nnixaa commented 6 years ago

Hi @ruisebastiao, thanks for reporting!

nnixaa commented 6 years ago

Hey @ruisebastiao, have you tried NbMenuService.addItems() instead of modifying the variable by reference? Is it working properly in that case?

ruisebastiao commented 6 years ago

Hi @nnixaa , the only async data are the menu children, how can i use addItems to add children items? But i try to use the addItems to add the root Item with the children data and the problem persists and now the child item doesn't get selected, here is my code:


 this.userService.currentUser.takeUntil(this._destroyed).subscribe(user => {
      if (user && user.units) {
        const molds_menu = [];

        user.units.forEach(unit => {
          molds_menu.push({
            title: unit.userdata.unit_description,
            link: '/pages/unit/' + unit.serialnumber,
            tag: 'unitchild'
          });
        });

         this.menuService.addItems([
         {
             title: 'Molds',
             icon: 'ion-ios-browsers-outline',
             children: molds_menu
           }
         ]);
        this.changedetectorref.detectChanges();

      }
    });
nnixaa commented 6 years ago

https://github.com/akveo/nebular/issues/532

nnixaa commented 6 years ago

https://github.com/akveo/ngx-admin/issues/1703