Skip to content

Commit c7bd3a2

Browse files
committed
refactor: 清空按钮复用 AutoComplete 逻辑
1 parent 61e7c08 commit c7bd3a2

File tree

2 files changed

+20
-33
lines changed

2 files changed

+20
-33
lines changed

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;

0 commit comments

Comments
 (0)