Skip to content

Commit 822f42e

Browse files
committed
add clearInput method to MatChipInputEvent
1 parent dc3f439 commit 822f42e

File tree

7 files changed

+36
-39
lines changed

7 files changed

+36
-39
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.clearInput();
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.clearInput();
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.clearInput();
7572
}
7673

7774
remove(person: Person): void {

src/material/chips/chip-input.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export interface MatChipInputEvent {
3030

3131
/** The value of the input. */
3232
value: string;
33+
34+
/** Call to clear the value of the input */
35+
clearInput(): void;
3336
}
3437

3538
// Increasing integer for generating unique ids.
@@ -185,12 +188,18 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy, A
185188
if (!this._inputElement.value && !!event) {
186189
this._chipList._keydown(event);
187190
}
188-
if (!event || this._isSeparatorKey(event)) {
189-
this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value });
190191

191-
if (event) {
192-
event.preventDefault();
193-
}
192+
if (!event || this._isSeparatorKey(event)) {
193+
this.chipEnd.emit({
194+
input: this._inputElement,
195+
value: this._inputElement.value,
196+
clearInput: () => {
197+
this._inputElement.value = '';
198+
this._focusLastChipOnBackspace = true;
199+
}
200+
});
201+
202+
event?.preventDefault();
194203
}
195204
}
196205

src/material/chips/chip-list.spec.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,21 +1516,18 @@ class InputChipList {
15161516
isRequired: boolean;
15171517

15181518
add(event: MatChipInputEvent): void {
1519-
let input = event.input;
1520-
let value = event.value;
1519+
const value = (event.value || '').trim();
15211520

15221521
// Add our foods
1523-
if ((value || '').trim()) {
1522+
if (value) {
15241523
this.foods.push({
1525-
value: `${value.trim().toLowerCase()}-${this.foods.length}`,
1526-
viewValue: value.trim()
1524+
value: `${value.toLowerCase()}-${this.foods.length}`,
1525+
viewValue: value
15271526
});
15281527
}
15291528

1530-
// Reset the input value
1531-
if (input) {
1532-
input.value = '';
1533-
}
1529+
// Clear the input value
1530+
event.clearInput();
15341531
}
15351532

15361533
remove(food: any): void {

src/material/chips/chip-list.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ export class MatChipListChange {
6666
public value: any) { }
6767
}
6868

69-
7069
/**
7170
* A material design chips component (named ChipList for its similarity to the List component).
7271
*/

tools/public_api_guard/material/chips.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export declare class MatChipInput implements MatChipTextControl, OnChanges, OnDe
106106
export interface MatChipInputEvent {
107107
input: HTMLInputElement;
108108
value: string;
109+
clearInput(): void;
109110
}
110111

111112
export declare class MatChipList extends _MatChipListMixinBase implements MatFormFieldControl<any>, ControlValueAccessor, AfterContentInit, DoCheck, OnInit, OnDestroy, CanUpdateErrorState {

0 commit comments

Comments
 (0)