angular / angular/components

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

未关闭
#28,173 1 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看
area: material/autocomplete area: material/chips P3
主要语言
TypeScript
星标
25k
派生
6.8k
平均合并
1 天 8 小时
30 天内合并 PR
91

描述

### 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

贡献指南

打开贡献指南

调研方向

从链接的 StackBlitz 复现开始,尤其检查 src/app/chips-autocomplete-example.ts、src/app/chips-autocomplete-example.html 和 package.json,并在 Chrome 中复现点击选择流程。当选择 mat-option 时只将该选项添加为 chip,而不会同时添加输入文本,即表示工作完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
angular, typescript
领域
frontend
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。