Skip to content
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 @@ -37,7 +37,7 @@
<section ignore>@((MarkupString)Localizer["ShowLabelDescription"].Value)</section>
<Divider Text="@Localizer["Divider1Text"]" Alignment="Alignment.Left" style="margin: 2rem 0;"></Divider>
<ValidateForm Model="@Model">
<AutoComplete Items="@StaticItems" @bind-Value="@Model.Name" ShowLabel="true" />
<AutoComplete Items="@StaticItems" @bind-Value="@Model.Name" ShowLabel="true" IsClearable="true" />
</ValidateForm>
<Divider Text="@Localizer["Divider2Text"]" Alignment="Alignment.Left" style="margin: 2rem 0;" />
<AutoComplete Items="@StaticItems" @bind-Value="@Model.Name" ShowLabel="false" />
Expand Down
17 changes: 14 additions & 3 deletions src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ private async Task OnClickItem(string val)

private List<string> Rows => _filterItems ?? [.. Items];

/// <summary>
/// 点击清空按钮时调用此方法 由 Javascript 触发
/// </summary>
/// <returns></returns>
[JSInvokable]
public async Task TriggerClear()
{
await TriggerFilter("");

_clientValue = null;
CurrentValueAsString = string.Empty;
}

/// <summary>
/// TriggerFilter method
/// </summary>
Expand Down Expand Up @@ -228,12 +241,10 @@ public async Task TriggerFilter(string val)
/// <param name="v"></param>
/// <returns></returns>
[JSInvokable]
public Task TriggerChange(string v)
public void TriggerChange(string v)
{
_clientValue = v;
CurrentValueAsString = v;

return Task.CompletedTask;
}

private List<string> GetFilterItemsByValue(string val)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function init(id, invoke, value, changedEventCallback) {

EventHandler.on(el, 'click', '.clear-icon', e => {
input.value = '';
invoke.invokeMethodAsync('TriggerFilter', '');
invoke.invokeMethodAsync('TriggerClear');
});
}

Expand Down
21 changes: 21 additions & 0 deletions test/UnitTest/Components/AutoCompleteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,27 @@ public async Task IgnoreCase_Ok()
Assert.Equal(2, menus.Count);
}

[Fact]
public async Task TriggerClear_Ok()
{
var val = "task1";
var items = new List<string>() { "task1", "Task2" };
var cut = Context.RenderComponent<AutoComplete>(builder =>
{
builder.Add(a => a.Items, items);
builder.Add(a => a.IgnoreCase, false);
builder.Add(a => a.Value, val);
builder.Add(a => a.IsClearable, true);
builder.Add(a => a.ValueChanged, EventCallback.Factory.Create<string?>(this, v =>
{
val = v;
}));
});

await cut.InvokeAsync(cut.Instance.TriggerClear);
Assert.Empty(val);
}

[Fact]
public async Task DisplayCount_Ok()
{
Expand Down
Loading