Search code examples
angularangular-materialmat-autocomplete

MatAutoComplete - How to stop opening of MatAutoComplete in mat-chip-list when an unknown text is entered in the input and "ENTER" key is pressed


I am using mat-chip-list along with mat-autocomplete by referring the official angular material example https://material.angular.io/components/chips/overview#chip-input and https://stackblitz.com/angular/arjnbxmepgn?file=src%2Fapp%2Fchips-autocomplete-example.html. The same template shown in the above example is here -

<mat-form-field class="example-chip-list">
  <mat-chip-list #chipList aria-label="Fruit selection">
    <mat-chip
      *ngFor="let fruit of fruits"
      [selectable]="selectable"
      [removable]="removable"
      (removed)="remove(fruit)">
      {{fruit}}
      <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
    </mat-chip>
    <input
      placeholder="New fruit..."
      #fruitInput
      [formControl]="fruitCtrl"
      [matAutocomplete]="auto"
      [matChipInputFor]="chipList"
      [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
      (matChipInputTokenEnd)="add($event)">
  </mat-chip-list>
  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
    <mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
      {{fruit}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

It works fine except the following issue -

As soon as the user focuses the input, the MatAutoComplete panel opens with suggestions. If the user enters a text which does not exist in the suggestions and presses the "ENTER" key, the entered text is visible in a Chip and it opens the MatAutoComplate panel. I want to stop the opening of the MatAutoComplete panel in such a case. I do not want to open the suggestion panel if the user enters an unknown text [i.e. text other than in suggestions]. The same case can be seen in https://stackblitz.com/angular/arjnbxmepgn?file=src%2Fapp%2Fchips-autocomplete-example.html.

If the user selects one of the options from the suggestions and presses the "ENTER" key, the MatAutoComplete panel does not open. This is what I want in the scenario when the user enters an unknown text in the input and presses the "ENTER" key.

Any help will be highly appreciated.


Solution

  • I've updated the example to this:

    import {COMMA, ENTER} from '@angular/cdk/keycodes';
    import {Component, ElementRef, ViewChild} from '@angular/core';
    import {FormControl} from '@angular/forms';
    import {MatAutocompleteSelectedEvent, MatAutocomplete, MatAutocompleteTrigger} from '@angular/material/autocomplete';
    import {MatChipInputEvent} from '@angular/material/chips';
    import {Observable} from 'rxjs';
    import {map, startWith} from 'rxjs/operators';
    
    /**
     * @title Chips Autocomplete
     */
    @Component({
      selector: 'chips-autocomplete-example',
      templateUrl: 'chips-autocomplete-example.html',
      styleUrls: ['chips-autocomplete-example.css'],
    })
    export class ChipsAutocompleteExample {
      visible = true;
      selectable = true;
      removable = 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;
      @ViewChild(MatAutocompleteTrigger) autocomplete: MatAutocompleteTrigger;
    
      constructor() {
        this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
            startWith(null),
            map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
      }
    
      add(event: MatChipInputEvent): void {
        const input = event.input;
        const value = event.value;
    
        // Add our fruit
        if ((value || '').trim()) {
          if(this.allFruits.indexOf(value) > -1){
           this.fruits.push(value.trim());
          }
        }
    
        // Reset the input value
        if (input) {
          input.value = '';
        }
    
        this.fruitCtrl.setValue(null);
        this.autocomplete.closePanel();
      }
    
      remove(fruit: string): void {
        const index = this.fruits.indexOf(fruit);
    
        if (index >= 0) {
          this.fruits.splice(index, 1);
        }
      }
    
      selected(event: MatAutocompleteSelectedEvent): void {
        this.fruits.push(event.option.viewValue);
        this.fruitInput.nativeElement.value = '';
        this.fruitCtrl.setValue(null);
      }
    
      private _filter(value: string): string[] {
        const filterValue = value.toLowerCase();
    
        return this.allFruits.filter(fruit => fruit.toLowerCase().indexOf(filterValue) === 0);
      }
    }