Skip to content

Commit 41e26e2

Browse files
authored
feat(Textarea): add OnBlurAsync parameter (#4518)
* feat: 增加 OnBlurAsync 回调方法 * test: 更新单元测试
1 parent 0b24a25 commit 41e26e2

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/BootstrapBlazor/Components/Textarea/Textarea.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
{
77
<BootstrapLabel required="@Required" for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
88
}
9-
<textarea @attributes="AdditionalAttributes" placeholder="@PlaceHolder" id="@Id" class="@ClassName" disabled="@Disabled" @bind-value="@CurrentValueAsString" @bind-value:event="@EventString" data-bb-scroll="@AutoScrollString" @ref="FocusElement"></textarea>
9+
<textarea @attributes="AdditionalAttributes" placeholder="@PlaceHolder" id="@Id" class="@ClassName" disabled="@Disabled" @bind-value="@CurrentValueAsString" @bind-value:event="@EventString" @onblur="OnBlur" data-bb-scroll="@AutoScrollString" @ref="FocusElement"></textarea>

src/BootstrapBlazor/Components/Textarea/Textarea.razor.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public partial class Textarea
3434
[Parameter]
3535
public bool IsAutoScroll { get; set; }
3636

37+
/// <summary>
38+
/// 获得/设置 失去焦点回调方法 默认 null
39+
/// </summary>
40+
[Parameter]
41+
public Func<string?, Task>? OnBlurAsync { get; set; }
42+
3743
/// <summary>
3844
/// 获得 客户端是否自动滚屏标识
3945
/// </summary>
@@ -53,4 +59,15 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5359
await InvokeVoidAsync("execute", Id, "update");
5460
}
5561
}
62+
63+
/// <summary>
64+
/// OnBlur 方法
65+
/// </summary>
66+
protected virtual async Task OnBlur()
67+
{
68+
if (OnBlurAsync != null)
69+
{
70+
await OnBlurAsync(Value);
71+
}
72+
}
5673
}

test/UnitTest/Components/TextareaTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,21 @@ public void AutoScrollString_OK()
5353
cut.Instance.ScrollToTop();
5454
cut.Instance.ScrollToBottom();
5555
}
56+
57+
[Fact]
58+
public async Task OnBlurAsync_Ok()
59+
{
60+
var blur = false;
61+
var cut = Context.RenderComponent<Textarea>(builder =>
62+
{
63+
builder.Add(a => a.OnBlurAsync, v =>
64+
{
65+
blur = true;
66+
return Task.CompletedTask;
67+
});
68+
});
69+
var input = cut.Find("textarea");
70+
await cut.InvokeAsync(() => { input.Blur(); });
71+
Assert.True(blur);
72+
}
5673
}

0 commit comments

Comments
 (0)