Skip to content

Commit

Permalink
Bugfix: ArgumentOutOfRangeException
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomodo committed Oct 20, 2023
1 parent f8039bf commit 83dd5dd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ public async Task Combobox_MultiSelectEditable()
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
// now click an item and see the value change
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item").Count.Should().Be(4));
comp.WaitForAssertion(() => combobox.Instance.GetEligibleAndNonDisabledItems().Count.Should().Be(4));
comp.WaitForAssertion(() => combobox.Instance.GetEnabledAndEligibleItems().Count.Should().Be(4));
var items = comp.FindAll("div.mud-combobox-item").ToArray();
await comp.InvokeAsync(() => combobox.Instance.HandleInternalValueChanged("t"));
await comp.InvokeAsync(() => combobox.Instance.ForceRenderItems());
items = comp.FindAll("div.mud-combobox-item").ToArray();
comp.WaitForAssertion(() => combobox.Instance.GetEligibleAndNonDisabledItems().Count.Should().Be(2));
comp.WaitForAssertion(() => combobox.Instance.GetEnabledAndEligibleItems().Count.Should().Be(2));
}

/// <summary>
Expand Down
107 changes: 51 additions & 56 deletions CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ protected internal async void HandleKeyDown(KeyboardEventArgs obj)
var key = obj.Key.ToLowerInvariant();
if (Editable == false && key.Length == 1 && key != " " && !(obj.CtrlKey || obj.ShiftKey || obj.AltKey || obj.MetaKey))
{
await ActiveFirstItem(key);
await ActivateFirstItem(key);
return;
}

Expand All @@ -782,10 +782,10 @@ protected internal async void HandleKeyDown(KeyboardEventArgs obj)
}
break;
case "Home":
await ActiveFirstItem();
await ActivateFirstItem();
break;
case "End":
await ActiveLastItem();
await ActivateLastItem();
break;
case "ArrowUp":
if (obj.AltKey)
Expand All @@ -798,7 +798,7 @@ protected internal async void HandleKeyDown(KeyboardEventArgs obj)
}
else
{
await ActiveAdjacentItem(-1);
await ActivateAdjacentItem(-1);
}
break;
case "ArrowDown":
Expand All @@ -812,7 +812,7 @@ protected internal async void HandleKeyDown(KeyboardEventArgs obj)
}
else
{
await ActiveAdjacentItem(1);
await ActivateAdjacentItem(1);
}
break;
case " ":
Expand Down Expand Up @@ -875,7 +875,7 @@ protected internal async Task SearchBoxHandleKeyDown(KeyboardEventArgs obj)
HandleKeyDown(obj);
break;
case "Tab":
await ActiveFirstItem();
await ActivateFirstItem();
await FocusAsync();
StateHasChanged();
break;
Expand Down Expand Up @@ -1263,7 +1263,7 @@ protected internal async Task ForceUpdateItems()

protected int GetActiveProperItemIndex()
{
var properItems = GetEligibleAndNonDisabledItems();
var properItems = GetEnabledAndEligibleItems();
if (properItems.Any())
{
if (_lastActivatedItem == null)
Expand Down Expand Up @@ -1306,72 +1306,73 @@ protected void DeactiveAllItems()
}
}

public async Task ActiveFirstItem(string startChar = null)
public async Task ActivateFirstItem(string startsWith = null)
{
if (Items == null || Items.Count == 0 || Items[0].Disabled)
{
var item = Items.FirstOrDefault();
if (item is null || item.Disabled)
return;
}

DeactiveAllItems();
var properItems = GetEligibleAndNonDisabledItems();
if (string.IsNullOrWhiteSpace(startChar))
if (string.IsNullOrWhiteSpace(startsWith))
{
properItems[0].SetActive(true);
_lastActivatedItem = properItems[0];
await ScrollToMiddleAsync(Items[0]);
item = GetEnabledAndEligibleItems().FirstOrDefault();
if (item is not null)
{
item.SetActive(true);
await ScrollToMiddleAsync(item);
}
_lastActivatedItem = item;
return;
}

if (Editable == true)
{
if (Editable)
return;
}

// find first item that starts with the letter
var possibleItems = Items.Where(x => (x.Text ?? Converter.Set(x.Value) ?? string.Empty).StartsWith(startChar, StringComparison.OrdinalIgnoreCase)).ToList();
if (possibleItems == null || !possibleItems.Any())
var foundItems = Items.Where(x => (x.Text ?? Converter.Set(x.Value) ?? string.Empty).StartsWith(startsWith, StringComparison.OrdinalIgnoreCase)).ToList();
if (!foundItems.Any())
{
if (_lastActivatedItem == null)
if (_lastActivatedItem is not null)
{
return;
_lastActivatedItem.SetActive(true);
await ScrollToMiddleAsync(_lastActivatedItem);
}
_lastActivatedItem.SetActive(true);
await ScrollToMiddleAsync(_lastActivatedItem);
return;
}

var theItem = possibleItems.FirstOrDefault(x => x == _lastActivatedItem);
if (theItem == null)
item = foundItems.FirstOrDefault(x => x == _lastActivatedItem);
if (item is null)
{
possibleItems[0].SetActive(true);
_lastActivatedItem = possibleItems[0];
await ScrollToMiddleAsync(possibleItems[0]);
item = foundItems[0];
item.SetActive(true);
await ScrollToMiddleAsync(item);
_lastActivatedItem = item;
return;
}

if (theItem == possibleItems.LastOrDefault())
if (item == foundItems.LastOrDefault())
{
possibleItems[0].SetActive(true);
_lastActivatedItem = possibleItems[0];
await ScrollToMiddleAsync(possibleItems[0]);
item = foundItems[0];
item.SetActive(true);
await ScrollToMiddleAsync(item);
_lastActivatedItem = item;
}
else
{
var item = possibleItems[possibleItems.IndexOf(theItem) + 1];
item = foundItems[foundItems.IndexOf(item) + 1];
item.SetActive(true);
_lastActivatedItem = item;
await ScrollToMiddleAsync(item);
_lastActivatedItem = item;
}
}

public async Task ActiveAdjacentItem(int changeCount)
public async Task ActivateAdjacentItem(int changeCount)
{
if (Items == null || Items.Count == 0)
{
return;
}

var properItems = GetEligibleAndNonDisabledItems();
var properItems = GetEnabledAndEligibleItems();
int index = GetActiveProperItemIndex();
if (index + changeCount >= properItems.Count || 0 > index + changeCount)
{
Expand All @@ -1385,31 +1386,25 @@ public async Task ActiveAdjacentItem(int changeCount)
await ScrollToMiddleAsync(Items[index + changeCount]);
}

public async Task ActiveLastItem()
public async Task ActivateLastItem()
{
if (!(Items.Count > 0))
return;

DeactiveAllItems();
var properItems = GetEligibleAndNonDisabledItems();
var lastItem = properItems.LastOrDefault();
lastItem?.SetActive(true);
_lastActivatedItem = lastItem;

if (_lastActivatedItem is not null)
await ScrollToMiddleAsync(_lastActivatedItem);
}

#endregion

protected internal List<MudComboBoxItem<T>> GetEligibleAndNonDisabledItems()
{
if (Items == null)
var item = GetEnabledAndEligibleItems().LastOrDefault();
if (item is not null)
{
return new();
item.SetActive(true);
await ScrollToMiddleAsync(item);
}
return Items.Where(x => x.Eligible == true && x.Disabled == false).ToList();
_lastActivatedItem = item;
}

#endregion

protected internal List<MudComboBoxItem<T>> GetEnabledAndEligibleItems() => Items.Where(x => !x.Disabled && x.Eligible).ToList();


protected bool HasEligibleItems()
{
Expand Down

0 comments on commit 83dd5dd

Please sign in to comment.