Skip to content

Commit ff5b664

Browse files
committed
refactor: 移动方法到基类
1 parent 7c9cee0 commit ff5b664

File tree

3 files changed

+53
-99
lines changed

3 files changed

+53
-99
lines changed

src/BootstrapBlazor/Components/Select/MultiSelect.razor.cs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@ public partial class MultiSelect<TValue>
4040
.AddClass("d-none", SelectedItems.Count != 0)
4141
.Build();
4242

43-
/// <summary>
44-
/// 获得/设置 绑定数据集
45-
/// </summary>
46-
[Parameter]
47-
[NotNull]
48-
public IEnumerable<SelectedItem>? Items { get; set; }
49-
50-
/// <summary>
51-
/// Gets or sets the callback method for loading virtualized items.
52-
/// </summary>
53-
[Parameter]
54-
[NotNull]
55-
public Func<VirtualizeQueryOption, Task<QueryData<SelectedItem>>>? OnQueryAsync { get; set; }
56-
5743
/// <summary>
5844
/// 获得/设置 选项模板
5945
/// </summary>
@@ -193,8 +179,6 @@ public partial class MultiSelect<TValue>
193179
[NotNull]
194180
private IStringLocalizer<MultiSelect<TValue>>? Localizer { get; set; }
195181

196-
private List<SelectedItem>? _itemsCache;
197-
198182
private List<SelectedItem> Rows
199183
{
200184
get
@@ -204,8 +188,6 @@ private List<SelectedItem> Rows
204188
}
205189
}
206190

207-
private string? _lastSelectedValueString;
208-
209191
private string? PlaceholderString => SelectedItems.Count == 0 ? PlaceHolder : null;
210192

211193
private string? ScrollIntoViewBehaviorString => ScrollIntoViewBehavior == ScrollIntoViewBehavior.Smooth ? null : ScrollIntoViewBehavior.ToDescriptionString();
@@ -268,7 +250,13 @@ protected override void OnAfterRender(bool firstRender)
268250
/// <inheritdoc/>
269251
/// </summary>
270252
/// <returns></returns>
271-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new { ConfirmMethodCallback = nameof(ConfirmSelectedItem), SearchMethodCallback = nameof(TriggerOnSearch), TriggerEditTag = nameof(TriggerEditTag), ToggleRow = nameof(ToggleRow) });
253+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
254+
{
255+
ConfirmMethodCallback = nameof(ConfirmSelectedItem),
256+
SearchMethodCallback = nameof(TriggerOnSearch),
257+
TriggerEditTag = nameof(TriggerEditTag),
258+
ToggleRow = nameof(ToggleRow)
259+
});
272260

273261
private int _totalCount;
274262
private ItemsProviderResult<SelectedItem> _result;
@@ -605,27 +593,4 @@ private void ResetItems()
605593
}
606594
}
607595
}
608-
609-
/// <summary>
610-
/// 客户端搜索栏回调方法
611-
/// </summary>
612-
/// <param name="searchText"></param>
613-
/// <returns></returns>
614-
[JSInvokable]
615-
public async Task TriggerOnSearch(string searchText)
616-
{
617-
_itemsCache = null;
618-
SearchText = searchText;
619-
await RefreshVirtualizeElement();
620-
StateHasChanged();
621-
}
622-
623-
private async Task RefreshVirtualizeElement()
624-
{
625-
if (IsVirtualize && OnQueryAsync != null)
626-
{
627-
// 通过 ItemProvider 提供数据
628-
await _virtualizeElement.RefreshDataAsync();
629-
}
630-
}
631596
}

src/BootstrapBlazor/Components/Select/Select.razor.cs

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ public partial class Select<TValue> : ISelect, ILookup
7676
[Parameter]
7777
public bool DisableItemChangedWhenFirstRender { get; set; }
7878

79-
/// <summary>
80-
/// Gets or sets the bound data set.
81-
/// </summary>
82-
[Parameter]
83-
[NotNull]
84-
public IEnumerable<SelectedItem>? Items { get; set; }
85-
8679
/// <summary>
8780
/// Gets or sets the item template.
8881
/// </summary>
@@ -143,13 +136,6 @@ public partial class Select<TValue> : ISelect, ILookup
143136
[Parameter]
144137
public object? LookupServiceData { get; set; }
145138

146-
/// <summary>
147-
/// Gets or sets the callback method for loading virtualized items.
148-
/// </summary>
149-
[Parameter]
150-
[NotNull]
151-
public Func<VirtualizeQueryOption, Task<QueryData<SelectedItem>>>? OnQueryAsync { get; set; }
152-
153139
/// <summary>
154140
/// <inheritdoc/>
155141
/// </summary>
@@ -178,12 +164,8 @@ public partial class Select<TValue> : ISelect, ILookup
178164

179165
private string? InputId => $"{Id}_input";
180166

181-
private string _lastSelectedValueString = string.Empty;
182-
183167
private bool _init = true;
184168

185-
private List<SelectedItem>? _itemsCache;
186-
187169
private ItemsProviderResult<SelectedItem> _result;
188170

189171
private SelectedItem? SelectedItem { get; set; }
@@ -292,21 +274,6 @@ protected override async Task OnParametersSetAsync()
292274
SelectedItem = null;
293275
}
294276

295-
/// <summary>
296-
/// <inheritdoc/>
297-
/// </summary>
298-
/// <returns></returns>
299-
protected override async Task OnAfterRenderAsync(bool firstRender)
300-
{
301-
await base.OnAfterRenderAsync(firstRender);
302-
303-
if (firstRender)
304-
{
305-
await RefreshVirtualizeElement();
306-
StateHasChanged();
307-
}
308-
}
309-
310277
private int _totalCount;
311278

312279
private List<SelectedItem> GetVirtualItems() => [.. FilterBySearchText(GetRowsByItems())];
@@ -327,15 +294,6 @@ private async ValueTask<ItemsProviderResult<SelectedItem>> LoadItems(ItemsProvid
327294
int GetCountByTotal() => _totalCount == 0 ? request.Count : Math.Min(request.Count, _totalCount - request.StartIndex);
328295
}
329296

330-
private async Task RefreshVirtualizeElement()
331-
{
332-
if (IsVirtualize && OnQueryAsync != null)
333-
{
334-
// 通过 ItemProvider 提供数据
335-
await _virtualizeElement.RefreshDataAsync();
336-
}
337-
}
338-
339297
/// <summary>
340298
/// <inheritdoc/>
341299
/// </summary>
@@ -371,7 +329,11 @@ private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue
371329
/// <summary>
372330
/// <inheritdoc/>
373331
/// </summary>
374-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new { ConfirmMethodCallback = nameof(ConfirmSelectedItem), SearchMethodCallback = nameof(TriggerOnSearch) });
332+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
333+
{
334+
ConfirmMethodCallback = nameof(ConfirmSelectedItem),
335+
SearchMethodCallback = nameof(TriggerOnSearch)
336+
});
375337

376338
/// <summary>
377339
/// Confirms the selected item.
@@ -388,20 +350,6 @@ public async Task ConfirmSelectedItem(int index)
388350
}
389351
}
390352

391-
/// <summary>
392-
/// Triggers the search callback method.
393-
/// </summary>
394-
/// <param name="searchText">The search text.</param>
395-
/// <returns>A task that represents the asynchronous operation.</returns>
396-
[JSInvokable]
397-
public async Task TriggerOnSearch(string searchText)
398-
{
399-
_itemsCache = null;
400-
SearchText = searchText;
401-
await RefreshVirtualizeElement();
402-
StateHasChanged();
403-
}
404-
405353
/// <summary>
406354
/// Handles the click event for a dropdown item.
407355
/// </summary>

src/BootstrapBlazor/Components/Select/SimpleSelectBase.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,45 @@ public class SimpleSelectBase<TValue> : SelectBase<TValue>
1818
/// </summary>
1919
[NotNull]
2020
protected Virtualize<SelectedItem>? _virtualizeElement = default;
21+
22+
protected string _lastSelectedValueString = string.Empty;
23+
24+
/// <summary>
25+
/// Gets or sets the items.
26+
/// </summary>
27+
[Parameter]
28+
[NotNull]
29+
public IEnumerable<SelectedItem>? Items { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the callback method for loading virtualized items.
33+
/// </summary>
34+
[Parameter]
35+
[NotNull]
36+
public Func<VirtualizeQueryOption, Task<QueryData<SelectedItem>>>? OnQueryAsync { get; set; }
37+
38+
protected List<SelectedItem>? _itemsCache;
39+
40+
/// <summary>
41+
/// Triggers the search callback method.
42+
/// </summary>
43+
/// <param name="searchText">The search text.</param>
44+
/// <returns>A task that represents the asynchronous operation.</returns>
45+
[JSInvokable]
46+
public async Task TriggerOnSearch(string searchText)
47+
{
48+
_itemsCache = null;
49+
SearchText = searchText;
50+
await RefreshVirtualizeElement();
51+
StateHasChanged();
52+
}
53+
54+
protected async Task RefreshVirtualizeElement()
55+
{
56+
if (IsVirtualize && OnQueryAsync != null)
57+
{
58+
// 通过 ItemProvider 提供数据
59+
await _virtualizeElement.RefreshDataAsync();
60+
}
61+
}
2162
}

0 commit comments

Comments
 (0)