Skip to content

Commit e2f0e3e

Browse files
committed
Update AutoComplete.razor.cs
fixes the same problem I was having in issue #5110
1 parent 5ddb33d commit e2f0e3e

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

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

+21-5
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ private async Task OnClickItem(string val)
135135
[JSInvokable]
136136
public override async Task TriggerFilter(string val)
137137
{
138+
// Store the current input value to prevent it from being overwritten
139+
var currentInputValue = val;
140+
138141
if (OnCustomFilter != null)
139142
{
140143
var items = await OnCustomFilter(val);
@@ -152,12 +155,20 @@ public override async Task TriggerFilter(string val)
152155
: Items.Where(s => s.StartsWith(val, comparison));
153156
_filterItems = [.. items];
154157
}
155-
158+
156159
if (DisplayCount != null)
157160
{
158161
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
159162
}
160-
await TriggerChange(val);
163+
164+
// Use currentInputValue here instead of potentially stale val
165+
CurrentValue = currentInputValue;
166+
167+
// Only trigger StateHasChanged if no binding is present
168+
if (!ValueChanged.HasDelegate)
169+
{
170+
StateHasChanged();
171+
}
161172
}
162173

163174
/// <summary>
@@ -167,10 +178,15 @@ public override async Task TriggerFilter(string val)
167178
[JSInvokable]
168179
public override Task TriggerChange(string val)
169180
{
170-
CurrentValue = val;
171-
if (!ValueChanged.HasDelegate)
181+
// Only update CurrentValue if the value has actually changed
182+
// This prevents overwriting the user's input
183+
if (CurrentValue != val)
172184
{
173-
StateHasChanged();
185+
CurrentValue = val;
186+
if (!ValueChanged.HasDelegate)
187+
{
188+
StateHasChanged();
189+
}
174190
}
175191
return Task.CompletedTask;
176192
}

0 commit comments

Comments
 (0)