Skip to content

Commit

Permalink
Move KeybaordNavigation to a dedicated region #348
Browse files Browse the repository at this point in the history
  • Loading branch information
Harvey1214 committed Sep 5, 2022
1 parent c206025 commit 7cdcf99
Showing 1 changed file with 31 additions and 32 deletions.
Expand Up @@ -11,12 +11,6 @@ namespace Havit.Blazor.Components.Web.Bootstrap;
/// <typeparam name="TItem"></typeparam>
public partial class HxSearchBox<TItem> : IAsyncDisposable
{
protected const string ArrowUpKeyCode = "ArrowUp";
protected const string ArrowDownKeyCode = "ArrowDown";

protected const string EnterKeyCode = "Enter";
protected const string NumpadEnterKeyCode = "NumpadEnter";

/// <summary>
/// Returns application-wide defaults for the component.
/// Enables overriding defaults in descandants (use separate set of defaults).
Expand Down Expand Up @@ -282,7 +276,8 @@ protected async Task UpdateSuggestionsAsync()

dataProviderInProgress = false;

focusedItemIndex = default;
focusedItemIndex = default; // KeyboardNavigation

searchResults = result?.Data.ToList();

textQueryHasBeenBelowMinimumLength = false;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
}
}
}

Expand All @@ -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)
{
Expand Down

0 comments on commit 7cdcf99

Please sign in to comment.