Starts tracking the destroyed chips in order to capture the focused one.
()
| 281 | |
| 282 | /** Starts tracking the destroyed chips in order to capture the focused one. */ |
| 283 | private _trackDestroyedFocusedChip() { |
| 284 | this.chipDestroyedChanges.pipe(takeUntil(this._destroyed)).subscribe((event: MatChipEvent) => { |
| 285 | // If the focused chip is destroyed, save its index so that we can move focus to the next |
| 286 | // chip. We only save the index here, rather than move the focus immediately, because we want |
| 287 | // to wait until the chip is removed from the chip list before focusing the next one. This |
| 288 | // allows us to keep focus on the same index if the chip gets swapped out. |
| 289 | const chipArray = this._chips.toArray(); |
| 290 | const chipIndex = chipArray.indexOf(event.chip); |
| 291 | const hasFocus = event.chip._hasFocus(); |
| 292 | const wasLastFocused = |
| 293 | event.chip._hadFocusOnRemove && |
| 294 | this._keyManager.activeItem && |
| 295 | event.chip._getActions().includes(this._keyManager.activeItem); |
| 296 | |
| 297 | // Note that depending on the timing, the chip might've already lost focus by the |
| 298 | // time we check this. We need the `wasLastFocused` as a fallback to detect such cases. |
| 299 | // In `wasLastFocused` we also need to ensure that the chip actually had focus when it was |
| 300 | // deleted so that we don't steal away the user's focus after they've moved on from the chip. |
| 301 | const shouldMoveFocus = hasFocus || wasLastFocused; |
| 302 | |
| 303 | if (this._isValidIndex(chipIndex) && shouldMoveFocus) { |
| 304 | this._lastDestroyedFocusedChipIndex = chipIndex; |
| 305 | } |
| 306 | }); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Finds the next appropriate chip to move focus to, |
no test coverage detected