From 46fc007dba18f40dec5d08395eca61b958713c0a Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 20 Jan 2022 11:56:46 +0100 Subject: [PATCH] fix(material-experimental/mdc-chips): fix changed after checked error when restoring focus to input Fixes that the MDC chip grid was causing a "changed after checked" error when the last chip is removed, if it is used together with `mat-autocomplete`. The problem is that the chip grid focuses the input when the last chip is removed which then opens the autocomplete panel, resulting in an error. --- src/material-experimental/mdc-chips/chip-grid.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/material-experimental/mdc-chips/chip-grid.ts b/src/material-experimental/mdc-chips/chip-grid.ts index 0b2ae83abd14..57add5da8653 100644 --- a/src/material-experimental/mdc-chips/chip-grid.ts +++ b/src/material-experimental/mdc-chips/chip-grid.ts @@ -357,7 +357,9 @@ export class MatChipGrid this._chips.forEach(chip => chip.primaryAction._updateTabindex(-1)); this._chips.first.focus(); } else { - this._focusInput(); + // Delay until the next tick, because this can cause a "changed after checked" + // error if the input does something on focus (e.g. opens an autocomplete). + Promise.resolve().then(() => this._chipInput.focus()); } this.stateChanges.next(); @@ -482,9 +484,4 @@ export class MatChipGrid this._lastDestroyedChipIndex = null; } - - /** Focus input element. */ - private _focusInput() { - this._chipInput.focus(); - } }