From 7cdcf9945ca992b480a32d150f1ba5c8cbfd0d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikul=C3=A1=C5=A1=20Hobl=C3=ADk?= Date: Mon, 5 Sep 2022 19:50:07 +0200 Subject: [PATCH] Move KeybaordNavigation to a dedicated region #348 --- .../Forms/SearchBox/HxSearchBox.razor.cs | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) 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 787728381..8a064c45c 100644 --- a/Havit.Blazor.Components.Web.Bootstrap/Forms/SearchBox/HxSearchBox.razor.cs +++ b/Havit.Blazor.Components.Web.Bootstrap/Forms/SearchBox/HxSearchBox.razor.cs @@ -11,12 +11,6 @@ namespace Havit.Blazor.Components.Web.Bootstrap; /// public partial class HxSearchBox : IAsyncDisposable { - protected const string ArrowUpKeyCode = "ArrowUp"; - protected const string ArrowDownKeyCode = "ArrowDown"; - - protected const string EnterKeyCode = "Enter"; - protected const string NumpadEnterKeyCode = "NumpadEnter"; - /// /// Returns application-wide defaults for the component. /// Enables overriding defaults in descandants (use separate set of defaults). @@ -282,7 +276,8 @@ protected async Task UpdateSuggestionsAsync() dataProviderInProgress = false; - focusedItemIndex = default; + focusedItemIndex = default; // KeyboardNavigation + searchResults = result?.Data.ToList(); textQueryHasBeenBelowMinimumLength = false; @@ -291,25 +286,6 @@ protected async Task UpdateSuggestionsAsync() StateHasChanged(); } - protected bool HasItemFocus(TItem item) - { - TItem focusedItem = GetItemByIndex(focusedItemIndex); - - if ((focusedItem is not null) && (!focusedItem.Equals(default))) - { - return item.Equals(focusedItem); - } - else - { - return false; - } - } - - protected bool HasFreeTextItemFocus() - { - return focusedItemIndex == GetFreeTextItemIndex(); - } - protected async Task HandleTextQueryValueChanged(string newTextQuery) { this.TextQuery = newTextQuery; @@ -346,6 +322,32 @@ protected async Task HandleTextQueryValueChanged(string newTextQuery) await InvokeTextQueryChangedAsync(newTextQuery); } + #region KeyboardNavigation + private const string ArrowUpKeyCode = "ArrowUp"; + private const string ArrowDownKeyCode = "ArrowDown"; + + private const string EnterKeyCode = "Enter"; + private const string NumpadEnterKeyCode = "NumpadEnter"; + + private bool HasItemFocus(TItem item) + { + TItem focusedItem = GetItemByIndex(focusedItemIndex); + + if ((focusedItem is not null) && (!focusedItem.Equals(default))) + { + return item.Equals(focusedItem); + } + else + { + return false; + } + } + + private bool HasFreeTextItemFocus() + { + return focusedItemIndex == GetFreeTextItemIndex(); + } + private async Task UpdateFocusedItem(KeyboardEventArgs keyboardEventArgs) { // Confirm selection on the focused item if an item is focused and the enter key is pressed. @@ -374,14 +376,10 @@ private async Task UpdateFocusedItem(KeyboardEventArgs keyboardEventArgs) else if (keyboardEventArgs.Code == ArrowDownKeyCode) { int nextItemIndex = focusedItemIndex + 1; - if (nextItemIndex < searchResults.Count) + if (nextItemIndex < GetFreeTextItemIndex()) // If the index equals GetFreeTextItemIndex(), then the freetext item is selected. { focusedItemIndex = nextItemIndex; } - else - { - focusedItemIndex = GetFreeTextItemIndex(); // Select the item that's used to confirm the freetext. - } } } @@ -397,10 +395,11 @@ private TItem GetItemByIndex(int index) } } - protected int GetFreeTextItemIndex() + private int GetFreeTextItemIndex() { return searchResults.Count; } + #endregion KeyboardNavigation private async void HandleTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) {