Skip to content

Commit b6272ee

Browse files
authored
feat(Table): use Enter/EscCallbck instead of keyup event (#7672)
* doc: 更新注释 * feat(Table): 增加 OnEnterAsync OnEscAsync 回调方法 * doc: 更新注释 * refactor: 复用代码 * refactor: 移除 OnSearchKeyUp 方法 * refactor: 移除冗余代码 * test: 更新单元测试
1 parent 9642a80 commit b6272ee

File tree

4 files changed

+30
-38
lines changed

4 files changed

+30
-38
lines changed

src/BootstrapBlazor/Components/Table/Table.razor

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,17 @@
197197
{
198198
<Tooltip Placement="Placement.Top" Title="@SearchTooltip" Sanitize="false" IsHtml="true">
199199
<BootstrapInput class="table-toolbar-search" placeholder="@SearchPlaceholderText"
200-
@onkeyup="OnSearchKeyUp" Value="@SearchText"
201-
OnValueChanged="OnSearchTextValueChanged"
200+
Value="@SearchText" SkipValidate="true"
201+
OnEnterAsync="OnEnterAsync" OnEscAsync="OnEscAsync"
202202
ShowLabel="false" UseInputEvent="AutoSearchOnInput">
203203
</BootstrapInput>
204204
</Tooltip>
205205
}
206206
else
207207
{
208208
<BootstrapInput class="table-toolbar-search" placeholder="@SearchPlaceholderText"
209-
Value="@SearchText" OnValueChanged="OnSearchTextValueChanged"
209+
Value="@SearchText" SkipValidate="true"
210+
OnEnterAsync="OnEnterAsync" OnEscAsync="OnEscAsync"
210211
ShowLabel="false" UseInputEvent="AutoSearchOnInput">
211212
</BootstrapInput>
212213
}
@@ -1049,18 +1050,18 @@
10491050
{
10501051
<Tooltip Placement="Placement.Top" Title="@SearchTooltip" Sanitize="false" IsHtml="true">
10511052
<BootstrapInput class="table-toolbar-search" placeholder="@SearchPlaceholderText"
1052-
@onkeyup="OnSearchKeyUp" Value="@SearchText"
1053-
OnValueChanged="OnSearchTextValueChanged"
1053+
Value="@SearchText"
1054+
OnEnterAsync="OnEnterAsync" OnEscAsync="OnEscAsync"
10541055
ShowLabel="false" SkipValidate="true" UseInputEvent="AutoSearchOnInput">
10551056
</BootstrapInput>
10561057
</Tooltip>
10571058
}
10581059
else
10591060
{
10601061
<BootstrapInput class="table-toolbar-search" placeholder="@SearchPlaceholderText"
1061-
@onkeyup="OnSearchKeyUp" Value="@SearchText"
1062-
OnValueChanged="OnSearchTextValueChanged"
1063-
SkipValidate="true" UseInputEvent="AutoSearchOnInput">
1062+
Value="@SearchText"
1063+
OnEnterAsync="OnEnterAsync" OnEscAsync="OnEscAsync"
1064+
ShowLabel="false" SkipValidate="true" UseInputEvent="AutoSearchOnInput">
10641065
</BootstrapInput>
10651066
}
10661067
}

src/BootstrapBlazor/Components/Table/Table.razor.Search.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
55

6-
using Microsoft.AspNetCore.Components.Web;
76
using System.Reflection;
87

98
namespace BootstrapBlazor.Components;
@@ -163,11 +162,22 @@ protected async Task ResetSearchClick()
163162
Utility.Reset(SearchModel, CreateSearchModel());
164163
}
165164

166-
PageIndex = 1;
167-
await QueryAsync();
165+
await SearchClick();
168166
await ToggleLoading(false);
169167
}
170168

169+
private async Task OnEnterAsync(string? v)
170+
{
171+
SearchText = v;
172+
await SearchClick();
173+
}
174+
175+
private async Task OnEscAsync(string? v)
176+
{
177+
SearchText = null;
178+
await ResetSearchClick();
179+
}
180+
171181
/// <summary>
172182
/// <para lang="zh">查询方法</para>
173183
/// <para lang="en">Search Method</para>
@@ -304,28 +314,9 @@ protected List<IFilterAction> GetAdvanceSearches()
304314
/// </summary>
305315
protected List<IFilterAction> GetSearches() => Columns.Where(col => col.GetSearchable()).ToSearches(SearchText);
306316

307-
private async Task OnSearchTextValueChanged(string? value)
308-
{
309-
SearchText = value;
310-
311-
await SearchClick();
312-
}
313-
314-
private async Task OnSearchKeyUp(KeyboardEventArgs args)
315-
{
316-
if (args.Key == "Enter")
317-
{
318-
await SearchClick();
319-
}
320-
else if (args.Key == "Escape")
321-
{
322-
await ClearSearchClick();
323-
}
324-
}
325-
326317
/// <summary>
327-
/// <para lang="zh">重置搜索按钮调用此方法</para>
328-
/// <para lang="en">Reset Search Button Click Method</para>
318+
/// <para lang="zh">点击重置搜索按钮时调用此方法</para>
319+
/// <para lang="en">Method called when the reset search button is clicked</para>
329320
/// </summary>
330321
protected async Task ClearSearchClick()
331322
{

src/BootstrapBlazor/Components/Table/Table.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace BootstrapBlazor.Components;
1818
public partial class Table<TItem> : ITable, IModelEqualityComparer<TItem> where TItem : class
1919
{
2020
/// <summary>
21-
/// <para lang="zh">获得/设置 a value indicating 是否 automatic search functionality is enabled. 默认 value is false</para>
22-
/// <para lang="en">Gets or sets a value indicating whether automatic search functionality is enabled. Default value is false</para>
21+
/// <para lang="zh">获得/设置 模糊搜索栏输入时是否自动搜索 默认值 false</para>
22+
/// <para lang="en">Gets or sets whether to auto search on fuzzy search bar input. Default false</para>
2323
/// </summary>
2424
[Parameter]
2525
public bool AutoSearchOnInput { get; set; }

test/UnitTest/Components/TableTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,10 @@ public async Task OnSearchKeyUp_Ok()
408408
});
409409
});
410410
});
411-
var searchBox = cut.Find(".table-toolbar-search");
412-
await cut.InvokeAsync(() => searchBox.KeyUp(new KeyboardEventArgs() { Key = "Enter" }));
413-
await cut.InvokeAsync(() => searchBox.KeyUp(new KeyboardEventArgs() { Key = "Escape" }));
414-
await cut.InvokeAsync(() => searchBox.Change("0"));
411+
var searchBox = cut.FindComponents<BootstrapInput<string?>>().FirstOrDefault(i => i.Markup.Contains("table-toolbar-search"));
412+
Assert.NotNull(searchBox);
413+
await cut.InvokeAsync(() => searchBox.Instance.EnterCallback());
414+
await cut.InvokeAsync(() => searchBox.Instance.EscCallback());
415415
Assert.True(resetSearch);
416416
}
417417

0 commit comments

Comments
 (0)