From 1cbc2a487097a9245a0501d5df8964aa97b2a973 Mon Sep 17 00:00:00 2001 From: Austin Date: Thu, 26 Oct 2017 14:13:06 -0500 Subject: [PATCH 1/3] fix(menu): make @Output names consistent #6677 --- src/lib/menu/menu-directive.ts | 24 +++++++++++++++--------- src/lib/menu/menu-item.ts | 6 +++--- src/lib/menu/menu-panel.ts | 2 +- src/lib/menu/menu-trigger.ts | 32 ++++++++++++++++++++++---------- src/lib/menu/menu.html | 2 +- src/lib/menu/menu.spec.ts | 8 ++++---- 6 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/lib/menu/menu-directive.ts b/src/lib/menu/menu-directive.ts index b7d2fbd29b65..7c8970b27391 100644 --- a/src/lib/menu/menu-directive.ts +++ b/src/lib/menu/menu-directive.ts @@ -142,7 +142,13 @@ export class MatMenu implements AfterContentInit, MatMenuPanel, OnDestroy { } /** Event emitted when the menu is closed. */ - @Output() close = new EventEmitter(); + @Output() closed = new EventEmitter(); + + /** + * Event emitted when the menu is closed. + * @deprecated Switch to `closed` instead + */ + @Output() close = this.closed; constructor( private _elementRef: ElementRef, @@ -156,22 +162,22 @@ export class MatMenu implements AfterContentInit, MatMenuPanel, OnDestroy { ngOnDestroy() { this._tabSubscription.unsubscribe(); - this.close.emit(); - this.close.complete(); + this.closed.emit(); + this.closed.complete(); } /** Stream that emits whenever the hovered menu item changes. */ - hover(): Observable { + _hovered(): Observable { if (this.items) { return RxChain.from(this.items.changes) .call(startWith, this.items) - .call(switchMap, (items: MatMenuItem[]) => merge(...items.map(item => item.hover))) + .call(switchMap, (items: MatMenuItem[]) => merge(...items.map(item => item._hovered))) .result(); } return RxChain.from(this._ngZone.onStable.asObservable()) .call(first) - .call(switchMap, () => this.hover()) + .call(switchMap, () => this._hovered()) .result(); } @@ -179,17 +185,17 @@ export class MatMenu implements AfterContentInit, MatMenuPanel, OnDestroy { _handleKeydown(event: KeyboardEvent) { switch (event.keyCode) { case ESCAPE: - this.close.emit('keydown'); + this.closed.emit('keydown'); event.stopPropagation(); break; case LEFT_ARROW: if (this.parentMenu && this.direction === 'ltr') { - this.close.emit('keydown'); + this.closed.emit('keydown'); } break; case RIGHT_ARROW: if (this.parentMenu && this.direction === 'rtl') { - this.close.emit('keydown'); + this.closed.emit('keydown'); } break; default: diff --git a/src/lib/menu/menu-item.ts b/src/lib/menu/menu-item.ts index 96c04201310e..e985c43dabd2 100644 --- a/src/lib/menu/menu-item.ts +++ b/src/lib/menu/menu-item.ts @@ -51,7 +51,7 @@ export class MatMenuItem extends _MatMenuItemMixinBase implements FocusableOptio OnDestroy { /** Stream that emits when the menu item is hovered. */ - hover: Subject = new Subject(); + _hovered: Subject = new Subject(); /** Whether the menu item is highlighted. */ _highlighted: boolean = false; @@ -69,7 +69,7 @@ export class MatMenuItem extends _MatMenuItemMixinBase implements FocusableOptio } ngOnDestroy() { - this.hover.complete(); + this._hovered.complete(); } /** Used to set the `tabindex`. */ @@ -93,7 +93,7 @@ export class MatMenuItem extends _MatMenuItemMixinBase implements FocusableOptio /** Emits to the hover stream. */ _emitHoverEvent() { if (!this.disabled) { - this.hover.next(this); + this._hovered.next(this); } } diff --git a/src/lib/menu/menu-panel.ts b/src/lib/menu/menu-panel.ts index fa05e8c34f1a..3281d7f31508 100644 --- a/src/lib/menu/menu-panel.ts +++ b/src/lib/menu/menu-panel.ts @@ -15,7 +15,7 @@ export interface MatMenuPanel { yPosition: MenuPositionY; overlapTrigger: boolean; templateRef: TemplateRef; - close: EventEmitter; + closed: EventEmitter; parentMenu?: MatMenuPanel | undefined; direction?: Direction; focusFirstItem: () => void; diff --git a/src/lib/menu/menu-trigger.ts b/src/lib/menu/menu-trigger.ts index abc1a1a11998..ec3b18e2c661 100644 --- a/src/lib/menu/menu-trigger.ts +++ b/src/lib/menu/menu-trigger.ts @@ -107,10 +107,22 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy { @Input('matMenuTriggerFor') menu: MatMenuPanel; /** Event emitted when the associated menu is opened. */ - @Output() onMenuOpen = new EventEmitter(); + @Output() menuOpened = new EventEmitter(); + + /** + * Event emitted when the associated menu is opened. + * @deprecated Switch to `menuOpened` instead + */ + @Output() onMenuOpen = this.menuOpened; /** Event emitted when the associated menu is closed. */ - @Output() onMenuClose = new EventEmitter(); + @Output() menuClosed = new EventEmitter(); + + /** + * Event emitted when the associated menu is closed. + * @deprecated Switch to `menuClosed` instead + */ + @Output() onMenuClose = this.menuClosed; constructor(private _overlay: Overlay, private _element: ElementRef, @@ -128,19 +140,19 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy { ngAfterContentInit() { this._checkMenu(); - this.menu.close.subscribe(reason => { + this.menu.closed.subscribe(reason => { this._destroyMenu(); // If a click closed the menu, we should close the entire chain of nested menus. if (reason === 'click' && this._parentMenu) { - this._parentMenu.close.emit(reason); + this._parentMenu.closed.emit(reason); } }); if (this.triggersSubmenu()) { // Subscribe to changes in the hovered item in order to toggle the panel. this._hoverSubscription = filter - .call(this._parentMenu.hover(), active => active === this._menuItemInstance) + .call(this._parentMenu._hovered(), active => active === this._menuItemInstance) .subscribe(() => { this._openedByMouse = true; this.openMenu(); @@ -182,7 +194,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy { if (!this._menuOpen) { this._createOverlay().attach(this._portal); this._closeSubscription = this._menuClosingActions().subscribe(() => { - this.menu.close.emit(); + this.menu.closed.emit(); }); this._initMenu(); @@ -194,7 +206,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy { /** Closes the menu. */ closeMenu(): void { - this.menu.close.emit(); + this.menu.closed.emit(); } /** Focuses the menu trigger. */ @@ -273,7 +285,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy { // set state rather than toggle to support triggers sharing a menu private _setIsMenuOpen(isOpen: boolean): void { this._menuOpen = isOpen; - this._menuOpen ? this.onMenuOpen.emit() : this.onMenuClose.emit(); + this._menuOpen ? this.menuOpened.emit() : this.menuClosed.emit(); if (this.triggersSubmenu()) { this._menuItemInstance._highlighted = isOpen; @@ -387,8 +399,8 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy { /** Returns a stream that emits whenever an action that should close the menu occurs. */ private _menuClosingActions() { const backdrop = this._overlayRef!.backdropClick(); - const parentClose = this._parentMenu ? this._parentMenu.close : observableOf(); - const hover = this._parentMenu ? RxChain.from(this._parentMenu.hover()) + const parentClose = this._parentMenu ? this._parentMenu.closed : observableOf(); + const hover = this._parentMenu ? RxChain.from(this._parentMenu._hovered()) .call(filter, active => active !== this._menuItemInstance) .call(filter, () => this._menuOpen) .result() : observableOf(); diff --git a/src/lib/menu/menu.html b/src/lib/menu/menu.html index ddbad3894152..6c3450a52fcf 100644 --- a/src/lib/menu/menu.html +++ b/src/lib/menu/menu.html @@ -3,7 +3,7 @@ class="mat-menu-panel" [ngClass]="_classList" (keydown)="_handleKeydown($event)" - (click)="close.emit('click')" + (click)="closed.emit('click')" [@transformMenu]="_panelAnimationState" (@transformMenu.done)="_onAnimationDone($event)" tabindex="-1" diff --git a/src/lib/menu/menu.spec.ts b/src/lib/menu/menu.spec.ts index 7eae9d1bae64..53f4c86805f5 100644 --- a/src/lib/menu/menu.spec.ts +++ b/src/lib/menu/menu.spec.ts @@ -559,7 +559,7 @@ describe('MatMenu', () => { const emitCallback = jasmine.createSpy('emit callback'); const completeCallback = jasmine.createSpy('complete callback'); - fixture.componentInstance.menu.close.subscribe(emitCallback, null, completeCallback); + fixture.componentInstance.menu.closed.subscribe(emitCallback, null, completeCallback); fixture.destroy(); expect(emitCallback).toHaveBeenCalledWith(undefined); @@ -625,7 +625,7 @@ describe('MatMenu', () => { fixture.detectChanges(); const spy = jasmine.createSpy('hover spy'); - const subscription = instance.rootMenu.hover().subscribe(spy); + const subscription = instance.rootMenu._hovered().subscribe(spy); const menuItems = overlay.querySelectorAll('[mat-menu-item]'); dispatchMouseEvent(menuItems[0], 'mouseenter'); @@ -1112,7 +1112,7 @@ describe('MatMenu default overrides', () => { @Component({ template: ` - +