Skip to content

fix(material/datepicker): fix date picker shortcuts #25951

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 2 commits into from
Nov 29, 2022
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
2 changes: 1 addition & 1 deletion src/cdk/keycodes/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

type ModifierKey = 'altKey' | 'shiftKey' | 'ctrlKey' | 'metaKey';
export type ModifierKey = 'altKey' | 'shiftKey' | 'ctrlKey' | 'metaKey';

/**
* Checks whether a modifier key is pressed.
Expand Down
9 changes: 8 additions & 1 deletion src/material/datepicker/datepicker-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ESCAPE,
hasModifierKey,
LEFT_ARROW,
ModifierKey,
PAGE_DOWN,
PAGE_UP,
RIGHT_ARROW,
Expand Down Expand Up @@ -815,6 +816,7 @@ export abstract class MatDatepickerBase<

/** Gets an observable that will emit when the overlay is supposed to be closed. */
private _getCloseStream(overlayRef: OverlayRef) {
const ctrlShiftMetaModifiers: ModifierKey[] = ['ctrlKey', 'shiftKey', 'metaKey'];
return merge(
overlayRef.backdropClick(),
overlayRef.detachments(),
Expand All @@ -823,7 +825,12 @@ export abstract class MatDatepickerBase<
// Closing on alt + up is only valid when there's an input associated with the datepicker.
return (
(event.keyCode === ESCAPE && !hasModifierKey(event)) ||
(this.datepickerInput && hasModifierKey(event, 'altKey') && event.keyCode === UP_ARROW)
(this.datepickerInput &&
hasModifierKey(event, 'altKey') &&
event.keyCode === UP_ARROW &&
ctrlShiftMetaModifiers.every(
(modifier: ModifierKey) => !hasModifierKey(event, modifier),
))
);
}),
),
Expand Down
8 changes: 6 additions & 2 deletions src/material/datepicker/datepicker-input-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {DOWN_ARROW} from '@angular/cdk/keycodes';
import {DOWN_ARROW, hasModifierKey, ModifierKey} from '@angular/cdk/keycodes';
import {
Directive,
ElementRef,
Expand Down Expand Up @@ -291,7 +291,11 @@ export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection
}

_onKeydown(event: KeyboardEvent) {
const isAltDownArrow = event.altKey && event.keyCode === DOWN_ARROW;
const ctrlShiftMetaModifiers: ModifierKey[] = ['ctrlKey', 'shiftKey', 'metaKey'];
const isAltDownArrow =
hasModifierKey(event, 'altKey') &&
event.keyCode === DOWN_ARROW &&
ctrlShiftMetaModifiers.every((modifier: ModifierKey) => !hasModifierKey(event, modifier));

if (isAltDownArrow && !this._elementRef.nativeElement.readOnly) {
this._openPopup();
Expand Down
39 changes: 39 additions & 0 deletions src/material/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,27 @@ describe('MatDatepicker', () => {
expect(testComponent.datepicker.opened).toBe(false);
}));

it('should not close the datepicker when using CTRL + SHIFT + ALT + UP_ARROW', fakeAsync(() => {
testComponent.datepicker.open();
fixture.detectChanges();
tick();
flush();

expect(testComponent.datepicker.opened).toBe(true);

const event = createKeyboardEvent('keydown', UP_ARROW, undefined, {
alt: true,
shift: true,
control: true,
});

dispatchEvent(document.body, event);
fixture.detectChanges();
flush();

expect(testComponent.datepicker.opened).toBe(true);
}));

it('should open the datepicker using ALT + DOWN_ARROW', fakeAsync(() => {
expect(testComponent.datepicker.opened).toBe(false);

Expand Down Expand Up @@ -581,6 +602,24 @@ describe('MatDatepicker', () => {
expect(event.defaultPrevented).toBe(false);
}));

it('should not open the datepicker using SHIFT + CTRL + ALT + DOWN_ARROW', fakeAsync(() => {
expect(testComponent.datepicker.opened).toBe(false);

const event = createKeyboardEvent('keydown', DOWN_ARROW, undefined, {
alt: true,
shift: true,
control: true,
});

dispatchEvent(fixture.nativeElement.querySelector('input'), event);
fixture.detectChanges();
tick();
flush();

expect(testComponent.datepicker.opened).toBe(false);
expect(event.defaultPrevented).toBe(false);
}));

it('should show the invisible close button on focus', fakeAsync(() => {
testComponent.opened = true;
fixture.detectChanges();
Expand Down
3 changes: 3 additions & 0 deletions tools/public_api_guard/cdk/keycodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ export const MAC_WK_CMD_RIGHT = 93;
// @public (undocumented)
export const META = 91;

// @public
export type ModifierKey = 'altKey' | 'shiftKey' | 'ctrlKey' | 'metaKey';

// @public (undocumented)
export const MUTE = 173;

Expand Down