Skip to content

fix(material/datepicker): avoid rerender when min/maxDate changes to different time on the same day #24434

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 1 commit into from
Mar 31, 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
29 changes: 29 additions & 0 deletions src/material/datepicker/calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ describe('MatCalendar', () => {
expect(calendarInstance.monthView._init).toHaveBeenCalled();
});

it('should not re-render the month view when the minDate changes to the same day at a different time', () => {
fixture.detectChanges();
spyOn(calendarInstance.monthView, '_init').and.callThrough();

testComponent.minDate = new Date(2016, JAN, 1, 0, 0, 0, 1);
fixture.detectChanges();

expect(calendarInstance.monthView._init).not.toHaveBeenCalled();
});

it('should re-render the month view when the maxDate changes', () => {
fixture.detectChanges();
spyOn(calendarInstance.monthView, '_init').and.callThrough();
Expand Down Expand Up @@ -479,6 +489,25 @@ describe('MatCalendar', () => {
expect(calendarInstance.yearView._init).toHaveBeenCalled();
});

it('should not re-render the year view when the maxDate changes to the same day at a different time', () => {
fixture.detectChanges();
const periodButton = calendarElement.querySelector(
'.mat-calendar-period-button',
) as HTMLElement;
periodButton.click();
fixture.detectChanges();

(calendarElement.querySelector('.mat-calendar-body-active') as HTMLElement).click();
fixture.detectChanges();

spyOn(calendarInstance.yearView, '_init').and.callThrough();

testComponent.maxDate = new Date(2018, JAN, 1, 0, 0, 1, 0);
fixture.detectChanges();

expect(calendarInstance.yearView._init).not.toHaveBeenCalled();
});

it('should re-render the multi-year view when the minDate changes', () => {
fixture.detectChanges();
const periodButton = calendarElement.querySelector(
Expand Down
17 changes: 16 additions & 1 deletion src/material/datepicker/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
OnDestroy,
Optional,
Output,
SimpleChange,
SimpleChanges,
ViewChild,
ViewEncapsulation,
Expand Down Expand Up @@ -393,7 +394,21 @@ export class MatCalendar<D> implements AfterContentInit, AfterViewChecked, OnDes
}

ngOnChanges(changes: SimpleChanges) {
const change = changes['minDate'] || changes['maxDate'] || changes['dateFilter'];
// Ignore date changes that are at a different time on the same day. This fixes issues where
// the calendar re-renders when there is no meaningful change to [minDate] or [maxDate]
// (#24435).
const minDateChange: SimpleChange | undefined =
changes['minDate'] &&
!this._dateAdapter.sameDate(changes['minDate'].previousValue, changes['minDate'].currentValue)
? changes['minDate']
: undefined;
const maxDateChange: SimpleChange | undefined =
changes['maxDate'] &&
!this._dateAdapter.sameDate(changes['maxDate'].previousValue, changes['maxDate'].currentValue)
? changes['maxDate']
: undefined;

const change = minDateChange || maxDateChange || changes['dateFilter'];

if (change && !change.firstChange) {
const view = this._getCurrentViewComponent();
Expand Down