Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
1.0 kB
2
Indexable
Never
<MudChipSet @bind-SelectedChips="_selected" SelectedChipChanged="Callback" MultiSelection="true" Filter="filter">
    <MudChip Text="Milk"></MudChip>
    <MudChip Text="Eggs"></MudChip>
</MudChipSet>

<div class="d-flex flex-column align-center">
    @if (_selected != null && _selected.Length>0)
    {
        <MudText>You selected @string.Join(", ", _selected.Select(chip=>chip.Text)).</MudText>
    }
    else
    {
        <MudText>Nothing selected.</MudText>
    }
</div>

@code {
    bool filter = true;
    private MudChip[] _selected;
    
    // On init method
    protected override void OnInitialized()
    {
        _selected = new MudChip[] { new () { Text = "Milk" } };
    }

    private void Callback(MudChip chip)
    {
        if (_selected.Length == 1 && _selected.First().Text == chip.Text)
            return;
        
        _selected = _selected.Any(c => c.Text == chip.Text) 
            ? _selected.Where(c => c.Text != chip.Text).ToArray() 
            : _selected.Append(chip).ToArray();
    }
}