Bug
Pasting the text: apple, banana, orange, into a chip input should result in three different chips.
Produces a single chip with all three items.
apple, banana, orange,EnterAllow inputting data via copy/paste.
Angular Material 6.2.1
add Html :
<mat-form-field class="demo-chip-list">
<mat-chip-list #chipList>
<mat-chip *ngFor="let fruit of fruits" [selectable]="selectable"
[removable]="removable" (removed)="remove(fruit)">
{{fruit.name}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input placeholder="New fruit..."
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)"
(paste)="paste($event)"/>
</mat-chip-list>
</mat-form-field>
then Add Component :
import { Component } from '@angular/core';
import { MatChipInputEvent } from '@angular/material';
import { ENTER, COMMA, SEMICOLON } from '@angular/cdk/keycodes';
/**
// Enter, comma
separatorKeysCodes = [ENTER, COMMA, SEMICOLON];
fruits = [
{ name: 'Lemon' },
{ name: 'Lime' },
{ name: 'Apple' },
];
paste(event: ClipboardEvent): void {
event.preventDefault();
event.clipboardData
.getData('Text')
.split(/;|,|\n/)
.forEach(value => {
if(value.trim()){
this.fruits.push({ name: value.trim() });
}
})
}
add(event: MatChipInputEvent): void {
let input = event.input;
let value = event.value;
// Add our fruit
if ((value || '').trim()) {
this.fruits.push({ name: value.trim() });
}
// Reset the input value
if (input) {
input.value = '';
}
}
remove(fruit: any): void {
let index = this.fruits.indexOf(fruit);
if (index >= 0) {
this.fruits.splice(index, 1);
}
}
}
/** Copyright 2018 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */
Good Time @dpurp ,
Need this! <3 +1
Thank you :)
Most helpful comment
add Html :
<mat-form-field class="demo-chip-list"> <mat-chip-list #chipList> <mat-chip *ngFor="let fruit of fruits" [selectable]="selectable" [removable]="removable" (removed)="remove(fruit)"> {{fruit.name}} <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon> </mat-chip> <input placeholder="New fruit..." [matChipInputFor]="chipList" [matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="addOnBlur" (matChipInputTokenEnd)="add($event)" (paste)="paste($event)"/> </mat-chip-list> </mat-form-field>then Add Component :
import { Component } from '@angular/core';
import { MatChipInputEvent } from '@angular/material';
import { ENTER, COMMA, SEMICOLON } from '@angular/cdk/keycodes';
/**
*/
@Component({
selector: 'chips-input-example',
templateUrl: 'chips-input-example.html',
styleUrls: ['chips-input-example.css']
})
export class ChipsInputExample {
visible: boolean = true;
selectable: boolean = true;
removable: boolean = true;
addOnBlur: boolean = true;
// Enter, comma
separatorKeysCodes = [ENTER, COMMA, SEMICOLON];
fruits = [
{ name: 'Lemon' },
{ name: 'Lime' },
{ name: 'Apple' },
];
paste(event: ClipboardEvent): void {
event.preventDefault();
event.clipboardData
.getData('Text')
.split(/;|,|\n/)
.forEach(value => {
if(value.trim()){
this.fruits.push({ name: value.trim() });
}
})
}
add(event: MatChipInputEvent): void {
let input = event.input;
let value = event.value;
}
remove(fruit: any): void {
let index = this.fruits.indexOf(fruit);
}
}
/** Copyright 2018 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */
Good Time @dpurp ,