Skip to content
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

Autocomplete stops showing the List when clicking in the input field after adding a Mat-Chip #17292

Open
vicmarcal opened this issue Oct 4, 2019 · 12 comments
Labels
area: material/chips P4 A relatively minor issue that is not relevant to core functions

Comments

@vicmarcal
Copy link

Reproduction

Use StackBlitz to reproduce your issue:
Official Demo shows this issue.
https://run.stackblitz.com/api/angular/v1?file=app%2Fchips-autocomplete-example.ts

This example starts showing a Lemon in the autocomplete component.

Steps to reproduce:

  1. Click in the Input of Autocomplete, next to the Lemon matchip
  2. It properly shows the List associated
  3. Click in an Option of the List, i.e "Apple". The mat-chip is added next to "Lemon".
  4. Click in the Input of Autocomplete. Now it does not show the "List", as you would expect and as it did in step 2, which breaks the UIX.
  5. Click outside the component (it loses the Focus)
  6. Click again the Input field: it receives the Focus and the List is again shown.

Expected Behavior

What behavior were you expecting to see?
Users expect that clicking in the Input field would show them the List of options again, as it did in the very beginning without needing to use the "hackish" click out-and-in.

Actual Behavior

What behavior did you actually see?
If a Mat-chip is added, the user needs to focus out and then click to set the Focus in, in order to have the List shown.

Environment

  • Angular: 8.1.0
  • CDK/Material: Material 8.0.2
  • Browser(s): Chrome, Safari, Firefox
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows, Ubuntu, MacOS
@vicmarcal
Copy link
Author

vicmarcal commented Oct 4, 2019

As an ugly fix, you can disable and enable the Control at the end of the "selected" method:
formControl.disable(); formControl.enable();
as you can see in this proof of concept:
https://angular-cp6d1k-h8up2b.stackblitz.io
You can see that thanks to the enable/disable hack in the selected() now the described issue is "solved". Feel free to comment/uncomment to feel the difference.

However I bet this will expose some counterbacks with advanced user navigation probably.
Hope it helps!

@crisbeto
Copy link
Member

crisbeto commented Oct 6, 2019

I'm guessing it's because the list only shows up on focus and focus technically hasn't left the input. There's a PR to make it also open on clicks #16020.

@vicmarcal
Copy link
Author

vicmarcal commented Oct 7, 2019

Yup. That's my guess too "(...) the user needs to focus out and then click to set the Focus in, in order to have the List shown."
So a Focus issue to me. I didn't find a unsetFocus method for FormControls, so I decided to use the disable/enable which seemed to unset focus.

@crisbeto
Copy link
Member

crisbeto commented Oct 7, 2019

It might be simpler to call blur on the input.

@francittadini
Copy link

It might be simpler to call blur on the input.

This worked for me, good idea :)

@mmalerba mmalerba added the needs triage This issue needs to be triaged by the team label May 20, 2020
@GitBrainsStudio
Copy link

It might be simpler to call blur on the input.

how it make?

@jelbourn jelbourn added area: material/chips has pr P4 A relatively minor issue that is not relevant to core functions and removed needs triage This issue needs to be triaged by the team labels May 28, 2020
@hiepxanh
Copy link

hiepxanh commented Jun 6, 2020

const chipElement = document.querySelector('#chip-list');
chipElement.blur();
setTimeout(() => { chipElement.focus() }, 1);

help resolve the problem for me.

@almcaffee
Copy link

almcaffee commented Jan 15, 2021

For anyone still needing this...

  @ViewChild('inputName') inputName: ElementRef<HTMLInputElement>;

  selected(event: MatAutocompleteSelectedEvent): void {
    ....
    this.inputName.nativeElement.blur();
    ....
  }

Hope this helps took a while to get it working smoothly today

Edit: Heres a snippet of the template...

<mat-form-field>
      <mat-label>{{ 'views.user.permissions.additional_data_center' | translate }}</mat-label>
      <mat-chip-list #chipList aria-label="{{ 'views.user.permissions.additional_data_center' | translate }}">
          <mat-chip *ngFor="let d of dataCenters | idFilter:'id':selectedDataCenters:5" 
              [selectable]="true"
              [removable]="true" 
              (removed)="remove(d)">
              {{d.name}}
              <mat-icon matChipRemove>cancel</mat-icon>
          </mat-chip>
          <input #dcInput
                  [matChipInputFor]="chipList"
                  [matAutocomplete]="auto"
                  [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                  [matChipInputAddOnBlur]="true"
                  (matChipInputTokenEnd)="add($event)">
          <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
              <mat-option *ngFor="let dc of filterDataCenters" [value]="dc.id">
              {{dc.name}}
              </mat-option>
          </mat-autocomplete>
          <span matSuffix *ngIf="selectedDataCenters.length > 5">+{{selectedDataCenters.length - 5 }} </span>
      </mat-chip-list>
  </mat-form-field>

@Koniouchine
Copy link

For anyone still needing this...

  @ViewChild('inputName') inputName: ElementRef<HTMLInputElement>;

  selected(event: MatAutocompleteSelectedEvent): void {
    ....
    this.inputName.nativeElement.blur();
    ....
  }

Hope this helps took a while to get it working smoothly today

Edit: Heres a snippet of the template...

<mat-form-field>
      <mat-label>{{ 'views.user.permissions.additional_data_center' | translate }}</mat-label>
      <mat-chip-list #chipList aria-label="{{ 'views.user.permissions.additional_data_center' | translate }}">
          <mat-chip *ngFor="let d of dataCenters | idFilter:'id':selectedDataCenters:5" 
              [selectable]="true"
              [removable]="true" 
              (removed)="remove(d)">
              {{d.name}}
              <mat-icon matChipRemove>cancel</mat-icon>
          </mat-chip>
          <input #dcInput
                  [matChipInputFor]="chipList"
                  [matAutocomplete]="auto"
                  [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                  [matChipInputAddOnBlur]="true"
                  (matChipInputTokenEnd)="add($event)">
          <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
              <mat-option *ngFor="let dc of filterDataCenters" [value]="dc.id">
              {{dc.name}}
              </mat-option>
          </mat-autocomplete>
          <span matSuffix *ngIf="selectedDataCenters.length > 5">+{{selectedDataCenters.length - 5 }} </span>
      </mat-chip-list>
  </mat-form-field>

This was a clear solution to the chip list autocomplete not working properly after selection. Even the current documentation has this bug.
Thanks for the help!

@almcaffee
Copy link

@Koniouchine .. glad I could help.

@128keaton
Copy link

128keaton commented Apr 14, 2021

I made this little directive to "fix" this issue
Currently, it'll show after adding from the list or removing from the list

https://gist.github.com/128keaton/a67c3fe14d70dd3becd4628f56b18e34

Just bind it to the mat-form-field containing everything
Screen Shot 2021-04-14 at 4 24 44 PM

@atilaoliveirajr
Copy link

atilaoliveirajr commented Jul 29, 2021

I made this little directive to "fix" this issue
Currently, it'll show after adding from the list or removing from the list

https://gist.github.com/128keaton/a67c3fe14d70dd3becd4628f56b18e34

Worked like a charm! Perfect solution. Thank you!

@mmalerba mmalerba removed the has pr label Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/chips P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

No branches or pull requests