@typeparam T @* * dbMango * * Copyright 2025 Deutsche Bank AG * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *@
@if (!string.IsNullOrWhiteSpace(Name)) {
}
@{ var groupedValues = GetGroupedValues(); foreach ( var g in groupedValues ) { if ( groupedValues.Count > 1) {
@g.Name
} @foreach (var v in g.Values) { if (v.Value?.Equals(v) ?? false) { @GetSelectedOption(v) } else { @GetOption(v) } } } }
@code { public class SelectableItem(T val) { public bool Selected { get; set { if (field == value) return; field = value; SelectedChanged.InvokeAsync(field); } } public T Value { get; } = val; public EventCallback SelectedChanged { get; set; } } [Parameter] public string Name { get; set; } = ""; [Parameter] public List Values { get; set { if (field?.Equals(value) ?? false) return; field = value; InvokeAsync(StateHasChanged); } } = []; [Parameter] public string? Icon { get; set; } [Parameter] public string Class { get; set; } = ""; [Parameter] public string ListClass { get; set; } = ""; [Parameter] public bool Enabled { get; set; } = true; [Parameter] public Func IsSelectable { get; set; } = _ => true; [Parameter] public Func GetText { get; set; } = x => x?.ToString() ?? ""; [Parameter] public RenderFragment? OptionTemplate { get; set; } [Parameter] public RenderFragment? SelectedOptionTemplate { get; set; } private string _id = $"h{Random.Shared.Next():X8}"; private class Group { public string Name { get; set; } = string.Empty; public List Values { get; set; } = new (); } private List GetGroupedValues() { var groups = new List(); Group? currentGroup = null; foreach (var v in Values) { if (!IsSelectable(v.Value)) { // Start a new group with this name currentGroup = new () { Name = GetText(v.Value) }; groups.Add(currentGroup); } else { if (currentGroup == null) { // If no group has been started, create a default group currentGroup = new () { Name = "Not grouped" }; groups.Add(currentGroup); } currentGroup.Values.Add(v); } } return groups; } private RenderFragment GetOption(SelectableItem v) { if (OptionTemplate != null) { return @
@OptionTemplate(v.Value)
; } else { return @
@GetText(v.Value)
; } } private RenderFragment GetSelectedOption(SelectableItem v) => GetOption(v); }