Skip to content

Commit 2375a3e

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 2375a3e

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/lib/datepicker/datepicker.spec.ts

Lines changed: 27 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,25 @@ 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 content = document.querySelector('.mat-datepicker-content')!;
404+
const event = createKeyboardEvent('keydown', UP_ARROW);
405+
Object.defineProperty(event, 'altKey', {get: () => true});
406+
407+
dispatchEvent(content, event);
408+
fixture.detectChanges();
409+
flush();
410+
411+
expect(testComponent.datepicker.opened).toBe(false);
412+
}));
413+
389414
});
390415

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

src/lib/datepicker/datepicker.ts

Lines changed: 9 additions & 1 deletion
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,
@@ -83,6 +83,7 @@ export const _MatDatepickerContentMixinBase = mixinColor(MatDatepickerContentBas
8383
styleUrls: ['datepicker-content.css'],
8484
host: {
8585
'class': 'mat-datepicker-content',
86+
'(keydown)': '_handleKeydown($event)',
8687
'[@transformPanel]': '"enter"',
8788
'[class.mat-datepicker-content-touch]': 'datepicker.touchUi',
8889
'[class.mat-datepicker-content-above]': '_isAbove',
@@ -138,6 +139,13 @@ export class MatDatepickerContent<D> extends _MatDatepickerContentMixinBase
138139
});
139140
}
140141

142+
_handleKeydown(event: KeyboardEvent) {
143+
// Closing on alt + up is only valid when there's an input associated with the datepicker.
144+
if (this.datepicker._datepickerInput && event.altKey && event.keyCode === UP_ARROW) {
145+
this.datepicker.close();
146+
}
147+
}
148+
141149
ngAfterContentInit() {
142150
this._focusActiveCell();
143151
}

0 commit comments

Comments
 (0)