Skip to content

docs(chips): fix autocomplete-chips example with addonblur enabled #12971

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

Merged
merged 2 commits into from
Sep 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {COMMA, ENTER} from '@angular/cdk/keycodes';
import {Component, ElementRef, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MatAutocompleteSelectedEvent, MatChipInputEvent} from '@angular/material';
import {MatAutocompleteSelectedEvent, MatChipInputEvent, MatAutocomplete} from '@angular/material';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';

Expand All @@ -17,14 +17,15 @@ export class ChipsAutocompleteExample {
visible = true;
selectable = true;
removable = true;
addOnBlur = false;
addOnBlur = true;
separatorKeysCodes: number[] = [ENTER, COMMA];
fruitCtrl = new FormControl();
filteredFruits: Observable<string[]>;
fruits: string[] = ['Lemon'];
allFruits: string[] = ['Apple', 'Lemon', 'Lime', 'Orange', 'Strawberry'];

@ViewChild('fruitInput') fruitInput: ElementRef<HTMLInputElement>;
@ViewChild('auto') matAutocomplete: MatAutocomplete;

constructor() {
this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
Expand All @@ -33,20 +34,24 @@ export class ChipsAutocompleteExample {
}

add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
// Add fruit only when MatAutocomplete is not open
// To make sure this does not conflict with OptionSelected Event
if (!this.matAutocomplete.isOpen) {
const input = event.input;
const value = event.value;

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

// Reset the input value
if (input) {
input.value = '';
}
// Reset the input value
if (input) {
input.value = '';
}

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

remove(fruit: string): void {
Expand Down