From 2ea4ff518705c3710b5fc51422bf2b59f0157ef3 Mon Sep 17 00:00:00 2001 From: Robert Haken Date: Mon, 16 Oct 2023 10:16:55 +0200 Subject: [PATCH] fixes #572 [HxSearchBox] Initial suggestions disappear when the DataProvider response is quick --- .../Pages/HxSearchBox_Issue572_Test.razor | 78 +++++++++++++++++++ .../Forms/SearchBox/HxSearchBox.razor.cs | 14 ++-- 2 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 BlazorAppTest/Pages/HxSearchBox_Issue572_Test.razor diff --git a/BlazorAppTest/Pages/HxSearchBox_Issue572_Test.razor b/BlazorAppTest/Pages/HxSearchBox_Issue572_Test.razor new file mode 100644 index 000000000..76ec92546 --- /dev/null +++ b/BlazorAppTest/Pages/HxSearchBox_Issue572_Test.razor @@ -0,0 +1,78 @@ +@page "/HxSearchBox_Issue572_Test" + + + +
Search for Mouse, Table or Door...
+
+
+ +

selectedItemTitle: @selectedItemTitle

+

textQueryTriggered: @textQueryTriggered

+

textQuery: @textQuery

+ +@code { + private string selectedItemTitle = string.Empty; + private string textQueryTriggered = string.Empty; + private string textQuery = string.Empty; + private void HandleItemSelected(SearchBoxItem item) + { + selectedItemTitle = item.Title; + } + private void HandleTextQueryTriggered(string text) + { + textQueryTriggered = text; + } + List Data { get; set; } = new() + { + new() + { + Title = "Table", + Subtitle = "$5000", + Icon = BootstrapIcon.Table + }, + new() + { + Title = "Mouse", + Subtitle = "$40", + Icon = BootstrapIcon.Mouse + }, + new() + { + Title = "Door", + Subtitle = "$100", + Icon = BootstrapIcon.DoorClosed + } + }; + + Task> ProvideSearchResults(SearchBoxDataProviderRequest request) + { + if (request.UserInput?.Length > 0) + { + SearchBoxDataProviderResult result2 = new() + { + Data = Data.Where(i => i.Title.Contains(request.UserInput, StringComparison.OrdinalIgnoreCase)).ToArray() + }; + return Task.FromResult(result2); + } + SearchBoxDataProviderResult result = new() + { + Data = Data.Take(10).ToArray() + }; + return Task.FromResult(result); + } + + record SearchBoxItem + { + public string Title { get; set; } + public string Subtitle { get; set; } + public BootstrapIcon Icon { get; set; } + } +} \ No newline at end of file diff --git a/Havit.Blazor.Components.Web.Bootstrap/Forms/SearchBox/HxSearchBox.razor.cs b/Havit.Blazor.Components.Web.Bootstrap/Forms/SearchBox/HxSearchBox.razor.cs index e36c4c6c6..d43480f6f 100644 --- a/Havit.Blazor.Components.Web.Bootstrap/Forms/SearchBox/HxSearchBox.razor.cs +++ b/Havit.Blazor.Components.Web.Bootstrap/Forms/SearchBox/HxSearchBox.razor.cs @@ -500,12 +500,7 @@ private async Task HandleInputFocus() await UpdateSuggestionsAsync(); } - if (!clickIsComing) - { - // clickIsComing logic fixes #572 - Initial suggestions disappear when the DataProvider response is quick - // If click is coming, we do not want to show the dropdown as it will be toggled by the later click event (if we open it here, onfocus, click will hide it) - await ShowDropdownMenu(); - } + await ShowDropdownMenu(); } private void HandleInputBlur() @@ -586,7 +581,12 @@ private void HandleDropdownMenuHidden() private async Task ShowDropdownMenu() { - await dropdownToggle.ShowAsync(); + if (!clickIsComing) + { + // clickIsComing logic fixes #572 - Initial suggestions disappear when the DataProvider response is quick + // If click is coming, we do not want to show the dropdown as it will be toggled by the later click event (if we open it here, onfocus, click will hide it) + await dropdownToggle.ShowAsync(); + } } private async Task HideDropdownMenu()