@typeparam TField
@inherits InputBase<TField>

<select class="form-select" @bind="Binding">
    @foreach(var item in Items)
    {
        <option value="@(item!.GetHashCode())">@(DisplayField(item))</option>
    }
</select>

@code
{
    [Parameter]
    public IEnumerable<TField> Items { get; set; }
    
    [Parameter]
    public Func<TField, string> DisplayField { get; set; }
    
    [Parameter]
    public Func<Task>? OnChange { get; set; }

    protected override void OnInitialized()
    {
        
    }

    protected override string? FormatValueAsString(TField? value)
    {
        if (value == null)
            return null;
        
        return DisplayField.Invoke(value);
    }

    protected override bool TryParseValueFromString(string? value, out TField result, out string? validationErrorMessage)
    {
        validationErrorMessage = "";
        result = default(TField)!;
        return false;
    }

    private int Binding
    {
        get
        {
            if (Value == null)
                return -1;

            return Value.GetHashCode();
        }
        set
        {
            var i = Items.FirstOrDefault(x => x!.GetHashCode() == value);

            if (i != null)
            {
                Value = i;
                ValueChanged.InvokeAsync(i);
                OnChange?.Invoke();
            }
        }
    }
}
