Aeseir
Aeseir

Reputation: 8434

Remove Focus/Blur on Select programmatically (Angular6 Material)

I duplicated the Angular 6 Material autocomplete simple example: Simple Autocomplete

I am trying to figure out how remove focus once selection has been made.

I have added the following change:

In HTML

<mat-autocomplete #autoGroup="matAutocomplete" (optionSelected)="onSelectionChanged($event)">

In Components

  onSelectionChanged(event: MatAutocompleteSelectedEvent) {
    console.log(event.option.value);
  }

After it outputs the value to console i would like to remove the focus from the input field, but not sure the way to do this.

Upvotes: 10

Views: 37253

Answers (4)

Sara Bianchi
Sara Bianchi

Reputation: 191

My autocomplete field works with this typescript function

public removeFocus(field, input){
   field._elementRef.nativeElement.classList.remove('mat-focused');
   input.blur(); 
}

HTML code

<form class="example-form">
  <mat-form-field class="example-full-width test" #autocompleteField>
   <input matInput type="text" 
     placeholder="Pick one" 
     aria-label="Number"  
     [formControl]="myControl" 
     [matAutocomplete]="auto" 
     #elementInput
   >
   <mat-autocomplete 
    #auto="matAutocomplete" 
    (optionSelected)="removeFocus(autocompleteField, elementInput)">
     <mat-option *ngFor="let option of options" [value]="option">
      {{ option }}
     </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

Remove focus from both the mat-form-field and its input.

Upvotes: 5

Michahell
Michahell

Reputation: 5071

An even nicer solution to removing focus on the autocomplete parent input if it is a mat-input, is to reference it using @ViewChild as MatInput. MatInput has a public field focused which can be set to false:

@ViewChild('searchInput', { static: false }) private searchInput: MatInput;
// some time later...
this.searchInput.focused = false

Upvotes: -1

Andrea Domp&#232;
Andrea Domp&#232;

Reputation: 159

Try this Template

<input #autoCompleteInput type="text" [matAutocomplete]="auto"/>

component

@ViewChild('autoCompleteInput', { read: MatAutocompleteTrigger }) autoComplete: MatAutocompleteTrigger;

  close() {
    this.autoComplete.closePanel();
  }

Plunker

Upvotes: 3

Lia
Lia

Reputation: 11982

the mat-focused class of mat-form-field causes the focus on mat-auto-complete, by removing it from mat-form-field it will be not focused,

in component:

export class AutocompleteSimpleExample {
  myControl: FormControl = new FormControl();
  public matForm ;
  constructor(){

  }

  ngOnInit(){
    this.matForm =  document.getElementById("matForm") 
  }
  options = [
    'One',
    'Two',
    'Three'
   ];

  test(option){
    console.log(option)
    setTimeout(function(){
      this.matForm.classList.remove('mat-focused' )}, 100);
  }
}

in html:

<form class="example-form">
  <mat-form-field class="example-full-width test" #matForm id="matForm">
    <input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto" #textInput>
    <mat-autocomplete #auto="matAutocomplete" (optionSelected)="test($event.option)">
      <mat-option *ngFor="let option of options" [value]="option">
        {{ option }}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

check this stackblitz.

Building on answer as per Aeseir's findings

Link @ViewChild to your input.

export class WhateverComponent {
@ViewChild('textInput') textInput: ElementRef;
//all other code
}

in onSelectionChanged function

onSelectionChanged(event: MatAutocompleteSelectedEvent) {
    console.log(event.option.value);
    // blur will remove focus on the currently selected element
    this.matInputMain.nativeElement.blur();
    // if you using form you can wipe the input too
    this.yourForm.reset();        
  }

The above will log the selected value to console, blur the focus and reset the form (if you using form and want this function).

Upvotes: 6

Related Questions