Skip to content

Commit 13473b2

Browse files
authored
feat(AutoFill): reuse AutoComplete clear-icon function (#6763)
* refactor: 清空按钮复用 AutoComplete 逻辑 * test: 更新单元测试 * chore: bump version 9.10.2
1 parent 61e7c08 commit 13473b2

File tree

4 files changed

+24
-38
lines changed

4 files changed

+24
-38
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.10.1</Version>
4+
<Version>9.10.2</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
2020
@if (GetClearable())
2121
{
22-
<span class="@ClearClassString" @onclick="OnClearValue"><i class="@ClearIcon"></i></span>
22+
<span class="@ClearClassString"><i class="@ClearIcon"></i></span>
2323
}
2424
<RenderTemplate @ref="_dropdown">
2525
@RenderDropdown

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ namespace BootstrapBlazor.Components;
1414
/// <typeparam name="TValue">The type of the value.</typeparam>
1515
public partial class AutoFill<TValue>
1616
{
17-
/// <summary>
18-
/// Gets the component style.
19-
/// </summary>
20-
private string? ClassString => CssBuilder.Default("auto-complete auto-fill")
21-
.AddClass("is-clearable", IsClearable)
22-
.Build();
23-
2417
/// <summary>
2518
/// Gets or sets the collection of items for the component.
2619
/// </summary>
@@ -141,6 +134,13 @@ public partial class AutoFill<TValue>
141134
[NotNull]
142135
private RenderTemplate? _dropdown = null;
143136

137+
/// <summary>
138+
/// Gets the component style.
139+
/// </summary>
140+
private string? ClassString => CssBuilder.Default("auto-complete auto-fill")
141+
.AddClass("is-clearable", IsClearable)
142+
.Build();
143+
144144
/// <summary>
145145
/// Gets the clear icon class string.
146146
/// </summary>
@@ -176,7 +176,6 @@ protected override void OnParametersSet()
176176
/// <returns></returns>
177177
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, _displayText);
178178

179-
180179
private bool IsNullable() => !ValueType.IsValueType || NullableUnderlyingType != null;
181180

182181
/// <summary>
@@ -185,30 +184,6 @@ protected override void OnParametersSet()
185184
/// <returns></returns>
186185
private bool GetClearable() => IsClearable && !IsDisabled && IsNullable();
187186

188-
/// <summary>
189-
/// <inheritdoc/>
190-
/// </summary>
191-
/// <returns></returns>
192-
private async Task OnClearValue()
193-
{
194-
// 使用脚本更新 input 值
195-
await InvokeVoidAsync("setValue", Id, "");
196-
197-
if (OnClearAsync != null)
198-
{
199-
await OnClearAsync();
200-
}
201-
CurrentValue = default;
202-
_displayText = null;
203-
_filterItems = null;
204-
_searchText = null;
205-
206-
if (OnQueryAsync != null)
207-
{
208-
await _virtualizeElement.RefreshDataAsync();
209-
}
210-
}
211-
212187
/// <summary>
213188
/// Callback method when a candidate item is clicked.
214189
/// </summary>
@@ -255,6 +230,18 @@ private async ValueTask<ItemsProviderResult<TValue>> LoadItems(ItemsProviderRequ
255230
[JSInvokable]
256231
public async Task TriggerFilter(string val)
257232
{
233+
if (string.IsNullOrEmpty(val))
234+
{
235+
CurrentValue = default;
236+
_filterItems = null;
237+
_displayText = null;
238+
239+
if (OnClearAsync != null)
240+
{
241+
await OnClearAsync();
242+
}
243+
}
244+
258245
if (OnQueryAsync != null)
259246
{
260247
_searchText = val;

test/UnitTest/Components/AutoFillTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,9 @@ public async Task IsVirtualize_OnQueryAsync_Clearable_Ok()
425425
Assert.Equal("2", searchText);
426426
Assert.Contains("<div>test2</div>", cut.Markup);
427427

428+
// 测试 Clear 清空逻辑
428429
query = false;
429-
// 点击 Clear 按钮
430-
var button = cut.Find(".clear-icon");
431-
await cut.InvokeAsync(() => button.Click());
430+
await cut.InvokeAsync(() => cut.Instance.TriggerFilter(""));
432431

433432
Assert.True(query);
434433
Assert.True(cleared);
@@ -445,7 +444,7 @@ public async Task IsVirtualize_OnQueryAsync_Clearable_Ok()
445444
});
446445
});
447446
});
448-
await cut.InvokeAsync(() => button.Click());
447+
await cut.InvokeAsync(() => cut.Instance.TriggerFilter(""));
449448
}
450449

451450
[Fact]

0 commit comments

Comments
 (0)