Skip to content

Commit fdfa278

Browse files
committed
fix(datepicker): close datepicker popup on alt + up arrow
Similarly to other controls that are based around an input and a popup, the datepicker should open on alt + down arrow and close on alt + up arrow. The opening logic was already being handled and this PR adds the logic for closing.
1 parent 7857b92 commit fdfa278

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/lib/datepicker/datepicker.spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import {ENTER, ESCAPE, RIGHT_ARROW} from '@angular/cdk/keycodes';
1+
import {ENTER, ESCAPE, RIGHT_ARROW, UP_ARROW} from '@angular/cdk/keycodes';
22
import {Overlay, OverlayContainer, ScrollDispatcher} from '@angular/cdk/overlay';
3-
import {dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent} from '@angular/cdk/testing';
3+
import {
4+
dispatchFakeEvent,
5+
dispatchKeyboardEvent,
6+
dispatchMouseEvent,
7+
createKeyboardEvent,
8+
dispatchEvent,
9+
} from '@angular/cdk/testing';
410
import {Component, ViewChild} from '@angular/core';
511
import {ComponentFixture, fakeAsync, flush, inject, TestBed} from '@angular/core/testing';
612
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
@@ -386,6 +392,24 @@ describe('MatDatepicker', () => {
386392
expect(testComponent.datepicker.opened).toBe(false);
387393
}))
388394
);
395+
396+
it('should close the datpeicker using ALT + UP_ARROW', fakeAsync(() => {
397+
testComponent.datepicker.open();
398+
fixture.detectChanges();
399+
flush();
400+
401+
expect(testComponent.datepicker.opened).toBe(true);
402+
403+
const event = createKeyboardEvent('keydown', UP_ARROW);
404+
Object.defineProperty(event, 'altKey', {get: () => true});
405+
406+
dispatchEvent(document.body, event);
407+
fixture.detectChanges();
408+
flush();
409+
410+
expect(testComponent.datepicker.opened).toBe(false);
411+
}));
412+
389413
});
390414

391415
describe('datepicker with too many inputs', () => {

src/lib/datepicker/datepicker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Directionality} from '@angular/cdk/bidi';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
11-
import {ESCAPE} from '@angular/cdk/keycodes';
11+
import {ESCAPE, UP_ARROW} from '@angular/cdk/keycodes';
1212
import {
1313
FlexibleConnectedPositionStrategy,
1414
Overlay,
@@ -472,7 +472,11 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
472472
merge(
473473
this._popupRef.backdropClick(),
474474
this._popupRef.detachments(),
475-
this._popupRef.keydownEvents().pipe(filter(event => event.keyCode === ESCAPE))
475+
this._popupRef.keydownEvents().pipe(filter(event => {
476+
// Closing on alt + up is only valid when there's an input associated with the datepicker.
477+
return event.keyCode === ESCAPE ||
478+
(this._datepickerInput && event.altKey && event.keyCode === UP_ARROW);
479+
}))
476480
).subscribe(() => this.close());
477481
}
478482

0 commit comments

Comments
 (0)