Skip to content

Commit 18eccf4

Browse files
committed
fix(datepicker): cancel current range selection when pressing escape (#18630)
Adds some extra logic so that the user can abort the current range selection if they press the escape key.
1 parent ac23295 commit 18eccf4

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/material/datepicker/calendar-body.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
148148
if (columnChanges || !this._cellWidth) {
149149
this._cellWidth = `${100 / numCols}%`;
150150
}
151+
152+
if (changes['startValue'] || changes['endValue']) {
153+
this._hoveredValue = -1;
154+
}
151155
}
152156

153157
ngOnDestroy() {

src/material/datepicker/month-view.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
RIGHT_ARROW,
1818
UP_ARROW,
1919
SPACE,
20+
ESCAPE,
2021
} from '@angular/cdk/keycodes';
2122
import {
2223
AfterContentInit,
@@ -239,6 +240,15 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
239240
event.preventDefault();
240241
}
241242
return;
243+
case ESCAPE:
244+
// Abort the current range selection if the user presses escape mid-selection.
245+
if (this._matCalendarBody._hoveredValue > -1) {
246+
this.selectedChange.emit(null);
247+
this._userSelection.emit();
248+
event.preventDefault();
249+
event.stopPropagation(); // Prevents the overlay from closing.
250+
}
251+
return;
242252
default:
243253
// Don't prevent default or focus active cell on keys that we don't explicitly handle.
244254
return;

0 commit comments

Comments
 (0)