Skip to content

Commit

Permalink
perf(DateTimePicker): update cache when click year button (#4381)
Browse files Browse the repository at this point in the history
* fix: 年视图切换时获得禁用日期数据

* test: 增加单元测试

* refactor: 更新单元测试
  • Loading branch information
ArgoZhang authored Sep 30, 2024
1 parent 3739e9b commit b18a550
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ private async Task OnClickPrevYear()
? GetSafeYearDateTime(CurrentDate, -20)
: GetSafeYearDateTime(CurrentDate, -1);

_render = false;
await UpdateDisabledDaysCache(false);
_render = true;

if (OnDateChanged != null)
{
await OnDateChanged(CurrentDate);
Expand Down Expand Up @@ -608,6 +612,10 @@ private async Task OnClickNextYear()
? GetSafeYearDateTime(CurrentDate, 20)
: GetSafeYearDateTime(CurrentDate, 1);

_render = false;
await UpdateDisabledDaysCache(false);
_render = true;

if (OnDateChanged != null)
{
await OnDateChanged(CurrentDate);
Expand Down
33 changes: 26 additions & 7 deletions test/UnitTest/Components/DateTimePickerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,31 +1177,32 @@ public void MinValueToEmpty_Ok()
public async Task OnGetDisabledDaysCallback_Ok()
{
var fetched = false;

var dtm = new DateTime(2024, 9, 25);
// 禁用当天
var cut = Context.RenderComponent<DateTimePicker<DateTime?>>(pb =>
{
pb.Add(a => a.OnGetDisabledDaysCallback, async (start, end) =>
{
fetched = true;
await Task.Yield();
return [DateTime.Today];
return [dtm];
});
pb.Add(a => a.Value, DateTime.Today);
pb.Add(a => a.Value, dtm);
});

// 组件值为 null
Assert.True(fetched);
Assert.Equal(DateTime.Today, cut.Instance.Value);
Assert.Equal(dtm, cut.Instance.Value);

fetched = false;
// 设置组件值不为当前天
// 相同月数据已缓存不会触发回调
fetched = false;
cut.SetParametersAndRender(pb =>
{
pb.Add(a => a.Value, DateTime.Today.AddDays(1));
pb.Add(a => a.Value, dtm.AddDays(1));
});
Assert.False(fetched);
Assert.Equal(DateTime.Today.AddDays(1), cut.Instance.Value);
Assert.Equal(dtm.AddDays(1), cut.Instance.Value);

// 禁用缓存
// 每次组件渲染都会触发回调
Expand All @@ -1210,11 +1211,29 @@ public async Task OnGetDisabledDaysCallback_Ok()
pb.Add(a => a.EnableDisabledDaysCache, false);
});

// 获得所有按钮
var buttons = cut.FindAll(".picker-panel-header button");

// 下一月
await cut.InvokeAsync(() => buttons[2].Click());
Assert.True(fetched);

// 上一月
// 数据已缓存不会触发回调
fetched = false;
await cut.InvokeAsync(() => buttons[1].Click());
Assert.False(fetched);

// 上一年
await cut.InvokeAsync(() => buttons[0].Click());
Assert.True(fetched);

// 下一年
// 数据已缓存不会触发回调
fetched = false;
await cut.InvokeAsync(() => buttons[3].Click());
Assert.False(fetched);

// 调用清除缓存方法
cut.Instance.ClearDisabledDays();
}
Expand Down

0 comments on commit b18a550

Please sign in to comment.