From c33eb02ace757adc75771f21514bc94d3fc37b20 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 2 Feb 2025 11:05:54 +0800 Subject: [PATCH] feat(BootstrapLabelSetting): add BootstrapLabelSetting component (#5274) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 增加 AddStyle 方法 * test: 增加单元测试 * refactor: use AddStyle method * doc: 重构代码 * refactor: 重构代码 * Revert "refactor: 重构代码" This reverts commit 37653dc4f1752f41e20b34745cda383c295fdcde. * feat: 增加 BootstrapLabelSetting 组件 * feat: 增加 LabelWidth 参数 * feat: 实现 LabelWidth 逻辑 * test: 增加单元测试 --- .../Components/Label/BootstrapLabel.razor.cs | 6 +++ .../Label/BootstrapLabelSetting.razor | 5 +++ .../Label/BootstrapLabelSetting.razor.cs | 24 +++++++++++ .../UnitTest/Components/BootstrapLabelTest.cs | 40 +++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 src/BootstrapBlazor/Components/Label/BootstrapLabelSetting.razor create mode 100644 src/BootstrapBlazor/Components/Label/BootstrapLabelSetting.razor.cs create mode 100644 test/UnitTest/Components/BootstrapLabelTest.cs diff --git a/src/BootstrapBlazor/Components/Label/BootstrapLabel.razor.cs b/src/BootstrapBlazor/Components/Label/BootstrapLabel.razor.cs index d5d7608f38d..8221930a009 100644 --- a/src/BootstrapBlazor/Components/Label/BootstrapLabel.razor.cs +++ b/src/BootstrapBlazor/Components/Label/BootstrapLabel.razor.cs @@ -29,6 +29,9 @@ public partial class BootstrapLabel [Parameter] public int? LabelWidth { get; set; } + [CascadingParameter] + private BootstrapLabelSetting? Setting { get; set; } + private bool _showTooltip; private string? ClassString => CssBuilder.Default("form-label") @@ -52,5 +55,8 @@ protected override void OnParametersSet() _showTooltip = ShowLabelTooltip.Value; } Value ??= ""; + + // 获得级联参数的 LabelWidth + LabelWidth ??= Setting?.LabelWidth; } } diff --git a/src/BootstrapBlazor/Components/Label/BootstrapLabelSetting.razor b/src/BootstrapBlazor/Components/Label/BootstrapLabelSetting.razor new file mode 100644 index 00000000000..a1d2d1a2123 --- /dev/null +++ b/src/BootstrapBlazor/Components/Label/BootstrapLabelSetting.razor @@ -0,0 +1,5 @@ +@namespace BootstrapBlazor.Components + + + @ChildContent + diff --git a/src/BootstrapBlazor/Components/Label/BootstrapLabelSetting.razor.cs b/src/BootstrapBlazor/Components/Label/BootstrapLabelSetting.razor.cs new file mode 100644 index 00000000000..ac3c1e1682c --- /dev/null +++ b/src/BootstrapBlazor/Components/Label/BootstrapLabelSetting.razor.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +namespace BootstrapBlazor.Components; + +/// +/// BootstrapLabelSetting 组件类 +/// +public partial class BootstrapLabelSetting +{ + /// + /// 获得/设置 子组件 + /// + [Parameter] + public RenderFragment? ChildContent { get; set; } + + /// + /// 获得/设置 标签宽度 默认 null 未设置使用全局设置 --bb-row-label-width 值 + /// + [Parameter] + public int? LabelWidth { get; set; } +} diff --git a/test/UnitTest/Components/BootstrapLabelTest.cs b/test/UnitTest/Components/BootstrapLabelTest.cs new file mode 100644 index 00000000000..9295034a7b5 --- /dev/null +++ b/test/UnitTest/Components/BootstrapLabelTest.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +namespace UnitTest.Components; + +public class BootstrapLabelTest : BootstrapBlazorTestBase +{ + [Fact] + public void BootstrapLabelSetting_LabelWidth_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.LabelWidth, 120); + pb.AddChildContent(); + }); + Assert.Equal("", cut.Markup); + + var label = cut.FindComponent(); + label.SetParametersAndRender(pb => + { + pb.Add(a => a.LabelWidth, 80); + }); + Assert.Equal("", cut.Markup); + } + + [Fact] + public void LabelWidth_Ok() + { + var cut = Context.RenderComponent(); + Assert.Equal("", cut.Markup); + + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.LabelWidth, 120); + }); + Assert.Equal("", cut.Markup); + } +}