Unplanned
Last Updated: 02 Jun 2025 14:33 by Lukasz
Lukasz
Created on: 02 Jun 2025 14:33
Category: AutoComplete
Type: Bug Report
0
AutoComplete dropdown opens after the component is blurred

The AutoComplete DebounceDelay value is larger and the user is quick to type and blur the component, the dropdown will still open when this is no longer necessary.

A possible workaround is to cancel the Open event if it fires too soon after OnChange (uncomment the yellow line):

<p>Quickly type a number (1 - 24) and blur the component:</p>

<TelerikAutoComplete Data="@ListItems"
                     @bind-Value="@SelectedValue"
                     ValueField="@nameof(ListItem.Text)"
                     DebounceDelay="@AutoCompleteDebounceDelay"
                     OnChange="@OnAutoCompleteChange"
                     OnOpen="@OnAutoCompleteOpen"
                     Width="300px" />

@code {
    private List<ListItem> ListItems { get; set; } = new();

    private string SelectedValue { get; set; } = string.Empty;

    private const int AutoCompleteDebounceDelay = 1000;

    private DateTime LastAutoCompleteOnChange { get; set; } = DateTime.Now;

    private void OnAutoCompleteOpen(AutoCompleteOpenEventArgs args)
    {
        if (DateTime.Now - LastAutoCompleteOnChange < new TimeSpan(0, 0, 0, AutoCompleteDebounceDelay * 2))
        {
            //args.IsCancelled = true;
        }
    }

    private void OnAutoCompleteChange(object currentValue)
    {
        LastAutoCompleteOnChange = DateTime.Now;
    }

    protected override void OnInitialized()
    {
        ListItems = new List<ListItem>();

        for (int i = 1; i <= 24; i++)
        {
            ListItems.Add(new ListItem()
            {
                Id = i,
                Text = $"{i}",
                Category = $"Category {i % 6 + 1}"
            });
        }

        base.OnInitialized();
    }

    public class ListItem
    {
        public int Id { get; set; }
        public string Text { get; set; } = string.Empty;
        public string Category { get; set; } = string.Empty;
    }
}

0 comments