Skip to content

Commit 0afbc41

Browse files
authored
feat(BootstrapInput): add OnBlurAsync parameter (#4521)
* feat(BootstrapInput): add OnBlurAsync parameter * test: 增加单元测试 * test: 更新单元测试
1 parent dcdf40d commit 0afbc41

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/BootstrapBlazor/Components/Input/BootstrapInput.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
<BootstrapLabel required="@Required" for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
88
}
99

10-
<input @attributes="@AdditionalAttributes" type="@Type" placeholder="@PlaceHolder" id="@Id" readonly="@ReadonlyString" class="@ClassName" disabled="@Disabled" @bind-value="CurrentValueAsString" @bind-value:event="@EventString" @ref="FocusElement" />
10+
<input @attributes="@AdditionalAttributes" type="@Type" placeholder="@PlaceHolder" id="@Id" readonly="@ReadonlyString" class="@ClassName" disabled="@Disabled" @bind-value="CurrentValueAsString" @bind-value:event="@EventString" @onblur="@OnBlur" @ref="FocusElement" />

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public partial class BootstrapInput<TValue>
2222
[Parameter]
2323
public bool AutoSetDefaultWhenNull { get; set; }
2424

25+
/// <summary>
26+
/// 获得/设置 失去焦点回调方法 默认 null
27+
/// </summary>
28+
[Parameter]
29+
public Func<TValue, Task>? OnBlurAsync { get; set; }
30+
2531
private string? ReadonlyString => Readonly ? "true" : null;
2632

2733
/// <summary>
@@ -47,4 +53,15 @@ protected override bool TryParseValueFromString(string value, [MaybeNullWhen(fal
4753
}
4854
return ret;
4955
}
56+
57+
/// <summary>
58+
/// OnBlur 方法
59+
/// </summary>
60+
protected virtual async Task OnBlur()
61+
{
62+
if (OnBlurAsync != null)
63+
{
64+
await OnBlurAsync(Value);
65+
}
66+
}
5067
}

test/UnitTest/Components/InputTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,21 @@ public void OnValueChanged_Ok()
356356
Assert.Equal("Test_Test-Test_Test", val);
357357
});
358358
}
359+
360+
[Fact]
361+
public async Task OnBlurAsync_Ok()
362+
{
363+
var blur = false;
364+
var cut = Context.RenderComponent<BootstrapInput<string>>(builder =>
365+
{
366+
builder.Add(a => a.OnBlurAsync, v =>
367+
{
368+
blur = true;
369+
return Task.CompletedTask;
370+
});
371+
});
372+
var input = cut.Find("input");
373+
await cut.InvokeAsync(() => { input.Blur(); });
374+
Assert.True(blur);
375+
}
359376
}

test/UnitTest/Utils/UtilityTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public void CreateComponentByFieldType_Ok()
256256
var editor = new MockNullDisplayNameColumn("Name", typeof(string)) { Readonly = true };
257257
var fragment = new RenderFragment(builder => builder.CreateComponentByFieldType(new BootstrapBlazorRoot(), editor, new Foo() { Name = "Test-Component" }));
258258
var cut = Context.Render(builder => builder.AddContent(0, fragment));
259-
Assert.Contains("class=\"form-control\" disabled=\"disabled\" value=\"Test-Component\"", cut.Markup);
259+
Assert.Contains("value=\"Test-Component\"", cut.Markup);
260260
}
261261

262262
[Fact]

0 commit comments

Comments
 (0)