title: Angular Material: Hide Autocomplete Panel when User hits enter Key
body: <p>I'm currently working on a table where the user is able to tab through editable elements by pressing enter. I also use Angular Material in this.</p>
  
  <p>I have a mat-form-field with several dynamically created input fields with the mat-autocomplete element. However my enter key event acts a bit different in this.</p>
  
  <p>When you press on the input field, a panel will open (dropdown) where the user can select the input or he can simply write himself and the panel will give suggestions (autocomplete).</p>
  
  <p><strong>What happens if you press the tab key?</strong></p>
  
  <p>If you press on tab while typing, the cursor will move onto the next editable element and the panel (dropdown) of the latest element will close.</p>
  
  <p><strong>What happens if you press the enter key</strong></p>
  
  <p>If you press on enter while typing, the cursor will move onto the next editable element HOWEVER the panel (dropdown) of the latest element stays open which resulst in multiple input fields having an open dropdown panel even though the user has already wrote what he needed to.</p>
  
  <p>Template:</p>
  
  <pre><code>&lt;tr *ngFor="let row of rows; let rowIdx = index"&gt;
              &lt;td *ngFor="let col of columns; let colIdx = index"&gt;
                  &lt;mat-form-field class="example-full-width"&gt;             
                  &lt;input  #inputs type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto"
                      (keyup.enter)="shiftFocusEnter(rowIdx, colIdx)"&gt;
                      &lt;mat-autocomplete #auto="matAutocomplete"&gt;
                              &lt;mat-option *ngFor="let option of filteredOptions | async" [value]="option"&gt;
                              {{ option }}
                              &lt;/mat-option&gt;
                          &lt;/mat-autocomplete&gt;
                      &lt;/mat-form-field&gt;
              &lt;/td&gt;
        &lt;/tr&gt;
  </code></pre>
  
  <p>This simply creates rows based on the number of objects in an array (not quite important here). </p>
  
  <p>There's also a keyup.enter event on the input fields which gets triggered when ever the user presses on enter while focus is on an input field and passes row index and column index to get the next editable element.</p>
  
  <p>Component:</p>
  
  <pre><code>shiftFocusEnter(rowIdx: number, colIdx: number) {
  console.log("Enter", rowIdx, colIdx);  
  if(colIdx == 4 &amp;&amp; rowIdx == 5) {
    console.log("Reached end of row");
  }
  else {
    colIdx = colIdx + this.columns.findIndex(c =&gt; c.editable);
    this.autocomplete.showPanel = false;
    this.focusInput(rowIdx, colIdx);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This function receives two parameters. Row Index and Column Index and calculates the index of the next editable element to focus on that. </p>
  
  <p>The line <strong>this.autocomplete.showPanel = false</strong> was written to see if I could simply close the panel like this but it didnt work.</p>
  
  <p><strong>this.autocomplete</strong> is an object of class <strong>MatAutocomplete</strong>. I've added this by writing </p>
  
  <pre><code>@Input('matAutocomplete')
  autocomplete: MatAutocomplete
  </code></pre>
  
  <p><strong>What I need:</strong></p>
  
  <p>I want the dropdown panel of the mat autocomplete element to close after pressing enter.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>So after working a bit I found this</p>
  
  <pre><code>@ViewChild('test', { read: MatAutocompleteTrigger }) test: MatAutocompleteTrigger;
  </code></pre>
  
  <p>+</p>
  
  <pre><code>this.test.closePanel();
  </code></pre>
  
  <p>This time I'm able to close the panel of the FIRST cell in the table however all the panels of the other input fields will stay open</p>