Skip to content

Commit 2e2e2b9

Browse files
authored
fix(AutoComplete): auto filter the dropdown items when Value is not null (#5823)
* refactor: 增加 Value 初始化时过滤候选项功能 * chore: bump version 9.5.6-beta05 * test: 更新单元测试
1 parent 1c6d6ae commit 2e2e2b9

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.5.6-beta04</Version>
4+
<Version>9.5.6-beta05</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ protected override void OnInitialized()
9393
base.OnInitialized();
9494

9595
SkipRegisterEnterEscJSInvoke = true;
96+
97+
Items ??= [];
98+
99+
if (!string.IsNullOrEmpty(Value))
100+
{
101+
_filterItems = GetFilterItemsByValue(Value);
102+
if (DisplayCount != null)
103+
{
104+
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
105+
}
106+
}
96107
}
97108

98109
/// <summary>
@@ -106,8 +117,6 @@ protected override void OnParametersSet()
106117
PlaceHolder ??= Localizer[nameof(PlaceHolder)];
107118
Icon ??= IconTheme.GetIconByKey(ComponentIcons.AutoCompleteIcon);
108119
LoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.LoadingIcon);
109-
110-
Items ??= [];
111120
}
112121

113122
private bool _render = true;
@@ -151,11 +160,7 @@ public override async Task TriggerFilter(string val)
151160
}
152161
else
153162
{
154-
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
155-
var items = IsLikeMatch
156-
? Items.Where(s => s.Contains(val, comparison))
157-
: Items.Where(s => s.StartsWith(val, comparison));
158-
_filterItems = [.. items];
163+
_filterItems = GetFilterItemsByValue(val);
159164
}
160165

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

174+
private List<string> GetFilterItemsByValue(string val)
175+
{
176+
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
177+
var items = IsLikeMatch
178+
? Items.Where(s => s.Contains(val, comparison))
179+
: Items.Where(s => s.StartsWith(val, comparison));
180+
return [.. items];
181+
}
182+
169183
/// <summary>
170184
/// TriggerChange method
171185
/// </summary>

test/UnitTest/Components/AutoCompleteTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ public async Task Items_Ok()
4040
Assert.Equal("Test", cut.Instance.Value);
4141
}
4242

43+
[Fact]
44+
public void Value_Ok()
45+
{
46+
var cut = Context.RenderComponent<AutoComplete>(pb =>
47+
{
48+
pb.Add(a => a.Items, new List<string>() { "test1", "test12", "test123", "test1234" });
49+
pb.Add(a => a.Value, "test12");
50+
pb.Add(a => a.DisplayCount, 2);
51+
});
52+
var menus = cut.FindAll(".dropdown-item");
53+
54+
// 由于 Value = test12
55+
// 并且设置了 DisplayCount = 2
56+
// 候选项只有 2 个
57+
Assert.Equal(2, menus.Count);
58+
}
59+
4360
[Fact]
4461
public void Debounce_Ok()
4562
{

0 commit comments

Comments
 (0)