Skip to content

Commit 66bf955

Browse files
committed
refactor: 更改为泛型组件
1 parent 3659fb6 commit 66bf955

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/BootstrapBlazor/Components/Search/Search.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@namespace BootstrapBlazor.Components
2-
@inherits PopoverCompleteBase<string>
2+
@typeparam TValue
3+
@inherits PopoverCompleteBase<TValue>
34

45
<div @attributes="@AdditionalAttributes" class="@ClassString" id="@Id">
56
<div class="input-group">
@@ -27,7 +28,7 @@
2728
}
2829
else
2930
{
30-
<div>@item</div>
31+
<div>@GetDisplayText(item)</div>
3132
}
3233
</li>
3334
}

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BootstrapBlazor.Components;
1010
/// <summary>
1111
/// Search 组件
1212
/// </summary>
13-
public partial class Search
13+
public partial class Search<TValue>
1414
{
1515
/// <summary>
1616
/// 获得/设置 是否显示清除按钮 默认为 false 不显示
@@ -77,17 +77,24 @@ public partial class Search
7777
/// 获得/设置 点击搜索按钮时回调委托
7878
/// </summary>
7979
[Parameter]
80-
public Func<string, Task<IEnumerable<string>>>? OnSearch { get; set; }
80+
public Func<string, Task<IEnumerable<TValue>>>? OnSearch { get; set; }
81+
82+
/// <summary>
83+
/// 获得/设置 通过模型获得显示文本方法 默认使用 ToString 重载方法
84+
/// </summary>
85+
[Parameter]
86+
[NotNull]
87+
public Func<TValue, string?>? OnGetDisplayText { get; set; }
8188

8289
/// <summary>
8390
/// 获得/设置 点击清空按钮时回调委托
8491
/// </summary>
8592
[Parameter]
86-
public Func<string, Task>? OnClear { get; set; }
93+
public Func<string?, Task>? OnClear { get; set; }
8794

8895
[Inject]
8996
[NotNull]
90-
private IStringLocalizer<Search>? Localizer { get; set; }
97+
private IStringLocalizer<Search<TValue>>? Localizer { get; set; }
9198

9299
/// <summary>
93100
/// <inheritdoc/>
@@ -105,7 +112,7 @@ public partial class Search
105112
/// 获得/设置 UI 呈现数据集合
106113
/// </summary>
107114
[NotNull]
108-
private List<string>? FilterItems { get; set; }
115+
private List<TValue>? FilterItems { get; set; }
109116

110117
/// <summary>
111118
/// <inheritdoc/>
@@ -139,6 +146,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
139146
}
140147
}
141148

149+
private string? _displayText;
142150
private bool _show;
143151
/// <summary>
144152
/// 点击搜索按钮时触发此方法
@@ -151,12 +159,12 @@ private async Task OnSearchClick()
151159
ButtonIcon = SearchButtonLoadingIcon;
152160
await Task.Yield();
153161

154-
var items = await OnSearch(Value);
162+
var items = await OnSearch(_displayText);
155163
FilterItems = items.ToList();
156164
ButtonIcon = SearchButtonIcon;
157165
if (IsAutoClearAfterSearch)
158166
{
159-
Value = "";
167+
_displayText = "";
160168
}
161169
if (IsOnInputTrigger == false)
162170
{
@@ -174,12 +182,22 @@ private async Task OnClearClick()
174182
{
175183
if (OnClear != null)
176184
{
177-
await OnClear(Value);
185+
await OnClear(_displayText);
178186
}
179-
CurrentValue = "";
187+
_displayText = "";
180188
FilterItems = [];
181189
}
182190

191+
private string? GetDisplayText(TValue item)
192+
{
193+
var displayText = item?.ToString();
194+
if (OnGetDisplayText != null)
195+
{
196+
displayText = OnGetDisplayText(item);
197+
}
198+
return displayText;
199+
}
200+
183201
/// <summary>
184202
/// TriggerOnChange 方法
185203
/// </summary>
@@ -188,7 +206,7 @@ private async Task OnClearClick()
188206
[JSInvokable]
189207
public async Task TriggerOnChange(string val, bool search = true)
190208
{
191-
CurrentValue = val;
209+
_displayText = val;
192210

193211
if (search)
194212
{

0 commit comments

Comments
 (0)