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

MatChip / MatChipSet: implemented exclusive and non-exclusive selection #601

Merged
merged 3 commits into from
Jun 13, 2020
Merged

MatChip / MatChipSet: implemented exclusive and non-exclusive selection #601

merged 3 commits into from
Jun 13, 2020

Conversation

henon
Copy link
Contributor

@henon henon commented Jun 12, 2020

According to Material Design Components: there are Choice and Filter chips.

Choice: exclusive selection (visualized by color change)
Filter: non-exclusive selection (visualized by check-marks)
Choice+Filter: exclusive selection with the ability to unselect currently selected value.

image

image

I implemented this behavior in MatChipSet which are turned on by setting the Parameter flags Choice=true or Filter=true.

MatChipSet exposes SelectedChip and SelectedChips which can be bound two-way.

Also, Chips can be configured to be removable by click on the TrailingIcon or not if IsRemovable=false. MatChipSet exposes an event that is raised for every removed chip.

This feature was requested by issue 181

This is my first full-fledged MatBlazor component and I'll apreciate any comments or tips for improvement.

I am thinking about implementing a nice little demo that shows off the new features (adding and removing chips, binding selection, etc) for our docs.

… and removed auto-remove logic.

the remove logic was a nice idea but it results in a conflict in DOM management between Blazor and JS which manifests itself in seemingly multiple chips being removed at once when clicking only one.
@henon
Copy link
Contributor Author

henon commented Jun 12, 2020

this is how the demo looks:
image

@SamProf SamProf merged commit 0be480f into SamProf:develop Jun 13, 2020
SamProf added a commit that referenced this pull request Jun 13, 2020
@SamProf
Copy link
Owner

SamProf commented Jun 13, 2020

Thank you for this contribution. That's great

@markiemarkus
Copy link

markiemarkus commented Jul 1, 2020

I've seen the demo and it working in the documentation site, but am struggling to replicate the Choice Chipset functionality

<MatChipSet Choice="true" @bind-SelectedChip="selectedChip">
        <MatChip LeadingIcon="true" Label="Male"></MatChip>
        <MatChip Label="Female"></MatChip>
        <MatChip Label="Rather not say"></MatChip="true">
        <p class="mt-2">Your choice: @selectedChip?.Label</p>
    </MatChipSet>

@henon
Copy link
Contributor Author

henon commented Jul 1, 2020

@markiemarkus have you looked at the complete source code of the chip demo? I guess you are missing a variable declaration or so.

@markiemarkus
Copy link

markiemarkus commented Jul 1, 2020

@markiemarkus have you looked at the complete source code of the chip demo? I guess you are missing a variable declaration or so.

I had thought so,
Essentially, nothing happens on clicking my choice

I have the code segment as


<MatChipSet Choice="true" @bind-SelectedChip="selectedChip">
        <MatChip Label="Male"></MatChip>
        <MatChip Label="Female"></MatChip>
        <MatChip Label="Rather not say"></MatChip="true">
        <p class="mt-2">Your choice: @selectedChip?.Label</p>
    </MatChipSet>

  @code
        {
            MatChip selectedChip = null;
        }

@henon
Copy link
Contributor Author

henon commented Jul 1, 2020

I just verified that it works. Follow these steps:

  1. create a new blazor project (I used Blazor Server)
  2. add BatBlazor 2.6.1-develop-013 (check prerelease to see it on nuget)
  3. add the using and the js/css like documented on matblazor.com
  4. add your snippet above to the Index.razor
    done

@GitSheva
Copy link

@markiemarkus have you looked at the complete source code of the chip demo? I guess you are missing a variable declaration or so.

I had thought so,
Essentially, nothing happens on clicking my choice

I have the code segment as


<MatChipSet Choice="true" @bind-SelectedChip="selectedChip">
        <MatChip Label="Male"></MatChip>
        <MatChip Label="Female"></MatChip>
        <MatChip Label="Rather not say"></MatChip="true">
        <p class="mt-2">Your choice: @selectedChip?.Label</p>
    </MatChipSet>

  @code
        {
            MatChip selectedChip = null;
        }

Hey, were you able to solve your problem? I have exactly the same issue.

@henon
Copy link
Contributor Author

henon commented Jul 16, 2020

@GitSheva are you using the preview build BatBlazor 2.6.1-develop-013 ?

@markiemarkus
Copy link

@henon @GitSheva Hi I had to install the pre-release package mentioned and this solved my issue.

@GitSheva
Copy link

@henon @markiemarkus the issue was resolved updating the package with 2.6.1. Thanks for your help.

@GitSheva
Copy link

@henon @markiemarkus another question, do you know how to make the first MatChip selected by default (only with the first load)? I used IsSelected property, it's loading control with fist chip selected but I can't change it anymore. Do you have any example? Thanks

@henon
Copy link
Contributor Author

henon commented Jul 16, 2020

I know that problem but I'm not sure it can be solved without changing something in the MatChip/MatChipSet code. Have you tried assigning the reference of the first chip to the variable selectedChip?

@GitSheva
Copy link

@henon it worked, thank you so much...

@GitSheva
Copy link

@henon maybe you know as well how to pre-select one of the choice chips by the value from code?
I have a chipset with two languages, need to pre-select one by value from local storage
Do I have to foreach all chips in chipset, compare the value and if value matches call chip.IsSelected?
Or the is any other way?
Thanks

@henon
Copy link
Contributor Author

henon commented Jul 21, 2020

I don't know off the top of my head. Either that or set SelectedChips on ChipSet via two-way binding to the Chips you want to be selected.

@peterthomastlc
Copy link

@henon, not sure if I'm begging for help in the right place here, but I'm using the Filter setting to select a combination of on/off MatChips. I need to preselect multiple chips on page initialisation, and track changes to enabled chips so that I can update the database on changes.
I can't seem to make it work. If I use chip.IsSelected in the markup, it'll preselect but prevent any further changes.
If I bind to SelectedChips, it'll track changes, but doesn't want to use it to preset chips on page load.
Any ideas would be very welcome, before I throw in the towel & fall back on a select list :-(

@henon
Copy link
Contributor Author

henon commented Aug 4, 2020

I think this is a bug that needs to be fixed in MatChipSet implementation.

@lindespang
Copy link
Collaborator

There is a bug indeed regarding preselection, I added some chips to my app and found out what the issue is. Currently I have implemented a workaround in my app but will submit a correction PR when I have time for it.

@lindespang
Copy link
Collaborator

Or I could give someone pointers if they want to fix it. (Not at the computer atm thats why I talk in riddles)

@peterthomastlc
Copy link

peterthomastlc commented Aug 4, 2020

Thanks, line 75 of BaseMatChipSet.cs looks dodgy to me - I don't see how you can ever preselect anything using set_SelectedChips?

                var selected = new HashSet<MatChip>(value);
                foreach (var chip in _chips)
                    chip.IsSelected = selected.Contains(chip);

No worries, I'll hang on for your fix, thanks.

@peterthomastlc
Copy link

Yup, replacing MatChipSet.cs,line 75 with matching on the Label value instead of a MatChip hash allows preselecting to work in OnAfterRender:-

var selected = new HashSet(value.Select(x => x.Label));
foreach (var chip in _chips)
chip.IsSelected = selected.Contains(chip.Label);

Issue #671 raised.

@SpaceOgre
Copy link

SpaceOgre commented Oct 5, 2020

I have a problem with MatChipSet/MatChip and IsSelected. If I put any value there then when I click a chip selection does not change. This seems like it should work? Or I'm using it wrong?

I guess I could do what @peterthomastlc have suggested but I would like to use the IsSelected property, otherwise what is it for?

@lindespang did you have some kind of solution/hint to give for this problem?

Edit

Now I understand why the <MatChip IsSelected=""> is not working as I want, because of the rendering. Guess I need to learn more about blazor.

Will try my hand on a solution tomorrow and create a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants