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(PopConfirmButton): add OnCloseAsync/OnConfirmAsync cascade parameter #3728

Merged
merged 5 commits into from
Jun 26, 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
3 changes: 2 additions & 1 deletion src/BootstrapBlazor/Components/Button/PopConfirmButton.razor
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ else
<PopConfirmButtonContent Title="@Title" Content="@Content" Icon="@ConfirmIcon"
CloseButtonColor="@CloseButtonColor" CloseButtonText="@CloseButtonText"
ConfirmButtonColor="@ConfirmButtonColor" ConfirmButtonText="@ConfirmButtonText"
OnClose="@OnClose" OnConfirm="@OnClickConfirm" ChildContent="@BodyTemplate" />
ShowCloseButton="@ShowCloseButton" OnClose="@OnClose"
ShowConfirmButton="@ShowConfirmButton" OnConfirm="@OnClickConfirm" ChildContent="@BodyTemplate" />
</DynamicElement>;
}
14 changes: 13 additions & 1 deletion src/BootstrapBlazor/Components/Button/PopConfirmButtonBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class PopConfirmButtonBase : ButtonBase
protected override string? TriggerString => Trigger == "click" ? null : Trigger;

/// <summary>
/// 获得/设置 是否为 A 标签 默认 false 使用 button 渲染
/// 获得/设置 是否为 A 标签 默认 false 使用 button 渲染
/// </summary>
[Parameter]
public bool IsLink { get; set; }
Expand Down Expand Up @@ -58,13 +58,25 @@ public abstract class PopConfirmButtonBase : ButtonBase
[NotNull]
public Func<Task>? OnConfirm { get; set; }

/// <summary>
/// 获得/设置 是否显示确认按钮
/// </summary>
[Parameter]
public bool ShowConfirmButton { get; set; } = true;

/// <summary>
/// 获得/设置 点击关闭时回调方法
/// </summary>
[Parameter]
[NotNull]
public Func<Task>? OnClose { get; set; }

/// <summary>
/// 获得/设置 是否显示关闭按钮
/// </summary>
[Parameter]
public bool ShowCloseButton { get; set; } = true;

/// <summary>
/// 获得/设置 点击确认弹窗前回调方法 返回真时弹出弹窗 返回假时不弹出 默认 null
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
<p class="popover-confirm-body">
@if (ChildContent != null)
{
@ChildContent
<CascadingValue Value="OnCloseClick" Name="PopoverConfirmButtonCloseAsync" IsFixed="true">
<CascadingValue Value="OnConfirmClick" Name="PopoverConfirmButtonConfirmAsync" IsFixed="true">
@ChildContent
</CascadingValue>
</CascadingValue>
}
else
{
Expand All @@ -19,12 +23,18 @@
}
</p>
<div class="popover-confirm-buttons">
<div class="@CloseButtonClass" @onclick="@OnCloseClick">
<span>@CloseButtonText</span>
</div>
<div class="@ConfirmButtonClass" @onclick="@OnConfirmClick">
<span>@ConfirmButtonText</span>
</div>
@if (ShowCloseButton)
{
<div class="@CloseButtonClass" @onclick="@OnCloseClick">
<span>@CloseButtonText</span>
</div>
}
@if (ShowConfirmButton)
{
<div class="@ConfirmButtonClass" @onclick="@OnConfirmClick">
<span>@ConfirmButtonText</span>
</div>
}
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public partial class PopConfirmButtonContent
.AddClass(Icon)
.Build();

/// <summary>
/// 获得/设置 是否显示确认按钮
/// </summary>
[Parameter]
public bool ShowConfirmButton { get; set; } = true;

/// <summary>
/// 获得/设置 是否显示关闭按钮
/// </summary>
[Parameter]
public bool ShowCloseButton { get; set; } = true;

/// <summary>
/// 获得/设置 显示标题
/// </summary>
Expand Down
140 changes: 74 additions & 66 deletions test/UnitTest/Components/PopConfirmButtonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,36 @@ await cut.InvokeAsync(() =>
var close = false;
var beforeClose = false;

await cut.InvokeAsync(() =>
var popButton = cut.FindComponent<PopConfirmButton>();
popButton.SetParametersAndRender(pb =>
{
var popButton = cut.FindComponent<PopConfirmButton>();
popButton.SetParametersAndRender(pb =>
pb.Add(a => a.Content, "Test_Content");
pb.Add(a => a.ConfirmButtonText, "Test_Confirm_Text");
pb.Add(a => a.CloseButtonText, "Test_Close_Text");
pb.Add(a => a.CustomClass, "test-custom-class");
pb.Add(a => a.OnConfirm, () =>
{
pb.Add(a => a.Content, "Test_Content");
pb.Add(a => a.ConfirmButtonText, "Test_Confirm_Text");
pb.Add(a => a.CloseButtonText, "Test_Close_Text");
pb.Add(a => a.CustomClass, "test-custom-class");
pb.Add(a => a.OnConfirm, () =>
{
confirm = true;
return Task.CompletedTask;
});
pb.Add(a => a.OnClose, () =>
{
close = true;
return Task.CompletedTask;
});
pb.Add(a => a.OnBeforeClick, () =>
{
beforeClose = true;
return Task.FromResult(true);
});
confirm = true;
return Task.CompletedTask;
});
pb.Add(a => a.OnClose, () =>
{
close = true;
return Task.CompletedTask;
});
pb.Add(a => a.OnBeforeClick, () =>
{
beforeClose = true;
return Task.FromResult(true);
});
});
// 默认设置增加 shadow 样式
Assert.Contains("data-bs-custom-class=\"test-custom-class shadow\"", cut.Markup);

var popButton = cut.FindComponent<PopConfirmButton>();
await cut.InvokeAsync(() =>
// 移除 shadow 样式
popButton.SetParametersAndRender(pb =>
{
// 移除 shadow 样式
popButton.SetParametersAndRender(pb =>
{
pb.Add(a => a.ShowShadow, false);
});
pb.Add(a => a.ShowShadow, false);
});
Assert.Contains("data-bs-custom-class=\"test-custom-class\"", cut.Markup);

Expand Down Expand Up @@ -125,13 +118,9 @@ await cut.InvokeAsync(() =>
Assert.True(confirm);

// Submit
popButton = cut.FindComponent<PopConfirmButton>();
await cut.InvokeAsync(() =>
popButton.SetParametersAndRender(pb =>
{
popButton.SetParametersAndRender(pb =>
{
pb.Add(a => a.ButtonType, ButtonType.Submit);
});
pb.Add(a => a.ButtonType, ButtonType.Submit);
});

// Show
Expand All @@ -150,17 +139,13 @@ await cut.InvokeAsync(() =>

// IsAsync
var tcs = new TaskCompletionSource();
popButton = cut.FindComponent<PopConfirmButton>();
await cut.InvokeAsync(() =>
popButton.SetParametersAndRender(pb =>
{
popButton.SetParametersAndRender(pb =>
pb.Add(a => a.IsAsync, true);
pb.Add(a => a.OnConfirm, () =>
{
pb.Add(a => a.IsAsync, true);
pb.Add(a => a.OnConfirm, () =>
{
tcs.TrySetResult();
return Task.CompletedTask;
});
tcs.TrySetResult();
return Task.CompletedTask;
});
});

Expand All @@ -179,13 +164,9 @@ await cut.InvokeAsync(() =>
});
await tcs.Task;

popButton = cut.FindComponent<PopConfirmButton>();
await cut.InvokeAsync(() =>
popButton.SetParametersAndRender(pb =>
{
popButton.SetParametersAndRender(pb =>
{
pb.Add(a => a.ButtonType, ButtonType.Button);
});
pb.Add(a => a.ButtonType, ButtonType.Button);
});

// Show
Expand All @@ -203,39 +184,53 @@ await cut.InvokeAsync(() =>
});

// IsLink
popButton = cut.FindComponent<PopConfirmButton>();
await cut.InvokeAsync(() =>
popButton.SetParametersAndRender(pb =>
{
popButton.SetParametersAndRender(pb =>
{
pb.Add(a => a.IsLink, true);
});
pb.Add(a => a.IsLink, true);
});

popButton = cut.FindComponent<PopConfirmButton>();
await cut.InvokeAsync(() =>
{
popButton.Contains("<a id=");
});

popButton = cut.FindComponent<PopConfirmButton>();
await cut.InvokeAsync(() =>
popButton.SetParametersAndRender(pb =>
{
popButton.SetParametersAndRender(pb =>
pb.Add(a => a.BodyTemplate, builder =>
{
pb.Add(a => a.BodyTemplate, builder =>
{
builder.OpenComponent<Button>(0);
builder.CloseComponent();
});
builder.OpenComponent<Button>(0);
builder.CloseComponent();
});
});

popButton = cut.FindComponent<PopConfirmButton>();
await cut.InvokeAsync(() =>
{
Assert.NotNull(popButton.FindComponent<Button>());
});

// ShowButton
popButton.SetParametersAndRender(pb =>
{
pb.Add(a => a.ShowCloseButton, false);
pb.Add(a => a.ShowConfirmButton, false);
});
cut.Contains("<div class=\"popover-confirm-buttons\"></div>");

// 级联参数
popButton.SetParametersAndRender(pb =>
{
pb.Add(a => a.ShowCloseButton, true);
pb.Add(a => a.ShowConfirmButton, true);
pb.Add(a => a.BodyTemplate, builder =>
{
builder.OpenComponent<MockContent>(0);
builder.CloseComponent();
});
});

var mockContent = cut.FindComponent<MockContent>();
Assert.True(mockContent.Instance.Close());
Assert.True(mockContent.Instance.Confirm());
}

[Fact]
Expand Down Expand Up @@ -293,4 +288,17 @@ public void TooltipText_Ok()
});
cut.Contains("data-bs-original-title=\"pop-tooltip\"");
}

public class MockContent : ComponentBase
{
[CascadingParameter(Name = "PopoverConfirmButtonCloseAsync")]
private Func<Task>? OnCloseAsync { get; set; }

[CascadingParameter(Name = "PopoverConfirmButtonConfirmAsync")]
private Func<Task>? OnConfirmAsync { get; set; }

public bool Close() => OnCloseAsync != null;

public bool Confirm() => OnConfirmAsync != null;
}
}