Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MatMenuTrigger): null should be a valid value, in order to have conditional submenus #24030

Closed
jnigro opened this issue Nov 29, 2021 · 2 comments · Fixed by #24437
Closed
Assignees
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@jnigro
Copy link

jnigro commented Nov 29, 2021

Feature Description

Very often we find the need to have a menu, where their submenus are conditional. That is, they may or may not exist, base on a condition.

Ideally, the following should work:

<button mat-menu-item [matMenuTriggerFor]="myCondition ? subMenu : null">{{ name }}</button>

Current Workaround

However, since matMenuTriggerFor cannot be null, the above code does not compile. The current solution is duplicating code with *ngIf:

<button mat-menu-item *ngIf="myCondition" [matMenuTriggerFor]="subMenu">{{ name }}</button>
<button mat-menu-item *ngIf="!myCondition">{{ name }}</button>

Why This is Needed

At first sight, it doesn't look that terrible, but as we start to add other attributes (classes, i18n suff, and other directives), and as the button content also becomes richer (with icons and internationalized text). The code duplication is hard to maintain and can easily introduce bugs in our app.

For example, for the following code:

<button mat-menu-item  class="myclass1 myclass2" (onClick)="doSomething() [matMenuTriggerFor]="subMenu">
      <mat-icon>check</mat-icon>
      <span class="myClass3">{{ name }}</span>
</button>

with the current workaround, the code grows to:

<button mat-menu-item  *ngIf="myCondition" class="myclass1 myclass2" i18n="@@buttonDesc" (onClick)="doSomething() [matMenuTriggerFor]="subMenu">
      <mat-icon>check</mat-icon>
      <span class="myClass3"  i18n="@@buttonDesc">{{ name }}</span>
</button>
<button mat-menu-item  *ngIf="!myCondition" class="myclass1 myclass2" (onClick)="doSomething()>
      <mat-icon>check</mat-icon>
      <span class="myClass3"  i18n="@@buttonDesc">{{ name }}</span>
</button>

with this proposal, the code would only need to be modified slightly:

<button mat-menu-item  class="myclass1 myclass2" (onClick)="doSomething() [matMenuTriggerFor]="myCondition ? subMenu : null">
      <mat-icon>check</mat-icon>
      <span class="myClass3"  i18n="@@buttonDesc">{{ name }}</span>
</button>

Note: Creating a bogus empty submenu does not work as a workaround.

Use Case

Very often I find the need to have a menu, where their submenus are conditional. That is, they may or may not exist, base on a condition.

@jnigro jnigro added feature This issue represents a new feature or feature request rather than a bug or bug fix needs triage This issue needs to be triaged by the team labels Nov 29, 2021
@wagnermaciel wagnermaciel added P5 The team acknowledges the request but does not plan to address it, it remains open for discussion and removed needs triage This issue needs to be triaged by the team labels Nov 29, 2021
@crisbeto crisbeto added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed P5 The team acknowledges the request but does not plan to address it, it remains open for discussion labels Feb 16, 2022
@crisbeto
Copy link
Member

Bumping the priority, because this has come up multiple times.

@crisbeto crisbeto self-assigned this Feb 17, 2022
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 17, 2022
…trigger

Adds support for conditionally removing the menu from a menu trigger by passing in `null`.

Fixes angular#24030.
andrewseguin pushed a commit that referenced this issue Feb 25, 2022
…trigger (#24437)

Adds support for conditionally removing the menu from a menu trigger by passing in `null`.

Fixes #24030.
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Mar 28, 2022
forsti0506 pushed a commit to forsti0506/components that referenced this issue Apr 3, 2022
…trigger (angular#24437)

Adds support for conditionally removing the menu from a menu trigger by passing in `null`.

Fixes angular#24030.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants