Skip to content

Commit fd49fa0

Browse files
feat(DateTimePicker): add FirstDayOfWeek parameter (#5417)
* chore: bump version 9.3.1-beta28 Co-Authored-By: cervinkamichal0 <[email protected]> * feat: 增加 FirstDayOfWeek 设置 * test: 更新单元测试 --------- Co-authored-by: cervinkamichal0 <[email protected]>
1 parent 406eb5a commit fd49fa0

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.3.1-beta27</Version>
4+
<Version>9.3.1-beta28</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private DateTime StartDate
2020
get
2121
{
2222
var d = GetSafeDayDateTime(CurrentDate, 1 - CurrentDate.Day);
23-
d = GetSafeDayDateTime(d, 0 - (int)d.DayOfWeek);
23+
d = GetSafeDayDateTime(d, (int)FirstDayOfWeek - (int)d.DayOfWeek);
2424
return d;
2525
}
2626
}
@@ -388,6 +388,12 @@ public bool AllowNull
388388
[Parameter]
389389
public bool EnableDisabledDaysCache { get; set; } = true;
390390

391+
/// <summary>
392+
/// 获得/设置 星期第一天 默认 <see cref="DayOfWeek.Sunday"/>
393+
/// </summary>
394+
[Parameter]
395+
public DayOfWeek FirstDayOfWeek { get; set; } = DayOfWeek.Sunday;
396+
391397
[Inject]
392398
[NotNull]
393399
private ICalendarFestivals? CalendarFestivals { get; set; }
@@ -494,7 +500,7 @@ protected override void OnParametersSet()
494500
YearPeriodText ??= Localizer[nameof(YearPeriodText)];
495501
MonthLists = [.. Localizer[nameof(MonthLists)].Value.Split(',')];
496502
Months = [.. Localizer[nameof(Months)].Value.Split(',')];
497-
WeekLists = [.. Localizer[nameof(WeekLists)].Value.Split(',')];
503+
WeekLists = GetWeekList();
498504

499505
Today ??= Localizer[nameof(Today)];
500506
Yesterday ??= Localizer[nameof(Yesterday)];
@@ -513,7 +519,9 @@ protected override async Task OnParametersSetAsync()
513519
{
514520
await base.OnParametersSetAsync();
515521

522+
_render = false;
516523
await UpdateDisabledDaysCache(true);
524+
_render = true;
517525
}
518526

519527
private bool _render = true;
@@ -523,6 +531,15 @@ protected override async Task OnParametersSetAsync()
523531
/// </summary>
524532
protected override bool ShouldRender() => _render;
525533

534+
private List<string> GetWeekList()
535+
{
536+
var list = Localizer[nameof(WeekLists)].Value.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
537+
538+
// 调整顺序
539+
var firstDayIndex = (int)FirstDayOfWeek;
540+
return list.Skip(firstDayIndex).Concat(list.Take(firstDayIndex)).ToList();
541+
}
542+
526543
private async Task UpdateDisabledDaysCache(bool force)
527544
{
528545
if (OnGetDisabledDaysCallback != null)

src/BootstrapBlazor/Components/DateTimePicker/DateTimePicker.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
<i class="@DateTimePickerIconClassString"></i>
1515
}
16-
<DatePickerBody @bind-Value="SelectedValue" @ref="_pickerBody"
16+
<DatePickerBody @bind-Value="SelectedValue" @ref="_pickerBody" FirstDayOfWeek="FirstDayOfWeek"
1717
ShowClearButton="AllowNull" ShowSidebar="ShowSidebar" SidebarTemplate="SidebarTemplate"
1818
DateTimeFormat="@DateTimeFormat" DateFormat="@DateFormat" TimeFormat="@TimeFormat" ShowFooter="true"
1919
ShowLunar="ShowLunar" ShowSolarTerm="ShowSolarTerm" ShowFestivals="ShowFestivals" ShowHolidays="ShowHolidays"

src/BootstrapBlazor/Components/DateTimePicker/DateTimePicker.razor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ public string? Format
8686
[NotNull]
8787
public string? TimeFormat { get; set; }
8888

89+
/// <summary>
90+
/// 获得/设置 星期第一天 默认 <see cref="DayOfWeek.Sunday"/>
91+
/// </summary>
92+
[Parameter]
93+
public DayOfWeek FirstDayOfWeek { get; set; } = DayOfWeek.Sunday;
94+
8995
/// <summary>
9096
/// 获得/设置 组件图标 默认 fa-regular fa-calendar-days
9197
/// </summary>

test/UnitTest/Components/DateTimePickerTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,20 @@ public void SidebarTemplate_Ok()
959959
cut.Contains("test-sidebar-template");
960960
}
961961

962+
[Fact]
963+
public void FirstDayOfWeek_Ok()
964+
{
965+
var cut = Context.RenderComponent<DateTimePicker<DateTime>>(pb =>
966+
{
967+
pb.Add(a => a.FirstDayOfWeek, DayOfWeek.Monday);
968+
pb.Add(a => a.Value, new DateTime(2025, 02, 20));
969+
});
970+
971+
var labels = cut.FindAll(".date-table tbody > tr:first-child > th");
972+
Assert.Equal("一", labels[0].TextContent);
973+
Assert.Equal("日", labels[6].TextContent);
974+
}
975+
962976
[Fact]
963977
public void GetSafeYearDateTime_Ok()
964978
{

0 commit comments

Comments
 (0)