Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Select): Clear search text while clearing text content #3340

Merged
merged 6 commits into from
Apr 25, 2024
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
16 changes: 15 additions & 1 deletion src/BootstrapBlazor/Components/Select/Select.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public partial class Select<TValue> : ISelect
[Parameter]
public string? DefaultVirtualizeItemText { get; set; }

/// <summary>
/// 获得/设置 清除文本内容 OnClear 回调方法 默认 null
/// </summary>
[Parameter]
public Func<Task>? OnClearAsync { get; set; }

/// <summary>
/// 获得/设置 禁止首次加载时触发 OnSelectedItemChanged 回调方法 默认 false
/// </summary>
Expand Down Expand Up @@ -438,8 +444,16 @@ private async Task SelectedItemChanged(SelectedItem item)
/// </summary>
public void ClearSearchText() => SearchText = null;

private void OnClearValue()
private async Task OnClearValue()
{
if (ShowSearch)
{
ClearSearchText();
}
if (OnClearAsync != null)
{
await OnClearAsync();
}
CurrentValue = default;
}

Expand Down
31 changes: 31 additions & 0 deletions test/UnitTest/Components/SelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,35 @@ await cut.InvokeAsync(() =>
Assert.Equal("Test3", cut.Instance.Value);
Assert.True(updated);
}

[Fact]
public async Task OnClearAsync_Ok()
{
var clear = false;
var cut = Context.RenderComponent<Select<string>>(pb =>
{
pb.Add(a => a.Items, new SelectedItem[]
{
new("1", "<div>Test1</div>"),
new("2", "<div>Test2</div>")
});
pb.Add(a => a.Value, "2");
pb.Add(a => a.ShowSearch, true);
pb.Add(a => a.IsClearable, true);
pb.Add(a => a.OnClearAsync, () =>
{
clear = true;
return Task.CompletedTask;
});
});

var span = cut.Find(".clear-icon");
Assert.NotNull(span);

await cut.InvokeAsync(() =>
{
span.Click();
});
Assert.True(clear);
}
}