Skip to content

Commit

Permalink
fix(DateTimeRange): should be reset Value after close pop-up (#5413)
Browse files Browse the repository at this point in the history
* refactor: 增加 hideCallback 回调

* feat: 增加 TriggerHideCallback 回调

* test: 增加单元测试

* refactor: 移除日志输出

* refactor: 增加重置逻辑

* test: 增加单元测试

* chore: bump version 9.3.1-beta26

Co-Authored-By: Jiří Vokřínek <[email protected]>

---------

Co-authored-by: Jiří Vokřínek <[email protected]>
  • Loading branch information
ArgoZhang and jvk-digres authored Feb 21, 2025
1 parent c45b44c commit 1a219dc
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.3.1-beta25</Version>
<Version>9.3.1-beta26</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@namespace BootstrapBlazor.Components
@typeparam TValue
@inherits PopoverDropdownBase<TValue>
@attribute [BootstrapModuleAutoLoader("DateTimePicker/DateTimePicker.razor.js")]
@attribute [BootstrapModuleAutoLoader("DateTimePicker/DateTimePicker.razor.js", JSObjectReference = true)]

@if (IsShowLabel)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,15 @@ protected override string FormatValueAsString(TValue? value)
return ret;
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
{
TriggerHideCallback = nameof(TriggerHideCallback)
});

private bool MinValueToEmpty(DateTime val) => val == DateTime.MinValue && AllowNull && DisplayMinValueAsEmpty;

private bool MinValueToToday(DateTime val) => val == DateTime.MinValue && !AllowNull && AutoToday;
Expand Down Expand Up @@ -454,4 +463,15 @@ protected virtual async Task OnBlur()
await OnBlurAsync(Value);
}
}

/// <summary>
/// 客户端弹窗关闭后由 Javascript 调用此方法
/// </summary>
/// <returns></returns>
[JSInvokable]
public Task TriggerHideCallback()
{
StateHasChanged();
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import EventHandler from "../../modules/event-handler.js"
import Popover from "../../modules/base-popover.js"

export function init(id) {
export function init(id, invoke, options) {
const el = document.getElementById(id)
if (el == null) {
return
Expand All @@ -13,6 +13,9 @@ export function init(id) {
dropdownSelector: el.getAttribute('data-bb-dropdown'),
isDisabled: () => {
return el.classList.contains('disabled');
},
hideCallback: () => {
invoke?.invokeMethodAsync(options.triggerHideCallback);
}
});
const dateTimePicker = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@namespace BootstrapBlazor.Components
@inherits PopoverDropdownBase<DateTimeRangeValue>
@attribute [BootstrapModuleAutoLoader("DateTimePicker/DateTimePicker.razor.js")]
@attribute [BootstrapModuleAutoLoader("DateTimePicker/DateTimePicker.razor.js", JSObjectReference = true)]

@if (IsShowLabel)
{
Expand Down
61 changes: 43 additions & 18 deletions src/BootstrapBlazor/Components/DateTimeRange/DateTimeRange.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,7 @@ protected override void OnParametersSet()
new() { Text = Localizer["LastMonth"], StartDateTime = DateTime.Today.AddDays(1- DateTime.Today.Day).AddMonths(-1), EndDateTime = DateTime.Today.AddDays(1- DateTime.Today.Day).AddSeconds(-1) },
];

Value ??= new DateTimeRangeValue();
EndValue = Value.End == DateTime.MinValue ? GetEndDateTime(DateTime.Today) : Value.End;

if (ViewMode == DatePickerViewMode.Year)
{
var d = DateTime.Today.AddYears(-1);
StartValue = Value.Start == DateTime.MinValue ? new DateTime(d.Year, 1, 1) : Value.Start;
}
else if (ViewMode == DatePickerViewMode.Month)
{
var d = DateTime.Today.AddMonths(-1);
StartValue = Value.Start == DateTime.MinValue ? new DateTime(d.Year, d.Month, 1) : Value.Start;
}
else
{
StartValue = EndValue.AddMonths(-1).Date;
}

ResetBodyValue();
SelectedValue.Start = Value.Start;
SelectedValue.End = Value.End;

Expand All @@ -342,6 +325,15 @@ void CheckValid()
}
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
{
TriggerHideCallback = nameof(TriggerHideCallback)
});

private async Task OnClickSidebarItem(DateTimeRangeSidebarItem item)
{
SelectedValue.Start = item.StartDateTime;
Expand Down Expand Up @@ -505,4 +497,37 @@ private void UpdateValue(DateTime d)
public override bool IsComplexValue(object? propertyValue) => false;

private static DateTime GetEndDateTime(DateTime dt) => dt.Date.AddHours(23).AddMinutes(59).AddSeconds(59);

private void ResetBodyValue()
{
Value ??= new DateTimeRangeValue();
EndValue = Value.End == DateTime.MinValue ? GetEndDateTime(DateTime.Today) : Value.End;

if (ViewMode == DatePickerViewMode.Year)
{
var d = DateTime.Today.AddYears(-1);
StartValue = Value.Start == DateTime.MinValue ? new DateTime(d.Year, 1, 1) : Value.Start;
}
else if (ViewMode == DatePickerViewMode.Month)
{
var d = DateTime.Today.AddMonths(-1);
StartValue = Value.Start == DateTime.MinValue ? new DateTime(d.Year, d.Month, 1) : Value.Start;
}
else
{
StartValue = EndValue.AddMonths(-1).Date;
}
}

/// <summary>
/// 客户端弹窗关闭后由 Javascript 调用此方法
/// </summary>
/// <returns></returns>
[JSInvokable]
public Task TriggerHideCallback()
{
ResetBodyValue();
StateHasChanged();
return Task.CompletedTask;
}
}
15 changes: 13 additions & 2 deletions src/BootstrapBlazor/wwwroot/modules/base-popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const Popover = {
isDisabled: () => {
return isDisabled(el) || isDisabled(el.parentNode) || isDisabled(el.querySelector('.form-control'))
},
initCallback: null
initCallback: null,
hideCallback: null
},
...config || {}
...(config ?? {})
}
const createPopover = () => {
if (!popover.isDisabled()) {
Expand Down Expand Up @@ -69,6 +70,12 @@ const Popover = {
}
}

popover.triggerHideCallback = () => {
if (popover.hideCallback) {
popover.hideCallback();
};
}

if (popover.isPopover) {
popover.hasDisplayNone = false;

Expand Down Expand Up @@ -112,6 +119,8 @@ const Popover = {
popover.popover.tip.classList.remove('show');
el.classList.remove('show');
el.append(popover.toggleMenu);

popover.triggerHideCallback();
}

const active = e => {
Expand Down Expand Up @@ -176,6 +185,7 @@ const Popover = {
}

EventHandler.on(el, 'show.bs.dropdown', show)
EventHandler.on(el, 'hide.bs.dropdown', popover.triggerHideCallback)

popover.popover = bootstrap.Dropdown.getOrCreateInstance(popover.toggleElement);
}
Expand All @@ -200,6 +210,7 @@ const Popover = {
}
else {
EventHandler.off(popover.el, 'show.bs.dropdown')
EventHandler.off(popover.el, 'hide.bs.dropdown')
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/UnitTest/Components/DateTimePickerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,19 @@ class MockCalendarHolidayService : ICalendarHolidays
public bool IsWorkday(DateTime dt) => dt == new DateTime(2024, 4, 7);
}

[Fact]
public async Task TriggerHideCallback_Ok()
{
var cut = Context.RenderComponent<DateTimePicker<DateTime>>(pb =>
{
pb.Add(a => a.DayTemplate, dt => builder =>
{
builder.AddContent(0, "day-template");
});
});
await cut.InvokeAsync(() => cut.Instance.TriggerHideCallback());
}

[Fact]
public void DayTemplate_Ok()
{
Expand Down
10 changes: 10 additions & 0 deletions test/UnitTest/Components/DateTimeRangeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,14 @@ public async Task ViewMode_Month()
await cut.InvokeAsync(() => cells[0].Click());
await cut.InvokeAsync(() => cells[1].Click());
}

[Fact]
public async Task TriggerHideCallback_Ok()
{
var cut = Context.RenderComponent<DateTimeRange>(pb =>
{
pb.Add(a => a.Value, new DateTimeRangeValue());
});
await cut.InvokeAsync(() => cut.Instance.TriggerHideCallback());
}
}

0 comments on commit 1a219dc

Please sign in to comment.