Skip to content

Strange focus behaviour for dynamic matMenu content #7973

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
skappler opened this issue Oct 23, 2017 · 5 comments
Closed

Strange focus behaviour for dynamic matMenu content #7973

skappler opened this issue Oct 23, 2017 · 5 comments
Assignees

Comments

@skappler
Copy link

Bug, feature request, or proposal:

Bug

What is the expected behavior?

No menu item should be selected automatically

What is the current behavior?

I have a matMenu which contains an input field which can be used to filter the menu items. After entering the first character of the filter string, the input loses focus and one of the menu items is selected. After re-focussing the input and typing more, it behaves as expected

What are the steps to reproduce?

See this plunker http://plnkr.co/edit/dc3vNd4viVg2jM6Errtm?p=preview

Is there anything else we should know?

It worked on angular 2.4.5 and material 2.0.0-beta.1
Was broken after upgrading to 4.4.4 and beta.12

@julianobrasil
Copy link
Contributor

As a workaround, you can do something like the code bellow as soon as the filter value change:

@ViewChild(MatInput) input = MatInput;

...

setTimeout(() => this.input._elementRef.nativeElement.focus(), 200);

Plunk: http://plnkr.co/edit/hCg5lVZ1U5OFek31Vr0j?p=preview

@skappler
Copy link
Author

skappler commented Oct 24, 2017

Thanks for the suggestion. I built a similar workaround, binding the focus() call to the onBlur event of the input

@crisbeto
Copy link
Member

crisbeto commented Nov 4, 2017

Menus aren't really meant to be used for filtering, that's why focus ends up being stolen away from the input. I think what you're looking for is something like this which will be added to mat-select. If you really want to continue using a menu for it, you can go with the suggestion from @julianobrasil or create a custom menu panel by implementing the MatMenuPanel interface: https://github.com/angular/material2/blob/master/src/lib/menu/menu-panel.ts#L13.

@crisbeto crisbeto closed this as completed Nov 4, 2017
@cfremgen
Copy link

cfremgen commented Feb 1, 2019

Since the referenced PR looks to be dead, here's what I came up with:

<mat-menu #layoutsMenu="matMenu">
    <mat-form-field>
        <input #searchInput matInput (blur)="searchInput.focus()"
            (click)="$event.stopPropagation()">
    </mat-form-field>
</mat-menu>

@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 10, 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