Skip to content

Commit

Permalink
fix(material-experimental/mdc-chips): fix changed after checked error…
Browse files Browse the repository at this point in the history
… when restoring focus to input (#24243)

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.

(cherry picked from commit c5cede8)
  • Loading branch information
crisbeto committed Jan 20, 2022
1 parent 4acbae7 commit c9ab38b
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/material-experimental/mdc-chips/chip-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -482,9 +484,4 @@ export class MatChipGrid

this._lastDestroyedChipIndex = null;
}

/** Focus input element. */
private _focusInput() {
this._chipInput.focus();
}
}

0 comments on commit c9ab38b

Please sign in to comment.