Skip to content

Commit 5e8a2eb

Browse files
committed
feat: 增加 IsClearable 支持
1 parent 3125b61 commit 5e8a2eb

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement" />
1919
<span class="form-select-append"><i class="@Icon"></i></span>
2020
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
21+
@if (GetClearable())
22+
{
23+
<span class="@ClearClassString" @onclick="OnClearValue"><i class="@ClearIcon"></i></span>
24+
}
2125
<ul class="dropdown-menu">
2226
@if (IsVirtualize)
2327
{

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public partial class AutoFill<TValue>
1818
/// Gets the component style.
1919
/// </summary>
2020
private string? ClassString => CssBuilder.Default("auto-complete auto-fill")
21+
.AddClass("is-clearable", IsClearable)
2122
.AddClassFromAttributes(AdditionalAttributes)
2223
.Build();
2324

@@ -125,6 +126,19 @@ public partial class AutoFill<TValue>
125126
[Parameter]
126127
public bool IsClearable { get; set; }
127128

129+
/// <summary>
130+
/// Gets or sets the right-side clear icon. Default is fa-solid fa-angle-up.
131+
/// </summary>
132+
[Parameter]
133+
[NotNull]
134+
public string? ClearIcon { get; set; }
135+
136+
/// <summary>
137+
/// Gets or sets the callback method when the clear button is clicked. Default is null.
138+
/// </summary>
139+
[Parameter]
140+
public Func<Task>? OnClearAsync { get; set; }
141+
128142
[Inject]
129143
[NotNull]
130144
private IStringLocalizer<AutoComplete>? Localizer { get; set; }
@@ -138,6 +152,15 @@ public partial class AutoFill<TValue>
138152
[NotNull]
139153
private Virtualize<TValue>? _virtualizeElement = default;
140154

155+
/// <summary>
156+
/// Gets the clear icon class string.
157+
/// </summary>
158+
private string? ClearClassString => CssBuilder.Default("clear-icon")
159+
.AddClass($"text-{Color.ToDescriptionString()}", Color != Color.None)
160+
.AddClass($"text-success", IsValid.HasValue && IsValid.Value)
161+
.AddClass($"text-danger", IsValid.HasValue && !IsValid.Value)
162+
.Build();
163+
141164
/// <summary>
142165
/// <inheritdoc/>
143166
/// </summary>
@@ -149,11 +172,37 @@ protected override void OnParametersSet()
149172
PlaceHolder ??= Localizer[nameof(PlaceHolder)];
150173
Icon ??= IconTheme.GetIconByKey(ComponentIcons.AutoFillIcon);
151174
LoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.LoadingIcon);
175+
ClearIcon ??= IconTheme.GetIconByKey(ComponentIcons.SelectClearIcon);
152176

153177
_displayText = GetDisplayText(Value);
154178
Items ??= [];
155179
}
156180

181+
private bool IsNullable() => !ValueType.IsValueType || NullableUnderlyingType != null;
182+
183+
/// <summary>
184+
/// Gets whether show the clear button.
185+
/// </summary>
186+
/// <returns></returns>
187+
private bool GetClearable() => IsClearable && !IsDisabled && IsNullable();
188+
189+
/// <summary>
190+
/// <inheritdoc/>
191+
/// </summary>
192+
/// <returns></returns>
193+
private async Task OnClearValue()
194+
{
195+
if (OnClearAsync != null)
196+
{
197+
await OnClearAsync();
198+
}
199+
CurrentValue = default;
200+
if (OnQueryAsync != null)
201+
{
202+
await _virtualizeElement.RefreshDataAsync();
203+
}
204+
}
205+
157206
/// <summary>
158207
/// Callback method when a candidate item is clicked.
159208
/// </summary>

0 commit comments

Comments
 (0)