Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(autocomplete): calling input.blur from autocomplete option selected does not pass option #24182

Open
1 task
matthewjheaney opened this issue Jan 11, 2022 · 1 comment
Labels
area: material/autocomplete P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@matthewjheaney
Copy link

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

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:

  1. attach a blur handler to input component
  2. call the blur function of the API from the mat-input from the autocomplete

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

@matthewjheaney matthewjheaney added the needs triage This issue needs to be triaged by the team label Jan 11, 2022
@matthewjheaney matthewjheaney changed the title bug(COMPONENT): calling input.blur from autocomplete option selected does not pass option bug(autocomplete): calling input.blur from autocomplete option selected does not pass option Jan 11, 2022
@wagnermaciel wagnermaciel added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent area: material/autocomplete and removed needs triage This issue needs to be triaged by the team labels Mar 16, 2022
@Emprint
Copy link

Emprint commented Oct 24, 2023

This seems to be a new issue since the refactoring of Material components to MDC.
In the previous version of my application running Angular 13, the FocusEvent triggered on <input matInput (blur)="autoCompleteBlur($event)"> contained a relatedTarget property with the matOption that was clicked.
I have now upgraded to Angular 16 and event.relatedTarget is null.

For now, I have applied a workaround to ignore blur event when relatedTarget is null, it does the job for my implementation.

autoCompleteBlur(event: FocusEvent) {
  if (event.relatedTarget === null) {
    return;
  }
  ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/autocomplete P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests

3 participants