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(menu): not emitting close event when closing via the backdrop #3300

Merged
merged 3 commits into from
Mar 9, 2017
Merged
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
4 changes: 2 additions & 2 deletions src/lib/menu/menu-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
}

private _setPositionX(pos: MenuPositionX): void {
if ( pos !== 'before' && pos !== 'after') {
if (pos !== 'before' && pos !== 'after') {
throw new MdMenuInvalidPositionX();
}
this.positionX = pos;
}

private _setPositionY(pos: MenuPositionY): void {
if ( pos !== 'above' && pos !== 'below') {
if (pos !== 'above' && pos !== 'below') {
throw new MdMenuInvalidPositionY();
}
this.positionY = pos;
Expand Down
1 change: 1 addition & 0 deletions src/lib/menu/menu-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface MdMenuPanel {
close: EventEmitter<void>;
focusFirstItem: () => void;
setPositionClasses: (x: MenuPositionX, y: MenuPositionY) => void;
_emitCloseEvent: () => void;
}
4 changes: 2 additions & 2 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {MenuPositionX, MenuPositionY} from './menu-positions';
* responsible for toggling the display of the provided menu instance.
*/
@Directive({
selector: `[md-menu-trigger-for], [mat-menu-trigger-for],
selector: `[md-menu-trigger-for], [mat-menu-trigger-for],
[mdMenuTriggerFor], [matMenuTriggerFor]`,
host: {
'aria-haspopup': 'true',
Expand Down Expand Up @@ -144,7 +144,7 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
*/
private _subscribeToBackdrop(): void {
this._backdropSubscription = this._overlayRef.backdropClick().subscribe(() => {
this.closeMenu();
this.menu._emitCloseEvent();
});
}

Expand Down
34 changes: 33 additions & 1 deletion src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,34 @@ describe('MdMenu', () => {

});

describe('close event', () => {
let fixture: ComponentFixture<SimpleMenu>;

beforeEach(() => {
fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();
});

it('should emit an event when a menu item is clicked', () => {
const menuItem = overlayContainerElement.querySelector('[md-menu-item]') as HTMLElement;

menuItem.click();
fixture.detectChanges();

expect(fixture.componentInstance.closeCallback).toHaveBeenCalled();
});

it('should emit a close event when the backdrop is clicked', () => {
const backdrop = <HTMLElement>overlayContainerElement.querySelector('.cdk-overlay-backdrop');

backdrop.click();
fixture.detectChanges();

expect(fixture.componentInstance.closeCallback).toHaveBeenCalled();
});
});

describe('destroy', () => {
it('does not throw an error on destroy', () => {
const fixture = TestBed.createComponent(SimpleMenu);
Expand All @@ -395,7 +423,7 @@ describe('MdMenu', () => {
@Component({
template: `
<button [mdMenuTriggerFor]="menu" #triggerEl>Toggle menu</button>
<md-menu #menu="mdMenu">
<md-menu #menu="mdMenu" (close)="closeCallback()">
<button md-menu-item> Item </button>
<button md-menu-item disabled> Disabled </button>
</md-menu>
Expand All @@ -404,6 +432,7 @@ describe('MdMenu', () => {
class SimpleMenu {
@ViewChild(MdMenuTrigger) trigger: MdMenuTrigger;
@ViewChild('triggerEl') triggerEl: ElementRef;
closeCallback = jasmine.createSpy('menu closed callback');
}

@Component({
Expand Down Expand Up @@ -456,6 +485,9 @@ class CustomMenuPanel implements MdMenuPanel {
@Output() close = new EventEmitter<void>();
focusFirstItem = () => {};
setPositionClasses = () => {};
_emitCloseEvent() {
this.close.emit();
}
}

@Component({
Expand Down