Skip to content

Commit c33eb02

Browse files
authored
feat(BootstrapLabelSetting): add BootstrapLabelSetting component (#5274)
* feat: 增加 AddStyle 方法 * test: 增加单元测试 * refactor: use AddStyle method * doc: 重构代码 * refactor: 重构代码 * Revert "refactor: 重构代码" This reverts commit 37653dc. * feat: 增加 BootstrapLabelSetting 组件 * feat: 增加 LabelWidth 参数 * feat: 实现 LabelWidth 逻辑 * test: 增加单元测试
1 parent 3c07dff commit c33eb02

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/BootstrapBlazor/Components/Label/BootstrapLabel.razor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public partial class BootstrapLabel
2929
[Parameter]
3030
public int? LabelWidth { get; set; }
3131

32+
[CascadingParameter]
33+
private BootstrapLabelSetting? Setting { get; set; }
34+
3235
private bool _showTooltip;
3336

3437
private string? ClassString => CssBuilder.Default("form-label")
@@ -52,5 +55,8 @@ protected override void OnParametersSet()
5255
_showTooltip = ShowLabelTooltip.Value;
5356
}
5457
Value ??= "";
58+
59+
// 获得级联参数的 LabelWidth
60+
LabelWidth ??= Setting?.LabelWidth;
5561
}
5662
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@namespace BootstrapBlazor.Components
2+
3+
<CascadingValue Value="this" IsFixed="true">
4+
@ChildContent
5+
</CascadingValue>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// BootstrapLabelSetting 组件类
10+
/// </summary>
11+
public partial class BootstrapLabelSetting
12+
{
13+
/// <summary>
14+
/// 获得/设置 子组件
15+
/// </summary>
16+
[Parameter]
17+
public RenderFragment? ChildContent { get; set; }
18+
19+
/// <summary>
20+
/// 获得/设置 标签宽度 默认 null 未设置使用全局设置 <code>--bb-row-label-width</code> 值
21+
/// </summary>
22+
[Parameter]
23+
public int? LabelWidth { get; set; }
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace UnitTest.Components;
7+
8+
public class BootstrapLabelTest : BootstrapBlazorTestBase
9+
{
10+
[Fact]
11+
public void BootstrapLabelSetting_LabelWidth_Ok()
12+
{
13+
var cut = Context.RenderComponent<BootstrapLabelSetting>(pb =>
14+
{
15+
pb.Add(a => a.LabelWidth, 120);
16+
pb.AddChildContent<BootstrapLabel>();
17+
});
18+
Assert.Equal("<label class=\"form-label\" style=\"--bb-row-label-width: 120px;\"></label>", cut.Markup);
19+
20+
var label = cut.FindComponent<BootstrapLabel>();
21+
label.SetParametersAndRender(pb =>
22+
{
23+
pb.Add(a => a.LabelWidth, 80);
24+
});
25+
Assert.Equal("<label class=\"form-label\" style=\"--bb-row-label-width: 80px;\"></label>", cut.Markup);
26+
}
27+
28+
[Fact]
29+
public void LabelWidth_Ok()
30+
{
31+
var cut = Context.RenderComponent<BootstrapLabel>();
32+
Assert.Equal("<label class=\"form-label\"></label>", cut.Markup);
33+
34+
cut.SetParametersAndRender(pb =>
35+
{
36+
pb.Add(a => a.LabelWidth, 120);
37+
});
38+
Assert.Equal("<label class=\"form-label\" style=\"--bb-row-label-width: 120px;\"></label>", cut.Markup);
39+
}
40+
}

0 commit comments

Comments
 (0)