diff --git a/src/BootstrapBlazor/Components/Button/PopConfirmButton.razor b/src/BootstrapBlazor/Components/Button/PopConfirmButton.razor index 75a54c1cc53..db8af09f299 100644 --- a/src/BootstrapBlazor/Components/Button/PopConfirmButton.razor +++ b/src/BootstrapBlazor/Components/Button/PopConfirmButton.razor @@ -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>; } diff --git a/src/BootstrapBlazor/Components/Button/PopConfirmButtonBase.cs b/src/BootstrapBlazor/Components/Button/PopConfirmButtonBase.cs index 623de23331b..89bbe4cf629 100644 --- a/src/BootstrapBlazor/Components/Button/PopConfirmButtonBase.cs +++ b/src/BootstrapBlazor/Components/Button/PopConfirmButtonBase.cs @@ -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; } @@ -58,6 +58,12 @@ public abstract class PopConfirmButtonBase : ButtonBase [NotNull] public Func<Task>? OnConfirm { get; set; } + /// <summary> + /// 获得/设置 是否显示确认按钮 + /// </summary> + [Parameter] + public bool ShowConfirmButton { get; set; } = true; + /// <summary> /// 获得/设置 点击关闭时回调方法 /// </summary> @@ -65,6 +71,12 @@ public abstract class PopConfirmButtonBase : ButtonBase [NotNull] public Func<Task>? OnClose { get; set; } + /// <summary> + /// 获得/设置 是否显示关闭按钮 + /// </summary> + [Parameter] + public bool ShowCloseButton { get; set; } = true; + /// <summary> /// 获得/设置 点击确认弹窗前回调方法 返回真时弹出弹窗 返回假时不弹出 默认 null /// </summary> diff --git a/src/BootstrapBlazor/Components/Button/PopConfirmButtonContent.razor b/src/BootstrapBlazor/Components/Button/PopConfirmButtonContent.razor index f4c69075170..507c7ccfa08 100644 --- a/src/BootstrapBlazor/Components/Button/PopConfirmButtonContent.razor +++ b/src/BootstrapBlazor/Components/Button/PopConfirmButtonContent.razor @@ -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 { @@ -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> diff --git a/src/BootstrapBlazor/Components/Button/PopConfirmButtonContent.razor.cs b/src/BootstrapBlazor/Components/Button/PopConfirmButtonContent.razor.cs index 8e773a64b45..23071f3e329 100644 --- a/src/BootstrapBlazor/Components/Button/PopConfirmButtonContent.razor.cs +++ b/src/BootstrapBlazor/Components/Button/PopConfirmButtonContent.razor.cs @@ -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> diff --git a/test/UnitTest/Components/PopConfirmButtonTest.cs b/test/UnitTest/Components/PopConfirmButtonTest.cs index dd48f59c22e..2a33be83130 100644 --- a/test/UnitTest/Components/PopConfirmButtonTest.cs +++ b/test/UnitTest/Components/PopConfirmButtonTest.cs @@ -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); @@ -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 @@ -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; }); }); @@ -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 @@ -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] @@ -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; + } }