Skip to content

fix(AutoComplete): missing value when click delete item #5814

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

Merged
merged 6 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
{
<BootstrapLabel required="@Required" for="@InputId" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText"/>
}
<div class="@ClassString" id="@Id">
<div class="auto-complete" id="@Id">
<input @attributes="AdditionalAttributes" id="@InputId" class="@ClassName" autocomplete="off" type="text"
data-bs-toggle="@ToggleString" data-bs-placement="@PlacementString"
data-bs-offset="@OffsetString" data-bs-custom-class="@CustomClassString"
data-bb-auto-dropdown-focus="@ShowDropdownListOnFocusString" data-bb-debounce="@DurationString"
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString" data-bb-blur="@TriggerBlurString"
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString"
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString" data-bb-trigger-delete="true"
value="@CurrentValueAsString"
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement"/>
<span class="form-select-append"><i class="@Icon"></i></span>
Expand Down
22 changes: 14 additions & 8 deletions src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ namespace BootstrapBlazor.Components;
/// </summary>
public partial class AutoComplete
{
/// <summary>
/// Gets the component style
/// </summary>
private string? ClassString => CssBuilder.Default("auto-complete")
.Build();

/// <summary>
/// Gets or sets the collection of matching data obtained by inputting a string
/// </summary>
Expand Down Expand Up @@ -179,14 +173,26 @@ public override async Task TriggerFilter(string val)
[JSInvokable]
public override Task TriggerChange(string val)
{
// client input does not need to be re-rendered to prevent jitter when the network is congested
_render = false;
CurrentValue = val;
_render = true;
_dropdown.Render();
return Task.CompletedTask;
}

/// <summary>
/// TriggerChange method
/// </summary>
/// <param name="val"></param>
[JSInvokable]
public Task TriggerDeleteCallback(string val)
{
CurrentValue = val;
if (!ValueChanged.HasDelegate)
{
StateHasChanged();
}
_render = true;
_dropdown.Render();
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ const handlerKeyup = (ac, e) => {
current.classList.add('active');
scrollIntoView(el, current);
}
else if (key === 'Backspace' || key === 'Delete') {
if (input.getAttribute('data-bb-trigger-delete') === 'true') {
invoke.invokeMethodAsync('TriggerDeleteCallback', input.value);
}
}
}

export function showList(id) {
Expand Down
9 changes: 6 additions & 3 deletions test/UnitTest/Components/AutoCompleteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone

using Microsoft.AspNetCore.Components.Web;

namespace UnitTest.Components;

public class AutoCompleteTest : BootstrapBlazorTestBase
{
[Fact]
public void Items_Ok()
public async Task Items_Ok()
{
var cut = Context.RenderComponent<AutoComplete>(pb =>
{
pb.Add(a => a.IsSelectAllTextOnFocus, true);
pb.Add(a => a.IsSelectAllTextOnEnter, true);
});
Assert.Contains("<div class=\"auto-complete\"", cut.Markup);
Assert.Contains("data-bb-trigger-delete=\"true\"", cut.Markup);

var menus = cut.FindAll(".dropdown-item");
Assert.Single(menus);

Expand All @@ -35,6 +35,9 @@ public void Items_Ok()
});
menus = cut.FindAll(".dropdown-item");
Assert.Equal(2, menus.Count);

await cut.InvokeAsync(() => cut.Instance.TriggerDeleteCallback("Test"));
Assert.Equal("Test", cut.Instance.Value);
}

[Fact]
Expand Down