Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.5.6-beta04</Version>
<Version>9.5.6-beta05</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
28 changes: 21 additions & 7 deletions src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ protected override void OnInitialized()
base.OnInitialized();

SkipRegisterEnterEscJSInvoke = true;

Items ??= [];

if (!string.IsNullOrEmpty(Value))
{
_filterItems = GetFilterItemsByValue(Value);
if (DisplayCount != null)
{
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
}
}
}

/// <summary>
Expand All @@ -106,8 +117,6 @@ protected override void OnParametersSet()
PlaceHolder ??= Localizer[nameof(PlaceHolder)];
Icon ??= IconTheme.GetIconByKey(ComponentIcons.AutoCompleteIcon);
LoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.LoadingIcon);

Items ??= [];
}

private bool _render = true;
Expand Down Expand Up @@ -151,11 +160,7 @@ public override async Task TriggerFilter(string val)
}
else
{
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
var items = IsLikeMatch
? Items.Where(s => s.Contains(val, comparison))
: Items.Where(s => s.StartsWith(val, comparison));
_filterItems = [.. items];
_filterItems = GetFilterItemsByValue(val);
}

if (DisplayCount != null)
Expand All @@ -166,6 +171,15 @@ public override async Task TriggerFilter(string val)
await TriggerChange(val);
}

private List<string> GetFilterItemsByValue(string val)
{
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
var items = IsLikeMatch
? Items.Where(s => s.Contains(val, comparison))
: Items.Where(s => s.StartsWith(val, comparison));
return [.. items];
}

/// <summary>
/// TriggerChange method
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions test/UnitTest/Components/AutoCompleteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ public async Task Items_Ok()
Assert.Equal("Test", cut.Instance.Value);
}

[Fact]
public void Value_Ok()
{
var cut = Context.RenderComponent<AutoComplete>(pb =>
{
pb.Add(a => a.Items, new List<string>() { "test1", "test12", "test123", "test1234" });
pb.Add(a => a.Value, "test12");
pb.Add(a => a.DisplayCount, 2);
});
var menus = cut.FindAll(".dropdown-item");

// 由于 Value = test12
// 并且设置了 DisplayCount = 2
// 候选项只有 2 个
Assert.Equal(2, menus.Count);
}

[Fact]
public void Debounce_Ok()
{
Expand Down