Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(AutoComplete): improved the TriggerChange/TriggerFilter method to update CurrentValue #5531

Merged
merged 3 commits into from
Mar 6, 2025
Merged
Changes from 1 commit
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
26 changes: 21 additions & 5 deletions src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ private async Task OnClickItem(string val)
[JSInvokable]
public override async Task TriggerFilter(string val)
{
// Store the current input value to prevent it from being overwritten
var currentInputValue = val;

if (OnCustomFilter != null)
{
var items = await OnCustomFilter(val);
Expand All @@ -152,12 +155,20 @@ public override async Task TriggerFilter(string val)
: Items.Where(s => s.StartsWith(val, comparison));
_filterItems = [.. items];
}

if (DisplayCount != null)
{
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
}
await TriggerChange(val);

// Use currentInputValue here instead of potentially stale val
CurrentValue = currentInputValue;

// Only trigger StateHasChanged if no binding is present
if (!ValueChanged.HasDelegate)
{
StateHasChanged();
}
}

/// <summary>
Expand All @@ -167,10 +178,15 @@ public override async Task TriggerFilter(string val)
[JSInvokable]
public override Task TriggerChange(string val)
{
CurrentValue = val;
if (!ValueChanged.HasDelegate)
// Only update CurrentValue if the value has actually changed
// This prevents overwriting the user's input
if (CurrentValue != val)
{
StateHasChanged();
CurrentValue = val;
if (!ValueChanged.HasDelegate)
{
StateHasChanged();
}
}
return Task.CompletedTask;
}
Expand Down