Skip to content

Commit

Permalink
fix(menu): nested trigger staying highlighted after click (angular#6853)
Browse files Browse the repository at this point in the history
* fix(menu): nested trigger staying highlighted after click

Prevents the sub-menu trigger from staying highlighted if the user clicks on it and moves away to another menu item.

Fixes angular#6838.

* fix: firefox tests crashing
  • Loading branch information
crisbeto authored and mmalerba committed Sep 12, 2017
1 parent 881630f commit 04bf3d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/cdk/testing/dispatch-events.ts
Expand Up @@ -25,6 +25,7 @@ export function dispatchKeyboardEvent(node: Node, type: string, keyCode: number)
}

/** Shorthand to dispatch a mouse event on the specified coordinates. */
export function dispatchMouseEvent(node: Node, type: string, x = 0, y = 0): MouseEvent {
return dispatchEvent(node, createMouseEvent(type, x, y)) as MouseEvent;
export function dispatchMouseEvent(node: Node, type: string, x = 0, y = 0,
event = createMouseEvent(type, x, y)): MouseEvent {
return dispatchEvent(node, event) as MouseEvent;
}
2 changes: 1 addition & 1 deletion src/cdk/testing/event-objects.ts
Expand Up @@ -57,7 +57,7 @@ export function createKeyboardEvent(type: string, keyCode: number, target?: Elem

/** Creates a fake event object with any desired event type. */
export function createFakeEvent(type: string) {
let event = document.createEvent('Event');
const event = document.createEvent('Event');
event.initEvent(type, true, true);
return event;
}
7 changes: 7 additions & 0 deletions src/lib/menu/menu-trigger.ts
Expand Up @@ -413,6 +413,13 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
_handleMousedown(event: MouseEvent): void {
if (!isFakeMousedownFromScreenReader(event)) {
this._openedByMouse = true;

// Since clicking on the trigger won't close the menu if it opens a sub-menu,
// we should prevent focus from moving onto it via click to avoid the
// highlight from lingering on the menu item.
if (this.triggersSubmenu) {
event.preventDefault();
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/lib/menu/menu.spec.ts
Expand Up @@ -29,6 +29,7 @@ import {
dispatchMouseEvent,
dispatchEvent,
createKeyboardEvent,
createMouseEvent,
} from '@angular/cdk/testing';


Expand Down Expand Up @@ -1014,6 +1015,20 @@ describe('MdMenu', () => {
expect(overlay.querySelectorAll('.mat-menu-panel').length).toBe(2, 'Expected two open menus');
}));

it('should prevent the default mousedown action if the menu item opens a sub-menu', () => {
compileTestComponent();
instance.rootTrigger.openMenu();
fixture.detectChanges();

const event = createMouseEvent('mousedown');

Object.defineProperty(event, 'buttons', {get: () => 1});
event.preventDefault = jasmine.createSpy('preventDefault spy');

dispatchMouseEvent(overlay.querySelector('[md-menu-item]')!, 'mousedown', 0, 0, event);
expect(event.preventDefault).toHaveBeenCalled();
});

});

});
Expand Down

0 comments on commit 04bf3d1

Please sign in to comment.