4

I am working with a dropdown that has some keys and input fields.

Currently when I select the dropdown and press tab key then it will go to the second dropdown.

The behavior I am expecting is, after selecting the drop-down and I press the tab button it should select the next item in the dropdown.

Below is an image to show the behavior I am expecting

enter image description here

This is my code

<div class="fromTo">
   <mat-form-field style="width: 30%;">
      <mat-label>From</mat-label>
      <mat-select panelClass="fromSelect" style="margin-top:10% ;">
         <input matInput [(ngModel)]='searchValue' placeholder="Search" style="height: 35px; background-color: #f3f5f8; width: 98%;"/>
         <button mat-icon-button matSuffix (click)="onKeyAgency(searchValue)" class="cursor-pointer" type="button" style="float: right;position: absolute; right: 0;">
            <mat-icon class="search_icon">search</mat-icon>
         </button>
         <button mat-button (click)="showAdd=!showAdd" style="width: 100%;text-align: left;"><mat-icon>add</mat-icon>   Add New</button>
         <input matInput [ngClass]="{ 'hide':  showAdd}" [(ngModel)]='newPlace' placeholder="add" style="height: 35px; background-color: #f3f5f8; width: 98%;  position: inherit; "/>
         <button mat-icon-button matSuffix (click)="addPlace(newPlace)" [ngClass]="{ 'hide': showAdd }" class="cursor-pointer" type="button" style="float: right; position: absolute; right: 10%;">
            <mat-icon class="search_icon">done</mat-icon>
         </button>
         <button mat-icon-button matSuffix (click)="newPlace=''" [ngClass]="{ 'hide': showAdd }" class="cursor-pointer" type="button" style="float: right; position: absolute; right: 0;">
            <mat-icon class="search_icon">close</mat-icon>
         </button>
         <mat-option>None</mat-option>
         <mat-option *ngFor="let places of selectedAgencies" [value]="places">{{places}}</mat-option>
      </mat-select>
   </mat-form-field>
</div>
3
  • have you tried by giving tab index to each options ? Commented May 1, 2020 at 5:38
  • i tried adding tab index to options but it is not working Commented May 1, 2020 at 7:16
  • after adding tab index it gets selected but dropdown got closed Commented May 2, 2020 at 6:58

1 Answer 1

2

Add a tabindex to the options like below in your template -

 <div class="fromTo">
       <mat-form-field style="width: 30%;">
             <input (keydown)="onKeydown($event)" />
             <mat-option>None</mat-option>
              <!-- Add table index to matOptions-->
             <mat-option *ngFor="let places of selectedAgencies;let i = index" [value]="places" tabindex="index">{{places}}</mat-option>
          </mat-select>
       </mat-form-field>
    </div>

Then in your component, bind keydown event in the input field to a callback in your component. This should avoid the panel getting closed -

onKeydown(event) {
  if (event.keyCode === 9) {
       event.stopPropagation();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This does not work. It contains a coding error let i = index -> tabindex="index" -> should be tabindex="{{i}}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.