components icon indicating copy to clipboard operation
components copied to clipboard

Add autocomplete example to chips documentation.

Open topherfangio opened this issue 9 years ago • 49 comments

Bug, feature request, or proposal:

Feature request.

What is the expected behavior?

In order to better understand the interaction between components, users want an example of how to utilize the MdAutocomplete with the MdChipList.

What is the current behavior?

There is no current example.

What are the steps to reproduce?

N/A

Providing a Plunker (or similar) is the best way to get the team to see your issue. Plunker template: http://plnkr.co/edit/o077B6uEiiIgkC0S06dd

N/A

What is the use-case or motivation for changing an existing behavior?

N/A

Which versions of Angular, Material, OS, browsers are affected?

All

Is there anything else we should know?

topherfangio avatar Feb 23 '17 23:02 topherfangio

I did this by adding the autocomplete inside a disabled md-basic-chip since i wanted it to wrap properly and be a part of the list.

Then the autocomplete just adds to the "chips"-list on select, but that logic exists in the current autocomplete doc.

<md-chip-list>
      <md-chip *ngFor="let chip of chips">
        <span>{{ chip .Name }}</span>
        <span (click)="removeChip(chip )">x</span>
      </md-chip>

      <md-basic-chip disabled="true">
         // insert autocomplete example here.
      </md-basic-chip>
</md-chip-list>

JakobSegerslatt avatar Mar 09 '17 10:03 JakobSegerslatt

@JakobSegerslatt Indeed, with a disabled set of chips, this is definitely achievable. The difficulty comes in when you want to allow users to select from the autocomplete, or add their own via the forthcoming mdChipInput.

I'm currently working on adding this functionality, but we need to discuss how to handle component interoperability. Particularly:

  1. How do we handle keyboard navigation? Right now, both the Autocomplete and the Chips watch for LEFT/RIGHT/UP/DOWN and it causes conflicts.

  2. The Autocomplete automatically updates the attached input with the selected value, but it doesn't fire the ENTER keypress which would trigger the (chipAdded) event. And oddly, the Autocomplete's (select) event is emitted before the input value is updated, so it there is not currently a way to add the chip properly without a setTimeout() :-/

We're looking at how to best address these issues before moving forward. Also, I'm waiting until #2476 gets merged as it contains many updates including the mdChipInput directive.

topherfangio avatar Mar 09 '17 16:03 topherfangio

Is this something that will be implemented in the near future? And updates on the progress?

spencerolsen avatar Jul 06 '17 19:07 spencerolsen

What is the status of this? I could not find an example yet or a PR that closes this issue.

webmutation avatar Sep 05 '17 08:09 webmutation

You can now place a chiplist inside a md-input-container/md-form-field. Have a look at the examples at https://material2-dev.firebaseapp.com/chips Src at: https://github.com/angular/material2/tree/master/src/demo-app/chips

Havn't tested adding autocomplete to the form-field but it shouldn't be a problem.

JakobSegerslatt avatar Sep 05 '17 08:09 JakobSegerslatt

Having an example with autocomplete would definitely still be helpful. It's possible, but it's unclear on how to structure it correctly and how to use a formControl with it.

visurel avatar Sep 20 '17 10:09 visurel

With the addition of the md-input-container/md-form-field support, would it be possible to combine the chips with a md-select (multi)?

chriszrc avatar Sep 20 '17 14:09 chriszrc

This is really nedded. I need a implementation of a Contact Chips like in version 1: https://material.angularjs.org/1.1.2/demo/chips#contact-chips

It would be great if the Material2 implements a contact-chips. Don't need to be exactly like the older, but has the same idea.

henryavila avatar Sep 25 '17 11:09 henryavila

Here is a way towards a solution (it is very basic), until there is an official one: https://angular-material2-issue-v5gpri.stackblitz.io . Maybe it can help someone get started.

https://stackblitz.com/edit/angular-material2-issue-v5gpri

axtho avatar Oct 04 '17 14:10 axtho

Nice! Ha! I ended up doing something similar, I needed chips with multiselect, and I put the select below the chips, and made the value of the select hidden, and then synced the chips model with the selections from the multiselect

chriszrc avatar Oct 04 '17 17:10 chriszrc

@chriszrc did you hide the input value of the matInput? If so, how? Been trying to do this via ViewChild, but no luck.

axtho avatar Oct 05 '17 06:10 axtho

This also works, expect that you would need to unset the focus of the input field so you can activate the autocomplete again:

       <mat-form-field floatPlaceholder="never">
            <mat-chip-list #chipList>
                <mat-chip *ngFor="let value of statusSelected" (remove)="removeStatus(value)">
                    {{value}} <mat-icon matChipRemove>cancel</mat-icon>
                </mat-chip>
                <input [matChipInputFor]="chipList"
                       [matAutocomplete]="auto">
            </mat-chip-list>
            <mat-autocomplete #auto="matAutocomplete" (optionSelected)="addStatus($event)">
                <mat-option *ngFor="let state of status" [value]="state.id">
                    <span>{{ state.name }}</span>
                </mat-option>
            </mat-autocomplete>
        </mat-form-field>

axtho avatar Oct 05 '17 07:10 axtho

I did hide the input value, but with the very quick and dirty and not recommended global style in my main style.scss:

 .my-chips-component {
   md-select.text-hidden .mat-select-value-text{
     display:none;
   }
 }

On Thu, Oct 5, 2017 at 3:27 AM, Thomas Hochstetter <[email protected]

wrote:

This also works, expect that you would need to unset the focus of the input field so you can activate the autocomplete again:

   <mat-form-field floatPlaceholder="never">
        <mat-chip-list #chipList>
            <mat-chip *ngFor="let value of statusSelected" (remove)="removeStatus(value)">
                {{value}} <mat-icon matChipRemove>cancel</mat-icon>
            </mat-chip>
            <input [matChipInputFor]="chipList"
                   [matAutocomplete]="auto">
        </mat-chip-list>
        <mat-autocomplete #auto="matAutocomplete" (optionSelected)="addStatus($event)">
            <mat-option *ngFor="let state of status" [value]="state.id">
                <span>{{ state.name }}</span>
            </mat-option>
        </mat-autocomplete>
    </mat-form-field>

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/material2/issues/3273#issuecomment-334381561, or mute the thread https://github.com/notifications/unsubscribe-auth/AYgWKS96qQgNeq3q7j4EhCAl4i_qjIZuks5spITwgaJpZM4MKpBn .

--

Chris Marx ZevRoss - Know Your Data Data Science & Spatial Analysis New website: www.zevross.com Ithaca, NY

chriszrc avatar Oct 05 '17 13:10 chriszrc

here is an update to the autocomplete chips starter ... it has two issues:

  1. The input should loose focus after selection, otherwise one needs to click outside before selecting a second option
  2. When you enter a new tag (+ Enter) it gets saved to the array, but the string remains on the input

Maybe someone has a solution for these. If the two issues could be solved then we have a good working example, until we get something official (which, I guess, will be tons better).

Here is the Stackblitz: https://angular-material2-issue-v5gpri.stackblitz.io https://stackblitz.com/edit/angular-material2-issue-v5gpri

axtho avatar Oct 09 '17 08:10 axtho

@axtho If you want to clear the field after entering a new tag you can use this ugly solution: this.chipInput['nativeElement'].value = '';

Masov avatar Oct 11 '17 12:10 Masov

@Masov thanks.

I have put all of this into the stackblitz example (updated). So there we have a tags component that does most of what we would want from autocomplete and chips. Maybe someone improves this as well, since it may not be the most efficient way to do this.

Oh, and best of all ... it works with reactive forms. You're welcome.

https://stackblitz.com/edit/angular-material2-issue-v5gpri

Until we all get an official example 👍 Happy coding

axtho avatar Oct 13 '17 07:10 axtho

@axtho thank you very much for this! However, i don't see that typing filters the autocomplete. Also, when clicking on an autocomplete option the input text doesn't clear.

Are these known limitations or bugs?

Thank you

eyalhakim avatar Nov 04 '17 11:11 eyalhakim

@eyalhakim I borrowed the example that @axtho made and modified it to meet my needs. Perhaps it will work for you. I added the ability to filter based on what is being typed and the ability to add or not to add new tags (via an input) https://stackblitz.com/edit/angular-material2-issue-qbzcst

sternetj avatar Nov 06 '17 16:11 sternetj

@sternetj that is a great implementation, i will definitely use some of your code, thank you!

You do have a strange behavior though when "allowAddNew" is set to "true" - if i select a certain item from the list after only writing a part of that word in the input, both the existing value from the list is added as a chip and the new value that i wrote in the input (the partial).

eyalhakim avatar Nov 06 '17 17:11 eyalhakim

Also, @sternetj, i see that adding validators (tried adding email validator) doesn't affect the control at all.

eyalhakim avatar Nov 07 '17 10:11 eyalhakim