Skip to content

Commit

Permalink
feat(BootstrapInput): add OnBlurAsync parameter (#4521)
Browse files Browse the repository at this point in the history
* feat(BootstrapInput): add OnBlurAsync parameter

* test: 增加单元测试

* test: 更新单元测试
  • Loading branch information
ArgoZhang authored Oct 24, 2024
1 parent dcdf40d commit 0afbc41
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Input/BootstrapInput.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
<BootstrapLabel required="@Required" for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
}

<input @attributes="@AdditionalAttributes" type="@Type" placeholder="@PlaceHolder" id="@Id" readonly="@ReadonlyString" class="@ClassName" disabled="@Disabled" @bind-value="CurrentValueAsString" @bind-value:event="@EventString" @ref="FocusElement" />
<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" />
17 changes: 17 additions & 0 deletions src/BootstrapBlazor/Components/Input/BootstrapInput.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public partial class BootstrapInput<TValue>
[Parameter]
public bool AutoSetDefaultWhenNull { get; set; }

/// <summary>
/// 获得/设置 失去焦点回调方法 默认 null
/// </summary>
[Parameter]
public Func<TValue, Task>? OnBlurAsync { get; set; }

private string? ReadonlyString => Readonly ? "true" : null;

/// <summary>
Expand All @@ -47,4 +53,15 @@ protected override bool TryParseValueFromString(string value, [MaybeNullWhen(fal
}
return ret;
}

/// <summary>
/// OnBlur 方法
/// </summary>
protected virtual async Task OnBlur()
{
if (OnBlurAsync != null)
{
await OnBlurAsync(Value);
}
}
}
17 changes: 17 additions & 0 deletions test/UnitTest/Components/InputTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,21 @@ public void OnValueChanged_Ok()
Assert.Equal("Test_Test-Test_Test", val);
});
}

[Fact]
public async Task OnBlurAsync_Ok()
{
var blur = false;
var cut = Context.RenderComponent<BootstrapInput<string>>(builder =>
{
builder.Add(a => a.OnBlurAsync, v =>
{
blur = true;
return Task.CompletedTask;
});
});
var input = cut.Find("input");
await cut.InvokeAsync(() => { input.Blur(); });
Assert.True(blur);
}
}
2 changes: 1 addition & 1 deletion test/UnitTest/Utils/UtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void CreateComponentByFieldType_Ok()
var editor = new MockNullDisplayNameColumn("Name", typeof(string)) { Readonly = true };
var fragment = new RenderFragment(builder => builder.CreateComponentByFieldType(new BootstrapBlazorRoot(), editor, new Foo() { Name = "Test-Component" }));
var cut = Context.Render(builder => builder.AddContent(0, fragment));
Assert.Contains("class=\"form-control\" disabled=\"disabled\" value=\"Test-Component\"", cut.Markup);
Assert.Contains("value=\"Test-Component\"", cut.Markup);
}

[Fact]
Expand Down

0 comments on commit 0afbc41

Please sign in to comment.