mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
mmalerba
parent
881630f31d
commit
04bf3d1fd5
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user