Skip to content

Commit 6230560

Browse files
authored
feat(material/chips): prevent chips from being deleted when user holds backspace (#19700)
* feat(material/chips): prevent chips from being deleted when user holds backspace * add unit tests * replace MatChipInputEvent.clearInput with MatChipInputEvent.chipInput.clear() * fix unit test to actually test wanted behaviour * update mdc-chips/chip-input.ts * update chip-grid, update demos and add tests to spec * make MatChipInputEvent.chipInput temporarily optional (for backward compatibility) * yarn approve-api chips * Merge remote-tracking branch 'upstream/master' into giorag/chip-list-focus-last-chip-on-backspace-v2
1 parent 6a8a144 commit 6230560

File tree

11 files changed

+456
-332
lines changed

11 files changed

+456
-332
lines changed

src/components-examples/material/chips/chips-autocomplete/chips-autocomplete-example.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,15 @@ export class ChipsAutocompleteExample {
3434
}
3535

3636
add(event: MatChipInputEvent): void {
37-
const input = event.input;
38-
const value = event.value;
37+
const value = (event.value || '').trim();
3938

4039
// Add our fruit
41-
if ((value || '').trim()) {
42-
this.fruits.push(value.trim());
40+
if (value) {
41+
this.fruits.push(value);
4342
}
4443

45-
// Reset the input value
46-
if (input) {
47-
input.value = '';
48-
}
44+
// Clear the input value
45+
event.chipInput!.clear();
4946

5047
this.fruitCtrl.setValue(null);
5148
}

src/components-examples/material/chips/chips-input/chips-input-example.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@ export class ChipsInputExample {
2727
];
2828

2929
add(event: MatChipInputEvent): void {
30-
const input = event.input;
31-
const value = event.value;
30+
const value = (event.value || '').trim();
3231

3332
// Add our fruit
34-
if ((value || '').trim()) {
35-
this.fruits.push({name: value.trim()});
33+
if (value) {
34+
this.fruits.push({name: value});
3635
}
3736

38-
// Reset the input value
39-
if (input) {
40-
input.value = '';
41-
}
37+
// Clear the input value
38+
event.chipInput!.clear();
4239
}
4340

4441
remove(fruit: Fruit): void {

src/dev-app/chips/chips-demo.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {Component} from '@angular/core';
1111
import {MatChipInputEvent} from '@angular/material/chips';
1212
import {ThemePalette} from '@angular/material/core';
1313

14-
1514
export interface Person {
1615
name: string;
1716
}
@@ -61,17 +60,15 @@ export class ChipsDemo {
6160
}
6261

6362
add(event: MatChipInputEvent): void {
64-
const {input, value} = event;
63+
const value = (event.value || '').trim();
6564

6665
// Add our person
67-
if ((value || '').trim()) {
68-
this.people.push({ name: value.trim() });
66+
if (value) {
67+
this.people.push({ name: value });
6968
}
7069

71-
// Reset the input value
72-
if (input) {
73-
input.value = '';
74-
}
70+
// Clear the input value
71+
event.chipInput!.clear();
7572
}
7673

7774
remove(person: Person): void {

src/dev-app/mdc-chips/mdc-chips-demo.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,15 @@ export class MdcChipsDemo {
6161
}
6262

6363
add(event: MatChipInputEvent): void {
64-
const {input, value} = event;
64+
const value = (event.value || '').trim();
6565

6666
// Add our person
67-
if ((value || '').trim()) {
68-
this.people.push({ name: value.trim() });
67+
if (value) {
68+
this.people.push({ name: value });
6969
}
7070

71-
// Reset the input value
72-
if (input) {
73-
input.value = '';
74-
}
71+
// Clear the input value
72+
event.chipInput!.clear();
7573
}
7674

7775
remove(person: Person): void {

0 commit comments

Comments
 (0)