Skip to content

fix(material/datepicker): changed after checked error during overlapping open/close sequence #25843

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
Oct 21, 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
23 changes: 20 additions & 3 deletions src/material/datepicker/datepicker-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
inject,
} from '@angular/core';
import {CanColor, DateAdapter, mixinColor, ThemePalette} from '@angular/material/core';
import {AnimationEvent} from '@angular/animations';
import {merge, Subject, Observable, Subscription} from 'rxjs';
import {filter, take} from 'rxjs/operators';
import {_getFocusedElementPierceShadowDom} from '@angular/cdk/platform';
Expand Down Expand Up @@ -119,7 +120,8 @@ const _MatDatepickerContentBase = mixinColor(
host: {
'class': 'mat-datepicker-content',
'[@transformPanel]': '_animationState',
'(@transformPanel.done)': '_animationDone.next()',
'(@transformPanel.start)': '_handleAnimationEvent($event)',
'(@transformPanel.done)': '_handleAnimationEvent($event)',
'[class.mat-datepicker-content-touch]': 'datepicker.touchUi',
},
animations: [matDatepickerAnimations.transformPanel, matDatepickerAnimations.fadeInCalendar],
Expand Down Expand Up @@ -161,6 +163,9 @@ export class MatDatepickerContent<S, D = ExtractDateTypeFromSelection<S>>
/** Emits when an animation has finished. */
readonly _animationDone = new Subject<void>();

/** Whether there is an in-progress animation. */
_isAnimating = false;

/** Text for the close button. */
_closeButtonText: string;

Expand Down Expand Up @@ -240,6 +245,14 @@ export class MatDatepickerContent<S, D = ExtractDateTypeFromSelection<S>>
this._changeDetectorRef.markForCheck();
}

_handleAnimationEvent(event: AnimationEvent) {
this._isAnimating = event.phaseName === 'start';

if (!this._isAnimating) {
this._animationDone.next();
}
}

_getSelected() {
return this._model.selection as unknown as D | DateRange<D> | null;
}
Expand Down Expand Up @@ -596,7 +609,9 @@ export abstract class MatDatepickerBase<

/** Open the calendar. */
open(): void {
if (this._opened || this.disabled) {
// Skip reopening if there's an in-progress animation to avoid overlapping
// sequences which can cause "changed after checked" errors. See #25837.
if (this._opened || this.disabled || this._componentRef?.instance._isAnimating) {
return;
}

Expand All @@ -612,7 +627,9 @@ export abstract class MatDatepickerBase<

/** Close the calendar. */
close(): void {
if (!this._opened) {
// Skip reopening if there's an in-progress animation to avoid overlapping
// sequences which can cause "changed after checked" errors. See #25837.
if (!this._opened || this._componentRef?.instance._isAnimating) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions tools/public_api_guard/material/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AbstractControl } from '@angular/forms';
import { AfterContentInit } from '@angular/core';
import { AfterViewChecked } from '@angular/core';
import { AfterViewInit } from '@angular/core';
import { AnimationEvent as AnimationEvent_2 } from '@angular/animations';
import { AnimationTriggerMetadata } from '@angular/animations';
import { BooleanInput } from '@angular/cdk/coercion';
import { CanColor } from '@angular/material/core';
Expand Down Expand Up @@ -382,8 +383,11 @@ export class MatDatepickerContent<S, D = ExtractDateTypeFromSelection<S>> extend
// (undocumented)
_getSelected(): D | DateRange<D> | null;
// (undocumented)
_handleAnimationEvent(event: AnimationEvent_2): void;
// (undocumented)
_handleUserSelection(event: MatCalendarUserEvent<D | null>): void;
_isAbove: boolean;
_isAnimating: boolean;
// (undocumented)
ngAfterViewInit(): void;
// (undocumented)
Expand Down