using Connected.Annotations;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
namespace Connected.Components;
public partial class ChipSet : UIComponent, IDisposable
{
protected string Classname =>
new CssBuilder("mud-chipset")
.AddClass(Class)
.Build();
///
/// Child content of component.
///
[Parameter]
[Category(CategoryTypes.ChipSet.Behavior)]
public RenderFragment ChildContent { get; set; }
///
/// Allows to select more than one chip.
///
[Parameter]
[Category(CategoryTypes.ChipSet.Behavior)]
public bool MultiSelection { get; set; } = false;
///
/// Will not allow to deselect the selected chip in single selection mode.
///
[Parameter]
[Category(CategoryTypes.ChipSet.Behavior)]
public bool Mandatory { get; set; } = false;
///
/// Will make all chips closable.
///
[Parameter]
[Category(CategoryTypes.ChipSet.Behavior)]
public bool AllClosable { get; set; } = false;
///
/// Will show a check-mark for the selected components.
///
[Parameter]
[Category(CategoryTypes.ChipSet.Appearance)]
public bool Filter
{
get => _filter;
set
{
if (_filter == value)
return;
_filter = value;
StateHasChanged();
foreach (var chip in _chips)
chip.ForceRerender();
}
}
///
/// Will make all chips read only.
///
[Parameter]
[Category(CategoryTypes.ChipSet.Behavior)]
public bool ReadOnly { get; set; } = false;
///
/// The currently selected chip in Choice mode
///
[Parameter]
[Category(CategoryTypes.ChipSet.Behavior)]
public Chip SelectedChip
{
get { return _chips.OfType().FirstOrDefault(x => x.IsSelected); }
set
{
if (value == null)
{
foreach (var chip in _chips)
{
chip.IsSelected = false;
}
}
else
{
foreach (var chip in _chips)
{
chip.IsSelected = (chip == value);
}
}
this.InvokeAsync(StateHasChanged);
}
}
///
/// Called when the selected chip changes, in Choice mode
///
[Parameter]
public EventCallback SelectedChipChanged { get; set; }
///
/// The currently selected chips in Filter mode
///
[Parameter]
[Category(CategoryTypes.ChipSet.Behavior)]
public Chip[] SelectedChips
{
get { return _chips.OfType().Where(x => x.IsSelected).ToArray(); }
set
{
if (value == null || value.Length == 0)
{
foreach (var chip in _chips)
{
chip.IsSelected = false;
}
}
else
{
var selected = new HashSet(value);
foreach (var chip in _chips)
{
chip.IsSelected = selected.Contains(chip);
}
}
StateHasChanged();
}
}
protected override void OnInitialized()
{
base.OnInitialized();
if (_selectedValues == null)
_selectedValues = new HashSet