Skip to content

Commit 8fd3b2f

Browse files
authored
fix(datepicker): clearing completed range when pressing escape (#19179)
The check for when pressing escape should clear a range wasn't quite right which meant that pressing escape would clear a completed range. The intention was to only clear the range if the user has a start, but not an end.
1 parent 33a15ae commit 8fd3b2f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/material/datepicker/month-view.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,26 @@ describe('MatMonthView', () => {
333333
expect(testComponent.selected).toBeFalsy();
334334
});
335335

336+
it('should not clear the range when pressing escape while there is no preview', () => {
337+
const getRangeElements = () => monthViewNativeElement.querySelectorAll([
338+
'.mat-calendar-body-range-start',
339+
'.mat-calendar-body-in-range',
340+
'.mat-calendar-body-range-end'
341+
].join(','));
342+
343+
testComponent.selected = new DateRange(new Date(2017, JAN, 10), new Date(2017, JAN, 15));
344+
fixture.detectChanges();
345+
346+
expect(getRangeElements().length)
347+
.toBeGreaterThan(0, 'Expected range to be present on init.');
348+
349+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', ESCAPE);
350+
fixture.detectChanges();
351+
352+
expect(getRangeElements().length)
353+
.toBeGreaterThan(0, 'Expected range to be present after pressing the escape key.');
354+
});
355+
336356
it('should not fire the selected change event when clicking on an already-selected ' +
337357
'date while selecting a single date', () => {
338358
testComponent.selected = new Date(2017, JAN, 10);

src/material/datepicker/month-view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
275275
}
276276
return;
277277
case ESCAPE:
278-
// Abort the current range selection if the user presses escape mid-selection.
279-
if (this._previewEnd !== null) {
278+
// Abort the current range selection if the user presses escape mid-selection.
279+
if (this._previewEnd != null) {
280280
this._previewStart = this._previewEnd = null;
281281
this.selectedChange.emit(null);
282282
this._userSelection.emit({value: null, event});

0 commit comments

Comments
 (0)