fix(menu): nested trigger staying highlighted after click (#6853)

* 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 #6838.

* fix: firefox tests crashing
This commit is contained in:
Kristiyan Kostadinov
2017-09-12 22:21:28 +02:00
committed by mmalerba
parent 881630f31d
commit 04bf3d1fd5
4 changed files with 26 additions and 3 deletions
+3 -2
View File
@@ -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;
}
+1 -1
View File
@@ -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
View File
@@ -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();
}
}
}
+15
View File
@@ -29,6 +29,7 @@ import {
dispatchMouseEvent,
dispatchEvent,
createKeyboardEvent,
createMouseEvent,
} from '@angular/cdk/testing';
@@ -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();
});
});
});