Skip to content

fix(datepicker): changed after checked error when moving preview between months #19174

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
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
17 changes: 2 additions & 15 deletions src/material/datepicker/calendar-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
OnChanges,
SimpleChanges,
OnDestroy,
ChangeDetectorRef,
} from '@angular/core';
import {take} from 'rxjs/operators';

Expand Down Expand Up @@ -132,11 +131,7 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
/** Width of an individual cell. */
_cellWidth: string;

constructor(
private _elementRef: ElementRef<HTMLElement>,
private _changeDetectorRef: ChangeDetectorRef,
private _ngZone: NgZone) {

constructor(private _elementRef: ElementRef<HTMLElement>, private _ngZone: NgZone) {
_ngZone.runOutsideAngular(() => {
const element = _elementRef.nativeElement;
element.addEventListener('mouseenter', this._enterHandler, true);
Expand Down Expand Up @@ -321,15 +316,7 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
// we have a gap between the cells and the rows and we don't want to remove the
// range just for it to show up again when the user moves a few pixels to the side.
if (event.target && isTableCell(event.target as HTMLElement)) {
this._ngZone.run(() => {
this.previewChange.emit({value: null, event});

// Note that here we need to use `detectChanges`, rather than `markForCheck`, because
// the way `_focusActiveCell` is set up at the moment makes it fire at the wrong time
// when navigating one month back using the keyboard which will cause this handler
// to throw a "changed after checked" error when updating the preview state.
this._changeDetectorRef.detectChanges();
});
this._ngZone.run(() => this.previewChange.emit({value: null, event}));
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/material/datepicker/month-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
this._rangeStrategy.createPreview(value, this.selected as DateRange<D>, event);
this._previewStart = this._getCellCompareValue(previewRange.start);
this._previewEnd = this._getCellCompareValue(previewRange.end);

// Note that here we need to use `detectChanges`, rather than `markForCheck`, because
// the way `_focusActiveCell` is set up at the moment makes it fire at the wrong time
// when navigating one month back using the keyboard which will cause this handler
// to throw a "changed after checked" error when updating the preview state.
this._changeDetectorRef.detectChanges();
}
}

Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/datepicker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export declare class MatCalendarBody implements OnChanges, OnDestroy {
readonly selectedValueChange: EventEmitter<MatCalendarUserEvent<number>>;
startValue: number;
todayValue: number;
constructor(_elementRef: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _ngZone: NgZone);
constructor(_elementRef: ElementRef<HTMLElement>, _ngZone: NgZone);
_cellClicked(cell: MatCalendarCell, event: MouseEvent): void;
_focusActiveCell(movePreview?: boolean): void;
_isActiveCell(rowIndex: number, colIndex: number): boolean;
Expand Down