Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to enable focus on input element inside mat-select component?

I need to activate the cursor on the html input element (search field) using keyboard inside mat-select.

It works perfectly fine when using mouse, but because of WCAG requirements I need to make it fully working using keyboard.

https://stackblitz.com/edit/angular-dukfxf?file=src%2Fapp%2Fapp.component.ts

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve tried almost everything from custom js code to Angular directives.

Any thougths?

enter image description here

>Solution :

You can do the following:

1. Add an identification to your input and use the event openedChange from mat-select

<mat-select (openedChange)="focus()">
  <input
    ...
    #exampleInput
   />
</mat-select>

2. Define the function focus() in your component and it would work

export class AppComponent {
  @ViewChild('exampleInput') searchElement: ElementRef;
  ...

  focus() {
    setTimeout(() => {
      this.searchElement.nativeElement.focus();
    }, 0);
  }
}

https://stackblitz.com/edit/angular-nscqdp?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading