@* ChipContainer.razor *@ @typeparam T
@* Loop through and display each item as a chip *@ @foreach (var item in Items) { @DisplayFunc(item) } @* Render the input control provided by the consumer *@
@if (InputContent is not null) { @InputContent(this) }
@SubmitButton
@code { /// /// The list of items to display and manage. /// [Parameter] public required List Items { get; set; } = new(); /// /// Required for two-way binding (@bind-Items). /// [Parameter] public EventCallback> ItemsChanged { get; set; } /// /// The RenderFragment that defines the custom input control. /// The 'context' is a reference to this component instance. /// [Parameter] public RenderFragment>? InputContent { get; set; } [Parameter] public RenderFragment SubmitButton { get; set; } /// /// A function to convert an item of type T to a string for display in the chip. /// Defaults to item.ToString(). /// [Parameter] public Func DisplayFunc { get; set; } = item => item?.ToString() ?? string.Empty; /// /// A public method that the consumer's input control can call to add a new item. /// public async Task AddItem(T item) { if (item is null || (item is string str && string.IsNullOrWhiteSpace(str))) { return; } // Add the item if it doesn't already exist if (!Items.Contains(item)) { Items.Add(item); await ItemsChanged.InvokeAsync(Items); } } /// /// Removes an item from the list when the chip's close icon is clicked. /// private async Task RemoveItem(T item) { Items.Remove(item); await ItemsChanged.InvokeAsync(Items); } }