Skip to content

MatAutocomplete selection option event order #18650

Closed

Description

@raygig

Feature Description

The current order of events when selecting an option from MatAutocomplete is as follows:

  1. Update the NgModel
  2. Bring focus to the autocomplete
  3. Emit select event which calls @output() optionSelected

This is the code which executes this logic.

    MatAutocompleteTrigger.prototype._setValueAndClose = 
    function (event) {
        if (event && event.source) {
            this._clearPreviousSelectedOption(event.source);
            this._setTriggerValue(event.source.value);
            this._onChange(event.source.value);
            this._element.nativeElement.focus();
            this.autocomplete._emitSelectEvent(event.source);
        }
        this.closePanel();
    };

I am proposing to call the focus() event after this.autocomplete._emitSelectEvent(event.source);

The issue is if you want to test the current selected model value in the focus() event it will be incorrect because the setting of the model happens after focus is called.

Use Case

In the focus event we are testing if the model value is blank and if so then open the panel to give suggested search items.

Activity

  1. added
    feature
    on Feb 28, 2020
  2. crisbeto commented on Feb 29, 2020

    @crisbeto
    Member

    Rather than using the focus even, couldn't you use the optionSelected event instead? It's not a big deal to change the behavior, but I'm trying to understand what the use case is.

  3. raygig commented on Feb 29, 2020

    @raygig
    Author

    We are trying to build a suggestion feature which is activated when the autocomplete is focused and the ngmodel is null.

    Currently this breaks with this scenario.

    1. Autocomplete has ngmodel===null
    2. The user focuses the autocomplete
    3. Suggestions are displayed
    4. User searches ands selects an option
    5. The suggestion options display again

    #5 happens because the focus event triggers before ngmodel is set.

  4. self-assigned this
    on Mar 4, 2020
  5. added
    P4
    on Mar 4, 2020
  6. added a commit that references this issue on Mar 4, 2020
    0129a48
  7. added a commit that references this issue on Apr 7, 2020
    410b2a7
  8. added 2 commits that reference this issue on Jan 18, 2022
    7409bd6
    402c07b
  9. angular-automatic-lock-bot commented on Feb 18, 2022

    @angular-automatic-lock-bot

    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.

  10. locked and limited conversation to collaborators on Feb 18, 2022
  11. added a commit that references this issue on Apr 3, 2022
    03cda92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P4area: material/autocompletefeature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions