angular / angular/components

bug([MatChipInput]): matChipInputAddOnBlur: true + selecting option by click inserts 2 chips

Ouverte
#28,173 1 commentaire 6 réactions 0 personnes assignées Voir sur GitHub
area: material/autocomplete area: material/chips P3
Langage dominant
TypeScript
Étoiles
25k
Forks
6.8k
Merge moyen
1 j 8 h
PR mergées (30 j)
91

Description

### Description

This is an intentional duplicate of https://github.com/angular/components/issues/19279, because it's been closed with a workaround that no longer works with the new Angular Material components (v15+).

The workaround was to set `matChipInputAddOnBlur` to `false` and handle the `(blur)` event on our own while ignoring the one that is related (`relatedTarget`) to `mat-option` element, however, now, clicking the `mat-option` will fire the `FocusEvent` with `relatedTarget: null`.

My updated (and working) workaround looks like this:
```
addOnBlur(event: FocusEvent) {
const target: HTMLElement = event.relatedTarget as HTMLElement;

if (!target) {
return;
}

this.addCustomChip(this.itemInput.nativeElement.value); // or manually create MatChipInputEvent etc...
}
```

However, the original issue is still there and it shouldn't require any workaround in the first place.

### Reproduction

https://stackblitz.com/edit/angular-rd38q1-b74xmz?file=src%2Fapp%2Fchips-autocomplete-example.ts,package.json,src%2Fapp%2Fchips-autocomplete-example.html,src%2Fmain.ts
![81319947-011e3200-90c3-11ea-8cd3-4556ec9ff622](https://github.com/angular/components/assets/32791888/69bf1bf5-5da3-49ae-b633-1c7a67791e88)

### Expected Behavior

Only the selected option should become a chip

### Actual Behavior

The input text also becomes a chip

### Environment

- Angular: v15+
- CDK/Material: v15+
- Browser(s): Chrome
- Operating System (e.g. Windows, macOS, Ubuntu): Windows

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par la reproduction StackBlitz liée, en particulier src/app/chips-autocomplete-example.ts, src/app/chips-autocomplete-example.html et package.json, puis reproduisez le flux de sélection par clic dans Chrome. Le travail est terminé lorsque la sélection d’une mat-option ajoute uniquement cette option comme chip et n’ajoute pas également le texte saisi.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
angular, typescript
Domaine
frontend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
55/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.