Autocomplete focus when inside a dialog
See original GitHub issueBug, 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:
- Created 6 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Depending on your situation, a better solution may be to add
cdk-focus-initial
to whichever element you want to focus first.https://stackblitz.com/edit/angular-tksjrg?file=app%2Ftoolbar.html
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