Skip to content

Commit

Permalink
feat(DateTimePicker): add OnBlurAsync parameter (#4529)
Browse files Browse the repository at this point in the history
* feat: 增加 OnBlurAsync 参数

* test: 更新单元测试
  • Loading branch information
ArgoZhang authored Oct 24, 2024
1 parent 7bc5225 commit cf30213
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<BootstrapLabel required="@Required" for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
}
<div @attributes="@AdditionalAttributes" tabindex="@TabIndexString" id="@Id" class="@ClassString" data-bb-dropdown=".picker-panel" data-bb-dismiss=".picker-panel-link-btn">
<input readonly="@ReadonlyString" class="@InputClassName" @bind="@CurrentValueAsString" placeholder="@PlaceholderString" disabled="@Disabled" data-bs-toggle="@Constants.DropdownToggleString" data-bs-placement="@PlacementString" data-bs-custom-class="@CustomClassString" />
<input readonly="@ReadonlyString" class="@InputClassName" @bind="@CurrentValueAsString" placeholder="@PlaceholderString" disabled="@Disabled" data-bs-toggle="@Constants.DropdownToggleString" data-bs-placement="@PlacementString" data-bs-custom-class="@CustomClassString" @onblur="OnBlur" />
@if (ShowIcon)
{
<i class="@DateTimePickerIconClassString"></i>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ public string? Format
[Parameter]
public bool DisplayDisabledDayAsEmpty { get; set; }

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

[Inject]
[NotNull]
private IStringLocalizer<DateTimePicker<DateTime>>? Localizer { get; set; }
Expand Down Expand Up @@ -437,4 +443,15 @@ protected override bool TryParseValueFromString(string value, [MaybeNullWhen(fal
}

private string? ReadonlyString => IsEditable ? null : "readonly";

/// <summary>
/// <inheritdoc/>
/// </summary>
protected virtual async Task OnBlur()
{
if (OnBlurAsync != null)
{
await OnBlurAsync(Value);
}
}
}
17 changes: 17 additions & 0 deletions test/UnitTest/Components/DateTimePickerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,23 @@ public async Task OnGetDisabledDaysCallback_Ok()
cut.Instance.ClearDisabledDays();
}

[Fact]
public async Task OnBlurAsync_Ok()
{
var blur = false;
var cut = Context.RenderComponent<DateTimePicker<DateTime>>(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);
}

class MockDateTimePicker : DatePickerBody
{
public static bool GetSafeYearDateTime_Ok()
Expand Down

0 comments on commit cf30213

Please sign in to comment.