bug(autocomplete): Setting input value from code does not update autocomplete selected item
- Dominant language
- TypeScript
- Stars
- 25k
- Forks
- 6.8k
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 91
Description
### Is this a regression?
- [ ] Yes, this behavior used to work in the previous version
### The previous version in which this bug was not present was
_No response_
### Description
Autocomplete works beautifully when working from the user inputs.
When we set the value from the code-behind (either via FormControl or on the element directly) the selected option does not update.
In the attached example we need to clear the input from code, but the previously selected option still has the "selected" checkmark and foreground color.
### Reproduction
StackBlitz link: [components-issue-hmgdm3](https://stackblitz.com/edit/components-issue-hmgdm3?file=src%2Fapp%2Fexample-component.ts)
Steps to reproduce:
1. Select an option from the autocomplete
2. Clear the selection by using the button
3. Focus the input and you can see the previously selected option still having the "selected" indicators (checkmark and foreground)
### Expected Behavior
The autocomplete is supposed to remove the selected indication on the last selected option when the value of our input is cleared
### Actual Behavior
The autocomplete keeps previously selected option as "selected"
### Environment
- Angular: 16.2.1
- CDK/Material: 16.2.1
- Browser(s): Chrome, Firefox latest
- Operating System (e.g. Windows, macOS, Ubuntu): Windows 11
Contributor guide
Research direction
Start with the reproduction in src/app/example-component.ts from the linked StackBlitz and trace how clearing the FormControl or input value affects the autocomplete. Confirm the behavior in Chrome or Firefox: after clearing and refocusing, the previously selected option should no longer show its checkmark or selected foreground indication.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100