Skip to content

Commit

Permalink
fix(AutoFill): should not trigger OnCustomFilter callback when ArrowU…
Browse files Browse the repository at this point in the history
…p/Down/Esc/Enter key up (#4308)
  • Loading branch information
ArgoZhang authored Sep 15, 2024
1 parent f97a27f commit 9df814e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
41 changes: 21 additions & 20 deletions src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ private async Task OnFocus(FocusEventArgs args)
}
}

private static readonly List<string> HandlerKeys = ["ArrowUp", "ArrowDown", "Escape", "Enter"];

/// <summary>
/// OnKeyUp 方法
/// </summary>
Expand All @@ -186,23 +188,30 @@ private async Task OnFocus(FocusEventArgs args)
[JSInvokable]
public virtual async Task OnKeyUp(string key)
{
if (!_isLoading)
if (!HandlerKeys.Contains(key))
{
_isLoading = true;
if (OnCustomFilter != null)
{
var items = await OnCustomFilter(_inputString);
_filterItems = items.ToList();
}
else
// 非功能按键时触发过滤
if (!_isLoading)
{
var items = FindItem();
_filterItems = DisplayCount == null ? items.ToList() : items.Take(DisplayCount.Value).ToList();
_isLoading = true;
if (OnCustomFilter != null)
{
var items = await OnCustomFilter(_inputString);
_filterItems = items.ToList();
}
else
{
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
var items = IsLikeMatch ?
Items.Where(s => OnGetDisplayText(s).Contains(_inputString, comparison)) :
Items.Where(s => OnGetDisplayText(s).StartsWith(_inputString, comparison));
_filterItems = DisplayCount == null ? items.ToList() : items.Take(DisplayCount.Value).ToList();
}
_isLoading = false;
}
_isLoading = false;
}

if (_filterItems.Any())
if (_filterItems.Count > 0)
{
_isShown = true;
// 键盘向上选择
Expand Down Expand Up @@ -257,14 +266,6 @@ public virtual async Task OnKeyUp(string key)
}
}
StateHasChanged();

IEnumerable<TValue> FindItem()
{
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
return IsLikeMatch ?
Items.Where(s => OnGetDisplayText(s).Contains(_inputString, comparison)) :
Items.Where(s => OnGetDisplayText(s).StartsWith(_inputString, comparison));
}
}

/// <summary>
Expand Down
13 changes: 11 additions & 2 deletions test/UnitTest/Components/AutoFillTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ public async Task ShowDropdownListOnFocus_Ok()
}

[Fact]
public void ValidateForm_Ok()
public async Task ValidateForm_Ok()
{
var v = "";
var trigger = false;
IEnumerable<string> items = new List<string>() { "test1", "test2" };
var cut = Context.RenderComponent<ValidateForm>(pb =>
{
Expand All @@ -276,6 +277,7 @@ public void ValidateForm_Ok()
pb.Add(a => a.OnCustomFilter, key =>
{
v = key;
trigger = true;
return Task.FromResult(items);
});
});
Expand All @@ -284,8 +286,15 @@ public void ValidateForm_Ok()
// Trigger js invoke
var comp = cut.FindComponent<AutoFill<string>>().Instance;
comp.TriggerOnChange("v");
cut.InvokeAsync(() => comp.OnKeyUp("Enter"));
await cut.InvokeAsync(() => comp.OnKeyUp("v"));
Assert.Equal("v", v);
Assert.True(trigger);

// not trigger OnKeyUp
v = "";
trigger = false;
await cut.InvokeAsync(() => comp.OnKeyUp("Enter"));
Assert.False(trigger);
}

class AutoFillNullStringMock
Expand Down

0 comments on commit 9df814e

Please sign in to comment.