Skip to content

Feat(md-menu) : Added prevent close( when user clicks inside menu panel) feature #4354

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lib/menu/menu-directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// TODO(kara): prevent-close functionality
// wasn't this supposed to be from an outside developer who needs this feature urgently?

import {
AfterContentInit,
Expand Down Expand Up @@ -54,6 +55,9 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
@ContentChildren(MdMenuItem) items: QueryList<MdMenuItem>;
@Input() overlapTrigger = true;

/** Wether the preventClose for menu instance is asked or not.*/
@Input('preventClose') preventClose: boolean = false;

constructor(@Attribute('xPosition') posX: MenuPositionX,
@Attribute('yPosition') posY: MenuPositionY,
@Attribute('x-position') deprecatedPosX: MenuPositionX,
Expand Down Expand Up @@ -115,6 +119,17 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
this.close.emit();
}

/**
* This emits a close event to which the trigger is subscribed, only when a user
* click inside menu panel and prevent close attribute/input is false. When emitted,
* the trigger will close the menu.
*/
_emitClickCloseEvent(): void {
if (!this.preventClose) {
this.close.emit();
}
}

private _setPositionX(pos: MenuPositionX): void {
if (pos !== 'before' && pos !== 'after') {
throw new MdMenuInvalidPositionX();
Expand Down
3 changes: 1 addition & 2 deletions src/lib/menu/menu.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<ng-template>
<div class="mat-menu-panel" [ngClass]="_classList" (keydown)="_keyManager.onKeydown($event)"
(click)="_emitCloseEvent()" [@transformMenu]="'showing'">
(click)="_emitClickCloseEvent()" [@transformMenu]="'showing'">
<div class="mat-menu-content" [@fadeInItems]="'showing'">
<ng-content></ng-content>
</div>
</div>
</ng-template>