Skip to content

Commit fd89544

Browse files
committed
Update AutoComplete.razor.cs
re-apply fix, but use main branch as reference
1 parent abb3027 commit fd89544

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,21 @@ public partial class AutoComplete
109109
protected override void OnInitialized()
110110
{
111111
base.OnInitialized();
112-
SkipRegisterEnterEscJSInvoke = true; // Keep original base class interaction
112+
SkipRegisterEnterEscJSInvoke = true;
113+
114+
Items ??= []; // Ensure Items is initialized (from main)
115+
116+
// ++ ADDED: Initial filtering logic from main branch ++
117+
// If a Value is provided initially, pre-filter the dropdown list
118+
if (!string.IsNullOrEmpty(Value))
119+
{
120+
_filterItems = GetFilterItemsByValue(Value); // Use the new helper
121+
if (DisplayCount != null)
122+
{
123+
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
124+
}
125+
}
126+
// ++ END ADDED ++
113127
}
114128

115129
/// <summary>
@@ -124,7 +138,6 @@ protected override void OnParametersSet()
124138
PlaceHolder ??= Localizer[nameof(PlaceHolder)];
125139
Icon ??= IconTheme.GetIconByKey(ComponentIcons.AutoCompleteIcon);
126140
LoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.LoadingIcon);
127-
Items ??= [];
128141

129142
// Note: Logic for handling external Value changes might be needed here
130143
// by comparing previous/current Value and calling JSSetInputValue if changed.
@@ -178,6 +191,18 @@ private async Task OnClickItem(string val)
178191

179192
private List<string> Rows => _filterItems ?? [.. Items];
180193

194+
// ++ ADDED: Helper method from main branch ++
195+
private List<string> GetFilterItemsByValue(string val)
196+
{
197+
var sourceItems = Items ?? Enumerable.Empty<string>(); // Ensure source is not null
198+
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
199+
var items = IsLikeMatch
200+
? sourceItems.Where(s => s != null && s.Contains(val, comparison)) // Add null check
201+
: sourceItems.Where(s => s != null && s.StartsWith(val, comparison)); // Add null check
202+
return [.. items];
203+
}
204+
// ++ END ADDED ++
205+
181206
/// <summary>
182207
/// JSInvokable method called by JavaScript after debouncing input.
183208
/// Updates C# state, performs filtering, and updates dropdown UI.
@@ -201,29 +226,28 @@ public async Task PerformFilteringAndCommitValue(string val)
201226
if (IsNeedValidate && FieldIdentifier != null) EditContext?.NotifyFieldChanged(FieldIdentifier.Value);
202227
}
203228

204-
// Original filtering logic
229+
// -- MODIFIED: Filtering logic now uses the helper method --
205230
if (OnCustomFilter != null)
206231
{
207232
var items = await OnCustomFilter(val);
208-
_filterItems = [.. items];
233+
_filterItems = [.. (items ?? Enumerable.Empty<string>())]; // Handle null result from custom filter
209234
}
210235
else if (string.IsNullOrEmpty(val))
211236
{
212-
_filterItems = [.. Items];
237+
// Use full list if input is empty, don't filter by empty string
238+
_filterItems = Items?.ToList() ?? [];
213239
}
214240
else
215241
{
216-
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
217-
var items = IsLikeMatch
218-
? Items.Where(s => s.Contains(val, comparison))
219-
: Items.Where(s => s.StartsWith(val, comparison));
220-
_filterItems = [.. items];
242+
// Use the helper method for standard filtering
243+
_filterItems = GetFilterItemsByValue(val);
221244
}
222245

223246
if (DisplayCount != null)
224247
{
225248
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
226249
}
250+
// -- END MODIFIED --
227251

228252
// Update dropdown UI using targeted render (original approach)
229253
_shouldRender = false;

0 commit comments

Comments
 (0)