bug(autocomplete): optionSelected and onChangeCallback is called before the animation completes
- Dominant language
- TypeScript
- Stars
- 25k
- Forks
- 6.8k
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 91
Description
### Is this a regression?
- [X] Yes, this behavior used to work in the previous version
### The previous version in which this bug was not present was
v16.1.8
### Description
There is a _setValueAndClose method in the MatAutocompleteTrigger class that doesn't take into account the time to complete the animation, this has even been flagged todo but still not fixed. Currently, if we in optionSelected decide to clear the array used in ngFor for mat-option, we will get the array cleared before the panel closes and there will be a corresponding jump

@crisbeto
### Reproduction
StackBlitz link: https://components-issue-v4ujjj.stackblitz.io
Steps to reproduce:
1. put the focus in the field
2. pick a value from the list
### Expected Behavior
the optionSelected event will be called after the animation is complete, the menu will not jump after the array is cleared
### Actual Behavior
optionSelected event was triggered without waiting for the animation to complete, clearing the array caused the panel to jump
### Environment
- Angular: 16.2.12
- CDK/Material: 16.2.14
- Browser(s): google chrome v126.0.6478.127
- Operating System (e.g. Windows, macOS, Ubuntu): Windows 10
Contributor guide
Research direction
Start with the MatAutocompleteTrigger class and its _setValueAndClose method, then reproduce the behavior using the linked StackBlitz by selecting an option while clearing the mat-option ngFor array in optionSelected. Compare the event timing with the autocomplete panel animation; done means optionSelected and onChangeCallback do not cause the panel to jump before the animation completes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100