Skip to content

feat(overlay): expose keydown events on the opened overlay #11867

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

Merged
merged 1 commit into from
Jul 10, 2018
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
18 changes: 16 additions & 2 deletions src/cdk/overlay/overlay-directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {By} from '@angular/platform-browser';
import {ComponentFixture, TestBed, async, inject} from '@angular/core/testing';
import {Directionality} from '@angular/cdk/bidi';
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
import {ESCAPE} from '@angular/cdk/keycodes';
import {ESCAPE, A} from '@angular/cdk/keycodes';
import {CdkConnectedOverlay, OverlayModule, CdkOverlayOrigin} from './index';
import {OverlayContainer} from './overlay-container';
import {
Expand Down Expand Up @@ -447,6 +447,18 @@ describe('Overlay directives', () => {
expect(fixture.componentInstance.detachHandler).toHaveBeenCalled();
});

it('should emit the keydown events from the overlay', () => {
expect(fixture.componentInstance.keydownHandler).not.toHaveBeenCalled();

fixture.componentInstance.isOpen = true;
fixture.detectChanges();

const event = dispatchKeyboardEvent(document.body, 'keydown', A);
fixture.detectChanges();

expect(fixture.componentInstance.keydownHandler).toHaveBeenCalledWith(event);
});

});

});
Expand Down Expand Up @@ -474,6 +486,7 @@ describe('Overlay directives', () => {
(positionChange)="positionChangeHandler($event)"
(attach)="attachHandler()"
(detach)="detachHandler()"
(overlayKeydown)="keydownHandler($event)"
[cdkConnectedOverlayMinWidth]="minWidth"
[cdkConnectedOverlayMinHeight]="minHeight"
[cdkConnectedOverlayPositions]="positionOverrides">
Expand All @@ -499,7 +512,8 @@ class ConnectedOverlayDirectiveTest {
growAfterOpen: boolean;
push: boolean;
backdropClickHandler = jasmine.createSpy('backdropClick handler');
positionChangeHandler = jasmine.createSpy('positionChangeHandler');
positionChangeHandler = jasmine.createSpy('positionChange handler');
keydownHandler = jasmine.createSpy('keydown handler');
positionOverrides: ConnectionPositionPair[];
attachHandler = jasmine.createSpy('attachHandler').and.callFake(() => {
this.attachResult =
Expand Down
5 changes: 5 additions & 0 deletions src/cdk/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
/** Event emitted when the overlay has been detached. */
@Output() detach = new EventEmitter<void>();

/** Emits when there are keyboard events that are targeted at the overlay. */
@Output() overlayKeydown = new EventEmitter<KeyboardEvent>();

// TODO(jelbourn): inputs for size, scroll behavior, animation, etc.

constructor(
Expand Down Expand Up @@ -335,6 +338,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
this._createOverlay();

this._overlayRef!.keydownEvents().subscribe((event: KeyboardEvent) => {
this.overlayKeydown.next(event);

if (event.keyCode === ESCAPE) {
this._detachOverlay();
}
Expand Down