Skip to content

Commit a213b84

Browse files
authored
fix(AutoComplete): missing value when click delete item (#5814)
* refactor: 精简代码 * refactor: 移除不使用的样式 * refactor: 增加注释 * refactor: 增加 DeleteCallback 方法 * refactor: 更改回调名称 * test: 更新单元测试
1 parent b9e9111 commit a213b84

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
{
66
<BootstrapLabel required="@Required" for="@InputId" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText"/>
77
}
8-
<div class="@ClassString" id="@Id">
8+
<div class="auto-complete" id="@Id">
99
<input @attributes="AdditionalAttributes" id="@InputId" class="@ClassName" autocomplete="off" type="text"
1010
data-bs-toggle="@ToggleString" data-bs-placement="@PlacementString"
1111
data-bs-offset="@OffsetString" data-bs-custom-class="@CustomClassString"
1212
data-bb-auto-dropdown-focus="@ShowDropdownListOnFocusString" data-bb-debounce="@DurationString"
1313
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString" data-bb-blur="@TriggerBlurString"
14-
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString"
14+
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString" data-bb-trigger-delete="true"
1515
value="@CurrentValueAsString"
1616
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement"/>
1717
<span class="form-select-append"><i class="@Icon"></i></span>

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ namespace BootstrapBlazor.Components;
1212
/// </summary>
1313
public partial class AutoComplete
1414
{
15-
/// <summary>
16-
/// Gets the component style
17-
/// </summary>
18-
private string? ClassString => CssBuilder.Default("auto-complete")
19-
.Build();
20-
2115
/// <summary>
2216
/// Gets or sets the collection of matching data obtained by inputting a string
2317
/// </summary>
@@ -179,14 +173,26 @@ public override async Task TriggerFilter(string val)
179173
[JSInvokable]
180174
public override Task TriggerChange(string val)
181175
{
176+
// client input does not need to be re-rendered to prevent jitter when the network is congested
182177
_render = false;
178+
CurrentValue = val;
179+
_render = true;
180+
_dropdown.Render();
181+
return Task.CompletedTask;
182+
}
183+
184+
/// <summary>
185+
/// TriggerChange method
186+
/// </summary>
187+
/// <param name="val"></param>
188+
[JSInvokable]
189+
public Task TriggerDeleteCallback(string val)
190+
{
183191
CurrentValue = val;
184192
if (!ValueChanged.HasDelegate)
185193
{
186194
StateHasChanged();
187195
}
188-
_render = true;
189-
_dropdown.Render();
190196
return Task.CompletedTask;
191197
}
192198
}

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js

+5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ const handlerKeyup = (ac, e) => {
157157
current.classList.add('active');
158158
scrollIntoView(el, current);
159159
}
160+
else if (key === 'Backspace' || key === 'Delete') {
161+
if (input.getAttribute('data-bb-trigger-delete') === 'true') {
162+
invoke.invokeMethodAsync('TriggerDeleteCallback', input.value);
163+
}
164+
}
160165
}
161166

162167
export function showList(id) {

test/UnitTest/Components/AutoCompleteTest.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
55

6-
using Microsoft.AspNetCore.Components.Web;
7-
86
namespace UnitTest.Components;
97

108
public class AutoCompleteTest : BootstrapBlazorTestBase
119
{
1210
[Fact]
13-
public void Items_Ok()
11+
public async Task Items_Ok()
1412
{
1513
var cut = Context.RenderComponent<AutoComplete>(pb =>
1614
{
1715
pb.Add(a => a.IsSelectAllTextOnFocus, true);
1816
pb.Add(a => a.IsSelectAllTextOnEnter, true);
1917
});
2018
Assert.Contains("<div class=\"auto-complete\"", cut.Markup);
19+
Assert.Contains("data-bb-trigger-delete=\"true\"", cut.Markup);
20+
2121
var menus = cut.FindAll(".dropdown-item");
2222
Assert.Single(menus);
2323

@@ -35,6 +35,9 @@ public void Items_Ok()
3535
});
3636
menus = cut.FindAll(".dropdown-item");
3737
Assert.Equal(2, menus.Count);
38+
39+
await cut.InvokeAsync(() => cut.Instance.TriggerDeleteCallback("Test"));
40+
Assert.Equal("Test", cut.Instance.Value);
3841
}
3942

4043
[Fact]

0 commit comments

Comments
 (0)