Skip to content

Autocomplete focus when inside a dialog #7972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Swoox opened this issue Oct 23, 2017 · 8 comments
Closed

Autocomplete focus when inside a dialog #7972

Swoox opened this issue Oct 23, 2017 · 8 comments

Comments

@Swoox
Copy link

Swoox commented Oct 23, 2017

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

@debslord
Copy link

I'm having exactly the same issue - not that that helps you as I haven't found a workaround yet. Sorry

@amcdnl
Copy link
Contributor

amcdnl commented 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

@Swoox
Copy link
Author

Swoox commented Oct 24, 2017

Cheers this works for me.

@Swoox Swoox closed this as completed Oct 24, 2017
@debslord
Copy link

Workaround does work a bit of a hack as it's a shame this then means the autocomplete cannot be tabbed to

@Swoox
Copy link
Author

Swoox commented Oct 24, 2017

Restores focus to the element that was focused before the dialog opened. This kind is false in this situation but yeah I agree it's a workaround.

@willshowell
Copy link
Contributor

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

@debslord
Copy link

debslord commented Oct 25, 2017

That's great thank you!
Weirdly for me I have to put it on a different control (rather than the content div tag like in your example) otherwise the focus is initially on the original page where the the dialog was called from. But that's fine it's definitely a better solution for me - thanks again!

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants