Skip to content

feat(material/chips): prevent chips from being deleted when user holds backspace #19700

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

Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ export class ChipsAutocompleteExample {
}

add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
const value = (event.value || '').trim();

// Add our fruit
if ((value || '').trim()) {
this.fruits.push(value.trim());
if (value) {
this.fruits.push(value);
}

// Reset the input value
if (input) {
input.value = '';
}
// Clear the input value
event.chipInput!.clear();

this.fruitCtrl.setValue(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ export class ChipsInputExample {
];

add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
const value = (event.value || '').trim();

// Add our fruit
if ((value || '').trim()) {
this.fruits.push({name: value.trim()});
if (value) {
this.fruits.push({name: value});
}

// Reset the input value
if (input) {
input.value = '';
}
// Clear the input value
event.chipInput!.clear();
}

remove(fruit: Fruit): void {
Expand Down
13 changes: 5 additions & 8 deletions src/dev-app/chips/chips-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {Component} from '@angular/core';
import {MatChipInputEvent} from '@angular/material/chips';
import {ThemePalette} from '@angular/material/core';


export interface Person {
name: string;
}
Expand Down Expand Up @@ -61,17 +60,15 @@ export class ChipsDemo {
}

add(event: MatChipInputEvent): void {
const {input, value} = event;
const value = (event.value || '').trim();

// Add our person
if ((value || '').trim()) {
this.people.push({ name: value.trim() });
if (value) {
this.people.push({ name: value });
}

// Reset the input value
if (input) {
input.value = '';
}
// Clear the input value
event.chipInput!.clear();
}

remove(person: Person): void {
Expand Down
12 changes: 5 additions & 7 deletions src/dev-app/mdc-chips/mdc-chips-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ export class MdcChipsDemo {
}

add(event: MatChipInputEvent): void {
const {input, value} = event;
const value = (event.value || '').trim();

// Add our person
if ((value || '').trim()) {
this.people.push({ name: value.trim() });
if (value) {
this.people.push({ name: value });
}

// Reset the input value
if (input) {
input.value = '';
}
// Clear the input value
event.chipInput!.clear();
}

remove(person: Person): void {
Expand Down
Loading