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

MatChipSet: set_SelectedChips cannot be used to preselect chips #671

Open
peterthomastlc opened this issue Aug 4, 2020 · 1 comment
Open
Labels
bug Something isn't working

Comments

@peterthomastlc
Copy link

peterthomastlc commented Aug 4, 2020

When using MatChipSet.SelectedChips to preselect displayed chips in OnAfterRender, the comparison is done on a Hash of the MatChip, so a match will never be found.
As a result, chips cannot be preselected. Suggested fix below...

Reproduction Steps

  1. Use the code in the sample/demo application on the 'Chip' page.

  2. In the 'Filter ChipSet' section, replace the line
    MatChip[] selectedChips = null;
    with
    MatChip[] selectedChips = new MatChip[] { new MatChip() { Label = "Wedges" } };

  3. In the Razor file, add a ref value to the MatChipSet:-

     <MatChipSet @ref="filterChipset" Filter="true" @bind-SelectedChips="selectedChips">
    
  4. Override the OnAfterRender method to set the selected chips:-

        protected override void OnAfterRender(bool firstRender)
         {
             if (firstRender)
                 filterChipset.SelectedChips = selectedChips;
         }
    

Expected Result
The 'Wedges' chip should be selected when the page is shown.

Actual Result
All chips are deselected.

Suggested Fix
In MatChipSet.cs, at line 75, match on the Label value, not a hash of the MatChip object:-
Replace

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

With

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

I'd have submitted a pull request, but I'm not a registered contributor...

@peterthomastlc peterthomastlc added the bug Something isn't working label Aug 4, 2020
petert1401 pushed a commit to petert1401/MatBlazor that referenced this issue Aug 4, 2020
@SpaceOgre
Copy link

SpaceOgre commented Oct 8, 2020

It is possible to solve this with using @ref on the MatChip and storing it in an array or something like it. Example:

Markup

<MatChipSet Filter="true" @bind-SelectedChips="selectedChips">
  <MatChip Label="Wedges" IsCheckable="true" @ref="allChips[0]"/>
  <MatChip Label="Circles" IsCheckable="true" @ref="allChips[1]"/>
  <MatChip Label="Squares" IsCheckable="true" @ref="allChips[2]"/>
</MatChipSet>

Code

private MatChip[] allChips = new MatChip[3];
private MatChip[] selectedChips;

protected override void OnAfterRender(bool firstRender)
{
    if (firstRender)
        selectedChips = allChips.Where(x => x.Label == "Wedges").ToArray();
}

It would still be nice with an easier way to do this, but maybe a parameter to access _chips from MatChipSet is enough.

petert1401 pushed a commit to petert1401/MatBlazor that referenced this issue Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants