Skip to content

Commit 6c14cff

Browse files
committed
refactor: 重构 ShowSwal 逻辑
1 parent 6844004 commit 6c14cff

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ public partial class Select<TValue> : ISelect, ILookup
5252
public bool DisableItemChangedWhenFirstRender { get; set; }
5353

5454
/// <summary>
55-
/// Gets or sets the callback method before the selected item changes. Returns true to change the selected item value; otherwise, the selected item value does not change.
55+
/// 获取/设置 选中项改变前的回调方法。返回 true 则改变选中项的值;否则选中项的值不变。
56+
/// <para>Gets or sets the callback method before the selected item changes. Returns true to change the selected item value; otherwise, the selected item value does not change.</para>
5657
/// </summary>
5758
[Parameter]
5859
public Func<SelectedItem, Task<bool>>? OnBeforeSelectedItemChange { get; set; }
5960

6061
/// <summary>
6162
/// Gets or sets whether to show the Swal confirmation popup when <see cref="OnBeforeSelectedItemChange"/> returns true. Default is true.
62-
/// 获得/设置 是否显示 Swal 确认弹窗
63+
/// 获得/设置 是否显示 Swal 确认弹窗 默认值 为 true
6364
/// </summary>
6465
[Parameter]
6566
public bool ShowSwal { get; set; } = true;
@@ -342,26 +343,33 @@ public async Task ConfirmSelectedItem(int index)
342343
private async Task OnClickItem(SelectedItem item)
343344
{
344345
var ret = true;
346+
347+
// 自定义回调方法 OnBeforeSelectedItemChange 返回 false 时不修改选中项
345348
if (OnBeforeSelectedItemChange != null)
346349
{
347350
ret = await OnBeforeSelectedItemChange(item);
348-
if (ret && ShowSwal)
351+
return;
352+
}
353+
354+
// 如果 ShowSwal 为 true 且 则显示 Swal 确认弹窗,通过确认弹窗返回值决定是否修改选中项
355+
if (ShowSwal)
356+
{
357+
// Return true to show modal
358+
var option = new SwalOption()
359+
{
360+
Category = SwalCategory,
361+
Title = SwalTitle,
362+
Content = SwalContent
363+
};
364+
if (!string.IsNullOrEmpty(SwalFooter))
349365
{
350-
// Return true to show modal
351-
var option = new SwalOption()
352-
{
353-
Category = SwalCategory,
354-
Title = SwalTitle,
355-
Content = SwalContent
356-
};
357-
if (!string.IsNullOrEmpty(SwalFooter))
358-
{
359-
option.ShowFooter = true;
360-
option.FooterTemplate = builder => builder.AddContent(0, SwalFooter);
361-
}
362-
ret = await SwalService.ShowModal(option);
366+
option.ShowFooter = true;
367+
option.FooterTemplate = builder => builder.AddContent(0, SwalFooter);
363368
}
369+
ret = await SwalService.ShowModal(option);
364370
}
371+
372+
// 如果 ret 为 true 则修改选中项
365373
if (ret)
366374
{
367375
_defaultVirtualizedItemText = item.Text;

0 commit comments

Comments
 (0)