Skip to content

Commit

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

* test: 增加单元测试

* test: 更新单元测试

* chore: bump version 8.3.4-beta05

* test: 更新单元测试
  • Loading branch information
ArgoZhang authored Mar 16, 2024
1 parent eee5c30 commit 044e665
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 8 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>8.3.4-beta04</Version>
<Version>8.3.4-beta05</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
}
else
{
<DatePickerCell Value="@GetSafeDayDateTime(week, index)" Text="@text" OnClick="d => OnClickDateTime(d)" Template="DayTemplate!" ShowLunar="ShowLunar" />
<DatePickerCell Value="@GetSafeDayDateTime(week, index)" Text="@text" OnClick="d => OnClickDateTime(d)" Template="DayTemplate!" ShowLunar="ShowLunar" ShowSolarTerm="ShowSolarTerm" />
}
</td>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ public bool AllowNull
[Parameter]
public bool ShowLunar { get; set; }

/// <summary>
/// 获得/设置 是否显示中国 24 节气 默认 false
/// </summary>
[Parameter]
public bool ShowSolarTerm { get; set; }

/// <summary>
/// 获得/设置 是否为 Range 内使用 默认为 false
/// </summary>
Expand Down Expand Up @@ -839,5 +845,5 @@ protected static bool IsYearOverflow(DateTime dt, int year)
return ret;
}

private static string GetLunarText(DateTime dateTime) => dateTime.ToLunarText();
private string GetLunarText(DateTime dateTime) => dateTime.ToLunarText(ShowSolarTerm);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ public sealed partial class DatePickerCell
[Parameter]
public bool ShowLunar { get; set; }

private static string GetLunarText(DateTime dateTime) => dateTime.ToLunarText();
/// <summary>
/// 获得/设置 是否显示中国 24 节气 默认 false
/// </summary>
[Parameter]
public bool ShowSolarTerm { get; set; }

private string GetLunarText(DateTime dateTime) => dateTime.ToLunarText(ShowSolarTerm);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<i class="@DateTimePickerIconClassString"></i>
}
<DatePickerBody @bind-Value="SelectedValue" ShowClearButton="AllowNull" ShowSidebar="ShowSidebar" SidebarTemplate="SidebarTemplate"
DateTimeFormat="@DateTimeFormat" DateFormat="@DateFormat" TimeFormat="@TimeFormat" ShowFooter="true" ShowLunar="ShowLunar"
DateTimeFormat="@DateTimeFormat" DateFormat="@DateFormat" TimeFormat="@TimeFormat" ShowFooter="true" ShowLunar="ShowLunar" ShowSolarTerm="ShowSolarTerm"
OnConfirm="OnConfirm" OnClear="OnClear" MinValue="MinValue" MaxValue="MaxValue"
AutoClose="AutoClose" ViewMode="ViewMode" DayTemplate="DayTemplate!" DayDisabledTemplate="DayDisabledTemplate!">
@ChildContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ public string? Format
[Parameter]
public bool ShowLunar { get; set; }

/// <summary>
/// 获得/设置 是否显示中国 24 节气 默认 false
/// </summary>
[Parameter]
public bool ShowSolarTerm { get; set; }

[Inject]
[NotNull]
private IStringLocalizer<DateTimePicker<DateTime>>? Localizer { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion src/BootstrapBlazor/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ public static DateTime GetSafeMonthDateTime(this DateTime dt, int month)
/// 获得阴历时间
/// </summary>
/// <param name="dt"></param>
/// <param name="showSolarTerm"></param>
/// <returns></returns>
public static string ToLunarText(this DateTime dt) => dt.GetSolarTermName() ?? dt.GetLunarMonthName();
public static string ToLunarText(this DateTime dt, bool showSolarTerm = false) => showSolarTerm
? dt.GetSolarTermName() ?? dt.GetLunarMonthName()
: dt.GetLunarMonthName();

static string GetLunarMonthName(this DateTime dt)
{
Expand Down
51 changes: 51 additions & 0 deletions test/UnitTest/Components/DateTimePickerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,57 @@ public void ShowLunar_Ok()
cut.Contains("闰二月");
}

[Fact]
public void ShowSolarTerm_Ok()
{
var cut = Context.RenderComponent<DateTimePicker<DateTime>>(pb =>
{
pb.Add(a => a.ShowLunar, true);
pb.Add(a => a.ShowSolarTerm, true);
pb.Add(a => a.Value, new DateTime(2024, 3, 5));
});

cut.Contains("惊蛰");

cut.SetParametersAndRender(pb =>
{
pb.Add(a => a.Value, new DateTime(2023, 2, 20));
});
cut.Contains("二月");
}

[Fact]
public void DayTemplate_Ok()
{
var cut = Context.RenderComponent<DateTimePicker<DateTime>>(pb =>
{
pb.Add(a => a.DayTemplate, dt => builder =>
{
builder.AddContent(0, "day-template");
});
});

cut.Contains("day-template");
}

[Fact]
public void DayDisabledTemplate_Ok()
{
var cut = Context.RenderComponent<DateTimePicker<DateTime>>(pb =>
{
pb.Add(a => a.DayDisabledTemplate, dt => builder =>
{
builder.AddContent(0, "day-disabled-template");
});
pb.Add(a => a.MinValue, new DateTime(2024, 3, 7));
pb.Add(a => a.MaxValue, new DateTime(2024, 3, 17));
pb.Add(a => a.Value, new DateTime(2024, 3, 10));
});

cut.Contains("day-disabled-template");
cut.SetParametersAndRender(pb => pb.Add(a => a.ShowLunar, true));
}

[Fact]
public void DatePickerViewModel_Ok()
{
Expand Down
4 changes: 2 additions & 2 deletions test/UnitTest/Components/LayoutTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public void NotAuthorized_Ok()
}

[Fact]
public void OnUpdateAsync_Ok()
public async Task OnUpdateAsync_Ok()
{
var updated = false;
var cut = Context.RenderComponent<Layout>(pb =>
Expand All @@ -314,7 +314,7 @@ public void OnUpdateAsync_Ok()
return Task.CompletedTask;
});
});
cut.InvokeAsync(() => cut.Instance.UpdateAsync("Test"));
await cut.InvokeAsync(() => cut.Instance.UpdateAsync("Test"));
Assert.True(updated);
}

Expand Down

0 comments on commit 044e665

Please sign in to comment.