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

Fix(md-menu) : Added prevent close( when user clicks inside menu panel) feature. #3897

Closed
wants to merge 6 commits into from
Closed
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
26 changes: 23 additions & 3 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {MenuPositionX, MenuPositionY} from './menu-positions';
host: {
'aria-haspopup': 'true',
'(mousedown)': '_handleMousedown($event)',
'(click)': 'toggleMenu()',
'(click)': 'toggleMenu($event)',
},
exportAs: 'mdMenuTrigger'
})
Expand All @@ -54,6 +54,9 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
// the first item of the list when the menu is opened via the keyboard
private _openedByMouse: boolean = false;

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

/** @deprecated */
@Input('md-menu-trigger-for')
get _deprecatedMdMenuTriggerFor(): MdMenuPanel { return this.menu; }
Expand Down Expand Up @@ -93,8 +96,25 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
get menuOpen(): boolean { return this._menuOpen; }

/** Toggles the menu between the open and closed states. */
toggleMenu(): void {
return this._menuOpen ? this.closeMenu() : this.openMenu();
toggleMenu(event): void {
/** whent it's a void function why there is a return statement? */
/** Removed `return` */

/** Checks if preventClose is defined and has true value or not. */
if (this.preventClose) {
/**
* `this._element.nativeElement.contains(event.target)`.
* Checks if the target, where click was fired, falls inside menu panel or not.
*/
if (this._menuOpen && !this._element.nativeElement.contains(event.target)) {
this.closeMenu();
} else {
this.openMenu();
}
} else {
/** If no `preventClose` is defined, it will go same old way. */
this._menuOpen ? this.closeMenu() : this.openMenu();
}
}

/** Opens the menu. */
Expand Down