question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hide autocomplete options panel on disabled or add hidePanel method.

See original GitHub issue

Bug, feature request, or proposal:

(More a suggestion as a bug. But a unwanted behavior of users.) Hide the options panel of autocomplete if the input element goes disabled. Or add a method to hide the panel in an official way.

What is the expected behavior?

Hide the option panel if the input is disabled. Also if the panel was displayed before the input disabled.

What is the current behavior?

The option panel still displayed. The user can select an item. Also if the input is disabled. But note: Only when the clear button stops propagation. That’s a correct behavior but not as the user expected.

What are the steps to reproduce?

  1. User selects an item from mat-autocomplete. (works as expected.)
  2. User clicks again on the input to view the option, (ok)
  3. The user clicks on the clear button on the right (matSuffix). The method on the clear button disables the input and sets event.stopPropagation() to prevent the underlayed input get focused. Result: The options panel still showing. The user can select an item. Also if the input is disabled.

What is the use-case or motivation for changing an existing behavior?

The clear button stops the propagation becuase the button is over the input element. I do not want to select the input on clear-button clicked. But the stopPropagation() enables this “bug” here descripted. (User can select a option also if input is disabled after clear)

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

5.0.1

Is there anything else we should know?

It’s possible to hide the panel like

myAutoComplete.panel.nativeElement.hidden = true;

But this will still hide the panel after user clicked into the input again. Until the user clicked outside of the input an the input again.

Solutions:

  • Always hide the panel if the input is disabled.
  • Add a method like showPanel for hidePanel. But do not simple hidden = true of the native element. Because of the issue explained above.

I’ve found no hidePanel() method. Only a showPanel() method. What’s wrong with a hide method?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
crisbetocommented, Jan 30, 2018

The MatAutocompleteTrigger directive has openPanel and closePanel methods, however it doesn’t look like we’ve exported it through the template. You should still be able to get a reference to it through a ViewChild though. E.g.

class YourComponent {
  @ViewChild(MatAutocompleteTrigger) autocompleteTrigger: MatAutocompleteTrigger;

  closeAutocomplete() {
    this.autocompleteTrigger.closePanel();
  }
}
2reactions
crisbetocommented, Jan 31, 2018

autoTests is referring to the MatAutocomplete, not the MatAutocompleteTrigger. After #9703 gets in, you should be able to get a hold of the trigger instance like this:

<mat-form-field>
  <input matInput #inputTest #trigger="matAutocompleteTrigger" placeholder="Test" [formControl]="testControl" [matAutocomplete]="autoTests">
  <button mat-icon-button matSuffix *ngIf="testControl.value && testControl.enabled" (click)="clearTestSelect($event)">
    <mat-icon>clear</mat-icon>
  </button>
  <mat-autocomplete #autoTests="matAutocomplete" [displayWith]="displayFnTest" (optionSelected)="selectTest($event);">
 <!-- rest of your code -->
</mat-form-field>

<button (click)="trigger.closePanel()">Close panel test</button>

Until the PR gets released, you can refer to https://github.com/angular/material2/issues/9687#issuecomment-361732004.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Material: Hide Autocomplete Panel when User hits ...
Then you can use this to close the dropdown options: this.autocomplete.closePanel();. Make sure to also import ViewChild:
Read more >
Autocomplete | Angular Material
A stream of actions that should close the autocomplete panel, including when an option is selected, on blur, and when TAB is pressed....
Read more >
Specifying Preferences for a desktop policy
Use the Preferences tab of the Desktop Settings document, and all its sub-tabs, to specify client preferences for users of the policy.
Read more >
jQuery Tutorial - Google Sites
Demonstrates the jQuery hide() method, hiding the current HTML element ... <p class="flip">Show/Hide Panel</p> ... Adds HTML after selected elements ...
Read more >
Google Code Archive - Google Code
3272, Verified, [Firebug Lite] Install Google Chrome extension results in a 404 ... 3250, Works, General messages of Script Panel should hide Panel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found