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(MatAutocomplete): MatAutocomplete + FormControl does not clear input correctly after option selection #25809

Open
1 task
edsanchez-google opened this issue Oct 13, 2022 · 4 comments
Labels
area: material/input P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@edsanchez-google
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

See example: https://material.angular.io/components/chips/examples#chips-autocomplete

In this example, after selecting an autocomplete option, we want to create a new chip and clear the typed text in the input element. The input is bound to a FormControl instance, so it should be possible to clear the input by simply calling this.fruitCtrl.setValue(null) or this.fruitCtrl.setValue(''). Instead, the previously typed text remains visible, but if you subscribe to the FormControl.valueChanges property you will see the new empty value emitted correctly.

The example works around this issue by accessing the native input element and updating the value directly.

Reproduction

Steps to reproduce:

  1. See example https://material.angular.io/components/chips/examples#chips-autocomplete
  2. Remove the nativeElement reference
  3. Input text remains visible despite the FormControl instance correctly emitting the new value, creating an inconsistent state

Expected Behavior

It should be enough to just update the FormControl to ensure the input element has the correct value set.

Actual Behavior

The input element is left in an inconsistent state unless the user manually updates the native element's value directly.

Environment

  • Angular: n/a (internal stable version http://go/ng-cli)
  • CDK/Material: n/a
  • Browser(s): Chrome
  • Operating System (e.g. Windows, macOS, Ubuntu): macOS
@edsanchez-google edsanchez-google added the needs triage This issue needs to be triaged by the team label Oct 13, 2022
@andrewseguin andrewseguin added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent area: material/input and removed needs triage This issue needs to be triaged by the team labels May 30, 2023
@mario-d-s
Copy link

mario-d-s commented Jul 11, 2023

I'd like to add some weight on this because I have a very similar problem. I also want to use the FormControl.setValue function to set the text of an autocomplete element - an actual value instead of clearing it.

This unfortunately does not work and I have to jump through hoops to get at the native input element, which is of course counter to the spirit of a framework like Angular.

Previous bug reports have always gone stale, unfortunately: #22985, #22914, #18286 (explained poorly but probably the same). This is very much an issue...

@MikaStark
Copy link

MikaStark commented Nov 28, 2023

Same for me.

To workaround this issue, I use MatChipInput#clear method instead of direct use of nativeElement. IMO it's less "tricky".

// ...

@ViewChild('fruitChipInput') fruitChipInput: MatChipInput;

// ...

selected(event: MatAutocompleteSelectedEvent): void {
  this.fruits.push(event.option.viewValue);
  this.fruitChipInput.clear();
  this.fruitCtrl.setValue(null);
}

@anasbud
Copy link

anasbud commented Jul 3, 2024

In Angular 17, still got the issue. Any update on that ?

selectUser(user: User) {
    this.selectedUser.set(user);
    this.form.controls.name.setValue("");
}

The input is not cleared.

@bdirito
Copy link

bdirito commented Nov 4, 2024

This has been fixed in angular 17 (at least by 17.3.6) by something along the lines of what @MikaStark suggested. 'Fixed' meaning that the example explicitly sets the native element; presumably the DOM should be reflecting the FormControl.

https://v17.material.angular.io/components/chips/examples#chips-autocomplete

However the example was re-written for angular 18 (18.2.11) to use signals and re-broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/input 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

6 participants