Skip to content

Commit c7f5a33

Browse files
committed
refactor: 增加 Value 初始化时过滤候选项功能
1 parent 1c6d6ae commit c7f5a33

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

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>

0 commit comments

Comments
 (0)