Skip to content

Commit 278aada

Browse files
authored
fix(BootstrapInputGroupLabel): should show DisplayText when bind-Value in ValidateForm (#5167)
* refactor: 代码格式化 * fix: 修复 DisplayText 丢失问题 * doc: 更新示例 * test: 更新单元测试
1 parent de1b5b1 commit 278aada

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/BootstrapBlazor.Server/Components/Samples/InputGroups.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@
113113
<ValidateForm Model="@Model">
114114
<div class="@GroupFormClassString">
115115
<div class="col-12 col-sm-6">
116-
<BootstrapInputGroupLabel @bind-Value="@Model.Name" required="true" />
116+
<BootstrapInputGroupLabel @bind-Value="@Model.Name" ShowRequiredMark="true" />
117117
<BootstrapInputGroup>
118118
<Display @bind-Value="@Model.Name"></Display>
119119
<BootstrapInputGroupLabel @bind-Value="@Model.Name" />
120120
</BootstrapInputGroup>
121121
</div>
122122
<div class="col-12 col-sm-6">
123-
<BootstrapInputGroupLabel @bind-Value="@Model.Address" required="true" />
123+
<BootstrapInputGroupLabel @bind-Value="@Model.Address" ShowRequiredMark="true" />
124124
<BootstrapInputGroup>
125125
<Display @bind-Value="@Model.Address"></Display>
126126
<BootstrapInputGroupLabel @bind-Value="@Model.Address" />

src/BootstrapBlazor/Components/Input/BootstrapInputGroupLabel.razor.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public partial class BootstrapInputGroupLabel
1919
.Build();
2020

2121
private string? StyleString => CssBuilder.Default()
22-
.AddClass($"--bb-input-group-label-width: {Width}px;", Width.HasValue)
23-
.AddClassFromAttributes(AdditionalAttributes)
24-
.Build();
22+
.AddClass($"--bb-input-group-label-width: {Width}px;", Width.HasValue)
23+
.AddClassFromAttributes(AdditionalAttributes)
24+
.Build();
2525

2626
private bool IsInnerLabel { get; set; }
2727

@@ -53,5 +53,10 @@ protected override void OnParametersSet()
5353
base.OnParametersSet();
5454

5555
IsInnerLabel = InputGroup != null;
56+
57+
if (IsInnerLabel)
58+
{
59+
DisplayText ??= FieldIdentifier?.GetDisplayName();
60+
}
5661
}
5762
}

test/UnitTest/Components/DisplayTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void ShowTooltip_Ok()
191191
[Fact]
192192
public void Bind_Ok()
193193
{
194-
var foo = new Foo();
194+
var foo = new Foo() { Name = "test-name" };
195195
var cut = Context.RenderComponent<ValidateForm>(pb =>
196196
{
197197
pb.Add(a => a.Model, foo);
@@ -201,21 +201,21 @@ public void Bind_Ok()
201201
{
202202
builder.OpenComponent<Display<string>>(0);
203203
builder.AddAttribute(1, "Value", foo.Name);
204-
builder.AddAttribute(2, "ValueExpression",
205-
Utility.GenerateValueExpression(foo, "Name", typeof(string)));
204+
builder.AddAttribute(2, "ValueExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
206205
builder.CloseComponent();
207206

208207
builder.OpenComponent<BootstrapInputGroupLabel>(3);
209208
builder.AddAttribute(4, "Value", "Name");
210-
builder.AddAttribute(5, "ValueExpression",
211-
Utility.GenerateValueExpression(foo, "Name", typeof(string)));
209+
builder.AddAttribute(5, "ValueExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
210+
builder.CloseComponent();
211+
212+
builder.OpenComponent<BootstrapInputGroupLabel>(3);
213+
builder.AddAttribute(6, "Value", "Name");
212214
builder.CloseComponent();
213215
});
214216
});
215217
});
216-
Assert.Contains("is-display", cut.Markup);
217-
Assert.Contains("input-group-text", cut.Markup);
218-
Assert.DoesNotContain("<span>姓名</span>", cut.Markup);
218+
Assert.Contains("<div class=\"input-group\"><div class=\"form-control is-display\">test-name</div><div class=\"input-group-text\"><span>姓名</span></div><div class=\"input-group-text\"><span></span></div></div>", cut.Markup);
219219
}
220220

221221
[Fact]

0 commit comments

Comments
 (0)