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.

Autocomplete focus when inside a dialog

See original GitHub issue

Bug, feature request, or proposal:

Bug

What is the expected behavior?

When opening a dialog the autocomplete doesn’t have focus. Expected: https://angular-qgx8sh.stackblitz.io/

What is the current behavior?

When opening a dialog my autocomplete get focussed.

What are the steps to reproduce?

Just open a dialog with data of the states in it: this.dialog.open(SlideInToolbar, { width: '150px', data: { states: this.states } });

And inside the dialog an autocomplete with: <mat-option *ngFor="let item of data.states" [value]="item.name">

https://angular-omzvev.stackblitz.io/

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

I want to implement a autocomplete in a dialog without focus.

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

Angular: 4.4.4 Material: 2.0.0-beta.12

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
willshowellcommented, Oct 24, 2017

Depending on your situation, a better solution may be to add cdk-focus-initial to whichever element you want to focus first.

<div mat-dialog-content cdk-focus-initial> ... </div>

https://stackblitz.com/edit/angular-tksjrg?file=app%2Ftoolbar.html

1reaction
amcdnlcommented, Oct 23, 2017

This is working by design.

https://github.com/angular/material2/blob/master/src/lib/dialog/dialog-container.ts#L150

When the dialog opens it needs to switch the focus from the button that initiated the dialog the a focusable element in the dialog ( your input in this case ).

You can get around this by adding tabindex="-1" to the input like so: https://stackblitz.com/edit/angular-rrfabk

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular material mat-autocomplete on dialog opens panel
The results panel will automatically open when there are options to be displayed and the field has focus. Dialogs by default will set...
Read more >
Autocomplete | Angular Material
Start by creating the autocomplete panel and the options displayed inside it. ... At this point, the autocomplete panel should be toggleable on...
Read more >
Demos > Autocomplete - Angular Material
By default, md-autocomplete will cache results when performing a query. After the initial call is performed ... Inside Dialog. HTML JS dialog.tmpl.html HTML ......
Read more >
Dialog / How To / Close Dialog when click outside of its region
Close Dialog when click outside of its region in Angular Dialog component ... By default, dialog can be closed by pressing Esc key...
Read more >
Dialog | Angular Material
When a dialog is opened, it will move focus to the first focusable element that it can find. In order to prevent users...
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