@typeparam TModel
@typeparam TId
@code {
[Parameter] public Variant Variant { get; set; }
[Parameter] public string Label { get; set; }
[Parameter] public Func GetName { get; set; }
[Parameter] public EventCallback NameChanged { get; set; }
[Parameter] public Func>> GetDataFunc { get; set; }
IEnumerable models;
[Parameter] public TId Value { get; set; }
[Parameter] public EventCallback ValueChanged { get; set; }
private TModel model;
TModel Model
{
get
{
return model;
}
set
{
model = value;
Value = (TId)(typeof(TModel).GetProperty("Id").GetValue(model));
InvokeAsync(async () => await ValueChanged.InvokeAsync(Value));
InvokeAsync(async () => await NameChanged.InvokeAsync(GetName(model)));
}
}
protected override async Task OnInitializedAsync()
{
models = await GetDataFunc();
if (model != null)
await NameChanged.InvokeAsync(GetName(model));
}
private async Task> Search(string text, CancellationToken cancellationToken)
{
return await Task.Run(() =>
{
// If text is null or empty, return no results
if (string.IsNullOrWhiteSpace(text))
return models;
// Split the search text into parts
var searchParts = text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
return models.Where(model =>
{
// Get the name of the model
string modelName = GetName(model);
// Split the model name into parts
var modelNameParts = modelName.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
// Check if ALL search parts exist in the model name parts
return searchParts.All(searchPart =>
modelNameParts.Any(namePart =>
namePart.Contains(searchPart, StringComparison.OrdinalIgnoreCase)
)
);
});
}, cancellationToken);
}
}