bug(autocomplete): calling input.blur from autocomplete option selected does not pass option #24182
Labels
area: material/autocomplete
P3
An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Is this a regression?
The previous version in which this bug was not present was
No response
Description
I have some code like this:
<input
#companyInput
matInput
...
[matAutocomplete]="companyAutocomplete"
(keydown.enter)="companyInput.blur()"
(blur)="setCompany($event)">
<mat-autocomplete
#companyAutocomplete="matAutocomplete"
(optionSelected)="companyInput.blur()">
...
The goal is that if the user types a value into the input and presses the Enter key, or selects a menu option from the autocomplete option (so clicks the mouse), then the blur handler for the input will get invoked, and the control associated with the input element will have the new value.
However, if you select an option from the autocomplete menu, it does not pass the option to the control, and the control's value is empty.
We can work around this by interrogating the FocusEvent:
setCompany(event: FocusEvent) {
this.adjustControlValue(event);
...
}
private adjustControlValue(event: FocusEvent) {
const target = event.relatedTarget; // mat-option chosen via mouse clikc
... // find list item associated with text content of the option
// set control value manually
}
Note that I work for Google (matthewjheaney@google.com). I can point you to the actual code if that's helpful.
I followed the work-around suggested here:
https://stackoverflow.com/questions/52936740/in-angular-onselectionchange-is-not-getting-invoked-for-angular-mat-option?rq=1
Reproduction
Steps to reproduce:
Expected Behavior
That it update the control value with the menu option selected.
Actual Behavior
The control value is empty.
Environment
sync from github done on 2021-10-18
The text was updated successfully, but these errors were encountered: