components icon indicating copy to clipboard operation
components copied to clipboard

Mat-menu second level menu flickers and disappears

Open Howard-Song opened this issue 9 years ago • 3 comments

Bug, feature request, or proposal:

Bug

What is the expected behavior?

When I moved the mouse from the first node of the first-level menu to the second node, the source data of the second-level menu is re-assigned. At this time, the second-level menu appears normally.

What is the current behavior?

When the mouse is focused from the first node of the first level menu to the second node, at the same time onMenuOpen has called and the source data of the second level menu is re-assigned. At this time, the second level menu flickers and disappears. When the second node of the first level menu is clicked again, Normally displays the assigned second level menu. How to avoid the focus flash produced by replacing the secondary menu source data.

html:

<mat-form-field>
  <input type="text" matInput [(ngModel)]="cascaderLabel" [matMenuTriggerFor]="firstMenu">
</mat-form-field>
<div>
  <mat-menu overlapTrigger="true" #firstMenu="matMenu">
    <div *ngFor="let first of data;let index = index">
      <button *ngIf="first.child" mat-menu-item [matMenuTriggerFor]="secondMenu" (onMenuOpen)="firstOpen(index)">{{ first.label }}</button>
      <button *ngIf="!first.child" mat-menu-item (click)="firstClose(index)">{{ first.label }}</button>
    </div>
  </mat-menu>
  <mat-menu overlapTrigger="true" #secondMenu="matMenu">
    <div *ngIf="second">
      <div *ngFor="let item of second;let index = index">
        <button *ngIf="item.child" mat-menu-item [matMenuTriggerFor]="thirdMenu" (onMenuOpen)="secondOpen(index)">{{ item.label }}</button>
        <button *ngIf="!item.child" mat-menu-item (click)="secondClose(index)">{{ item.label }}</button>
      </div>
    </div>
  </mat-menu>
  <mat-menu overlapTrigger="true" #thirdMenu="matMenu">
    <div *ngIf="third">
      <div *ngFor="let item of third;let index = index">
        <button *ngIf="!item.child" mat-menu-item (click)="thirdClose(index)">{{ item.label }}</button>
      </div>
    </div>
  </mat-menu>
</div>

ts:

data = [
    {
      label: '陕西省',
      value: '1.test1',
      child: [
        {
          label: '西安市',
          value: '2.test1',
          child: [
            {
              label: '雁塔区',
              value: '3.test1',
            },
            {
              label: '高新区',
              value: '3.test2',
            },
          ]
        },
        {
          label: '宝鸡市',
          value: '2.test1',
        },
      ]
    },
    {
      label: '浙江省',
      value: '1.test2',
      child: [
        {
          label: '杭州市',
          value: '2.test1',
          child: [
            {
              label: '西湖区',
              value: '3.test1',
            },
            {
              label: '余杭区',
              value: '3.test2',
            },
          ]
        },
        {
          label: '宁波市',
          value: '2.test1',
        },
      ]
    },
  ];
  firstOpen(i) {
    this.firstIndex = i;
    this.data.map((data, index) => {
      if (i == index) {
        this.second = data.child;
      }
    });
    this.cdf.detectChanges();
  }

What are the steps to reproduce?

Providing a StackBlitz/Plunker (or similar) is the best way to get the team to see your issue.
Plunker starter (using on @master): https://goo.gl/uDmqyY
StackBlitz starter (using latest npm release): https://goo.gl/wwnhMV

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular: 5.0.0, Material: 5.0.0-rc0, OS: win10, Typescript: 2.4.2, browsers: chrome 62.0.3202.94

Is there anything else we should know?

I find that example in this,It seem that every first level menu which has second level menu is create new mat-menu,but in my case,that should be three level menu.So this problem show out,if you have some good advice,commit here.Thanks for help!

Howard-Song avatar Nov 24 '17 04:11 Howard-Song

The issue comes from the fact that you have two menu triggers pointing to the same mat-menu instance here:

    <div *ngFor="let first of data;let index = index">
      <button *ngIf="first.child" mat-menu-item [matMenuTriggerFor]="secondMenu" (onMenuOpen)="firstOpen(index)">{{ first.label }}</button>
      <button *ngIf="!first.child" mat-menu-item (click)="firstClose(index)">{{ first.label }}</button>
    </div>

This is a limitation with the way menus are set up at the moment. I'll look into it, but you should be able to work around the issue by creating a separate menu for each of the triggers. E.g. something like:

<div *ngFor="let first of data;let index = index">
  <button *ngIf="first.child" mat-menu-item [matMenuTriggerFor]="secondMenu" (onMenuOpen)="firstOpen(index)">{{ first.label }}</button>
  <button *ngIf="!first.child" mat-menu-item (click)="firstClose(index)">{{ first.label }}</button>

  <mat-menu overlapTrigger="true" #secondMenu="matMenu">
    <div *ngIf="second">
      <div *ngFor="let item of second;let index = index">
        <button *ngIf="item.child" mat-menu-item [matMenuTriggerFor]="thirdMenu" (onMenuOpen)="secondOpen(index)">{{ item.label }}</button>
        <button *ngIf="!item.child" mat-menu-item (click)="secondClose(index)">{{ item.label }}</button>
      </div>
    </div>
  </mat-menu>
</div>

crisbeto avatar Nov 26 '17 11:11 crisbeto

Thanks it worked! I shouldn't use send just according to that array: here my template:

<mat-form-field>
  <input type="text" matInput [(ngModel)]="cascaderLabel" [matMenuTriggerFor]="firstMenu">
</mat-form-field>
<div>
  <mat-menu overlapTrigger="true" #firstMenu="matMenu">
    <div *ngFor="let first of data;let index = index">
      <button *ngIf="first.child" mat-menu-item [matMenuTriggerFor]="secondMenu" (onMenuOpen)="firstOpen(index)">{{ first.label }}</button>
      <button *ngIf="!first.child" mat-menu-item (click)="firstClose(index)">{{ first.label }}</button>

      <mat-menu overlapTrigger="true" #secondMenu="matMenu">
        <div *ngFor="let item of first.child;let secondIndex = index">
          <button *ngIf="item.child" mat-menu-item [matMenuTriggerFor]="thirdMenu" (onMenuOpen)="secondOpen(secondIndex)">{{ item.label }}</button>
          <button *ngIf="!item.child" mat-menu-item (click)="secondClose(secondIndex)">{{ item.label }}</button>

          <mat-menu overlapTrigger="true" #thirdMenu="matMenu">
            <div *ngFor="let thirdChild of item.child;let thirdIndex = index">
              <button *ngIf="!thirdChild.child" mat-menu-item (click)="thirdClose(thirdIndex)">{{ thirdChild.label }}</button>
            </div>
          </mat-menu>
        </div>
      </mat-menu>
    </div>
  </mat-menu>
</div>

Howard-Song avatar Nov 27 '17 01:11 Howard-Song

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.