@*
* 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.
*@
@code {
[Parameter] public string Name { get; set; } = "";
[Parameter]
public string Value
{
get;
set
{
if (field == value)
return;
field = value;
ValueChanged.InvokeAsync(value);
}
} = "";
[Parameter] public EventCallback ValueChanged { get; set; }
[Parameter] public IReadOnlyCollection Values { get; set; } = [];
[Parameter] public string? Placeholder { get; set; } = "";
[Parameter] public string? Icon { get; set; }
[Parameter] public string Class { get; set; } = "";
[Parameter] public bool Enabled { get; set; } = true;
[Parameter] public bool ShowDropdown { get; set; }
[Parameter] public EventCallback ShowDropdownChanged { get; set; }
[Parameter] public bool IsMultiline { get; set; } = false;
[Parameter] public int Rows { get; set; } = 1;
private bool IsExpanded { get; set; } = false;
private void ToggleExpand()
{
IsExpanded = !IsExpanded;
_rows = IsExpanded ? 10 : Rows; // Adjust the number of rows when expanded
ShowDropdown = false;
ShowDropdownChanged.InvokeAsync(ShowDropdown);
StateHasChanged();
}
private readonly string _id = $"h{Random.Shared.Next():X8}";
private string FormGroupClass => string.IsNullOrWhiteSpace(Name) ? Class : $"form-group {Class}";
private List FilteredValues { get; set; } = [];
private string DropDownMenuClass => IsMultiline ? "dd-menu-multi" : "dd-menu";
private int _rows = 1;
private void FilterValues(ChangeEventArgs e)
{
Value = e.Value?.ToString() ?? "";
ApplyFiltering();
}
private void ApplyFiltering()
{
if (string.IsNullOrEmpty(Value))
{
// show everything if the input is empty
FilteredValues = Values.ToList();
}
else
{
FilteredValues = Values.Where(v => v.Contains(Value, StringComparison.OrdinalIgnoreCase)).ToList();
}
ShowDropdown = FilteredValues.Any();
ShowDropdownChanged.InvokeAsync(ShowDropdown);
StateHasChanged();
}
private void SelectValue(string value)
{
Value = value;
ValueChanged.InvokeAsync(value);
Value = value;
ShowDropdown = false;
ShowDropdownChanged.InvokeAsync(ShowDropdown);
}
private Task OnFocus()
{
ApplyFiltering();
return Task.CompletedTask;
}
protected override void OnInitialized()
{
_rows = Rows;
}
private void OnKeyDown(KeyboardEventArgs e)
{
if (e.Key == "Escape" && ShowDropdown )
{
ShowDropdown = false;
ShowDropdownChanged.InvokeAsync(ShowDropdown);
StateHasChanged();
}
}
}