angular / angular/components

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

Đang mở
#28,173 1 bình luận 6 reaction 0 người được giao Xem trên GitHub
area: material/autocomplete area: material/chips P3
Ngôn ngữ chính
TypeScript
Star
25k
Fork
6.8k
Merge trung bình
1 ngày 8 giờ
Pull request đã merge (30 ngày)
91

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với bản tái hiện StackBlitz được liên kết, đặc biệt là src/app/chips-autocomplete-example.ts, src/app/chips-autocomplete-example.html và package.json, rồi tái hiện luồng chọn bằng thao tác nhấp trong Chrome. Công việc hoàn tất khi việc chọn một mat-option chỉ thêm tùy chọn đó dưới dạng chip và không đồng thời thêm văn bản đầu vào.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
angular, typescript
Lĩnh vực
frontend
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.