Skip to content

Commit 54997c4

Browse files
crisbetowagnermaciel
authored andcommitted
fix(material/datepicker): reset preview range after selecting a date (#21591)
Currently the preview range stays there until the user moves their pointer into another date cell which looks glitchy. These changes add a couple of extra calls so the range is removed immediately. (cherry picked from commit 192efb8)
1 parent 698d6d0 commit 54997c4

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,34 @@ describe('MatMonthView', () => {
383383
expect(testComponent.selected).toBeTruthy();
384384
});
385385

386+
it('should clear the preview range when the user is done selecting', () => {
387+
const cellEls = monthViewNativeElement.querySelectorAll<HTMLElement>(
388+
'.mat-calendar-body-cell');
389+
testComponent.selected = new DateRange(new Date(2017, JAN, 10), null);
390+
fixture.detectChanges();
391+
dispatchMouseEvent(cellEls[15], 'mouseenter');
392+
fixture.detectChanges();
393+
394+
// Note that here we only care that _some_ kind of range is rendered. There are
395+
// plenty of tests in the calendar body which assert that everything is correct.
396+
expect(monthViewNativeElement.querySelectorAll(
397+
'.mat-calendar-body-preview-start').length).toBeGreaterThan(0);
398+
expect(monthViewNativeElement.querySelectorAll(
399+
'.mat-calendar-body-in-preview').length).toBeGreaterThan(0);
400+
expect(monthViewNativeElement.querySelectorAll(
401+
'.mat-calendar-body-preview-end').length).toBeGreaterThan(0);
402+
403+
cellEls[15].click();
404+
fixture.detectChanges();
405+
406+
expect(monthViewNativeElement.querySelectorAll(
407+
'.mat-calendar-body-preview-start').length).toBe(0);
408+
expect(monthViewNativeElement.querySelectorAll(
409+
'.mat-calendar-body-in-preview').length).toBe(0);
410+
expect(monthViewNativeElement.querySelectorAll(
411+
'.mat-calendar-body-preview-end').length).toBe(0);
412+
});
413+
386414
it('should not clear the range when pressing escape while there is no preview', () => {
387415
const getRangeElements = () => monthViewNativeElement.querySelectorAll([
388416
'.mat-calendar-body-range-start',

src/material/datepicker/month-view.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
238238
}
239239

240240
this._userSelection.emit({value: selectedDate, event: event.event});
241+
this._previewStart = this._previewEnd = null;
242+
this._changeDetectorRef.markForCheck();
241243
}
242244

243245
/** Handles keydown events on the calendar body when calendar is in month view. */

0 commit comments

Comments
 (0)