From 16317d98054005ca21bd767713bab7dd94f3b83a Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 10 Mar 2022 19:10:56 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/DateTimeExtensions.cs | 112 ++++++++++++++++++ src/BootstrapBlazor/_Imports.razor | 1 + 2 files changed, 113 insertions(+) create mode 100644 src/BootstrapBlazor/Extensions/DateTimeExtensions.cs diff --git a/src/BootstrapBlazor/Extensions/DateTimeExtensions.cs b/src/BootstrapBlazor/Extensions/DateTimeExtensions.cs new file mode 100644 index 000000000..adc8fb9ba --- /dev/null +++ b/src/BootstrapBlazor/Extensions/DateTimeExtensions.cs @@ -0,0 +1,112 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +namespace BootstrapBlazor.Extensions; + +internal static class DateTimeExtensions +{ + public static DateTime GetSafeYearDateTime(this DateTime dt, int year) + { + var @base = dt; + if (year < 0) + { + if (DateTime.MinValue.AddYears(0 - year) < dt) + { + @base = dt.AddYears(year); + } + else + { + @base = DateTime.MinValue; + } + } + else if (year > 0) + { + if (DateTime.MaxValue.AddYears(0 - year) > dt) + { + @base = dt.AddYears(year); + } + else + { + @base = DateTime.MaxValue; + } + } + return @base; + } + + public static DateTime GetSafeMonthDateTime(this DateTime dt, int month) + { + var @base = dt; + if (month < 0) + { + if (DateTime.MinValue.AddMonths(0 - month) < dt) + { + @base = dt.AddMonths(month); + } + else + { + @base = DateTime.MinValue; + } + } + else if (month > 0) + { + if (DateTime.MaxValue.AddMonths(0 - month) > dt) + { + @base = dt.AddMonths(month); + } + else + { + @base = DateTime.MaxValue; + } + } + return @base; + } + + public static DateTime GetSafeDayDateTime(this DateTime dt, int day) + { + var @base = dt; + if (day < 0) + { + if (DateTime.MinValue.AddDays(0 - day) < dt) + { + @base = dt.AddDays(day); + } + else + { + @base = DateTime.MinValue; + } + } + else if (day > 0) + { + if (DateTime.MaxValue.AddDays(0 - day) > dt) + { + @base = dt.AddDays(day); + } + else + { + @base = DateTime.MaxValue; + } + } + return @base; + } + + /// + /// + /// + /// + /// + /// + public static bool IsOverflow(this DateTime dt, int day) + { + var ret = false; + if (day < 0) + { + ret = DateTime.MinValue.AddDays(0 - day) > dt; + } + else if (day > 0) + { + ret = DateTime.MaxValue.AddDays(0 - day) < dt; + } + return ret; + } +} diff --git a/src/BootstrapBlazor/_Imports.razor b/src/BootstrapBlazor/_Imports.razor index c344693f1..9e9234a5c 100644 --- a/src/BootstrapBlazor/_Imports.razor +++ b/src/BootstrapBlazor/_Imports.razor @@ -1,4 +1,5 @@ @using BootstrapBlazor.Components +@using BootstrapBlazor.Extensions @using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Routing -- Gitee From fcaf04fcdd205df9c53531fa4148be6250e62990 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 10 Mar 2022 19:11:24 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=E6=97=B6=E9=97=B4=E5=8A=A0=E5=87=8F?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DateTimePicker/DatePickerBody.razor | 15 ++++++--- .../DateTimePicker/DatePickerBody.razor.cs | 33 ++++++++++--------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor b/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor index f39e3bfec..37d006c70 100644 --- a/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor +++ b/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor @@ -60,22 +60,27 @@ @w } - @for (var week = StartDate; week < EndDate; week = week.AddDays(7)) + @for (var week = StartDate; week < EndDate; week = week.GetSafeDayDateTime(7)) { @for (var index = 0; index < 7; index++) { - var day = week.AddDays(index); + var day = week.GetSafeDayDateTime(index); var text = GetDayText(day.Day); - + var isOverflow = week.IsOverflow(index); +
- @if (IsDisabled(day)) + @if(isOverflow) + { + - + } + else if (IsDisabled(day)) { @text } else { - + }
diff --git a/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs b/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs index 204c1cd10..b4789b58c 100644 --- a/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs +++ b/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using BootstrapBlazor.Extensions; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; @@ -19,8 +20,8 @@ public sealed partial class DatePickerBody { get { - var d = CurrentDate.AddDays(1 - CurrentDate.Day); - d = d.AddDays(0 - (int)d.DayOfWeek); + var d = CurrentDate.GetSafeDayDateTime(1 - CurrentDate.Day); + d = d.GetSafeDayDateTime(0 - (int)d.DayOfWeek); return d; } } @@ -28,7 +29,7 @@ public sealed partial class DatePickerBody /// /// 获得/设置 日历框结束时间 /// - private DateTime EndDate => StartDate.AddDays(42); + private DateTime EndDate => StartDate.GetSafeDayDateTime(42); /// /// 获得/设置 当前日历框月份 @@ -55,17 +56,17 @@ public sealed partial class DatePickerBody /// /// 获得/设置 日期样式 /// - private string? GetDayClass(DateTime day) => CssBuilder.Default("") + private string? GetDayClass(DateTime day, bool overflow) => CssBuilder.Default("") .AddClass("prev-month", day.Month < CurrentDate.Month) .AddClass("next-month", day.Month > CurrentDate.Month) - .AddClass("current", day == OriginaValue && Ranger == null && day.Month == CurrentDate.Month) + .AddClass("current", day == OriginaValue && Ranger == null && day.Month == CurrentDate.Month && !overflow) .AddClass("start", Ranger != null && day == Ranger.SelectedValue.Start.Date) .AddClass("end", Ranger != null && day == Ranger.SelectedValue.End.Date) .AddClass("range", Ranger != null && CurrentDate.Month >= Ranger.SelectedValue.Start.Month && Ranger.SelectedValue.Start != DateTime.MinValue && Ranger.SelectedValue.End != DateTime.MinValue && day >= Ranger.SelectedValue.Start && day <= Ranger.SelectedValue.End) .AddClass("today", day == DateTime.Today) - .AddClass("disabled", IsDisabled(day)) + .AddClass("disabled", IsDisabled(day) || overflow) .Build(); private bool IsDisabled(DateTime day) => (MinValue != null && day < MinValue) || (MaxValue != null && day > MaxValue); @@ -379,7 +380,7 @@ public sealed partial class DatePickerBody private void OnClickPrevYear() { ShowTimePicker = false; - CurrentDate = CurrentViewModel == DatePickerViewModel.Year ? CurrentDate.AddYears(-20) : CurrentDate.AddYears(-1); + CurrentDate = CurrentViewModel == DatePickerViewModel.Year ? CurrentDate.GetSafeYearDateTime(-20) : CurrentDate.GetSafeYearDateTime(-1); Ranger?.UpdateStart(CurrentDate); } @@ -389,7 +390,7 @@ public sealed partial class DatePickerBody private void OnClickPrevMonth() { ShowTimePicker = false; - CurrentDate = CurrentDate.AddMonths(-1); + CurrentDate = CurrentDate.GetSafeMonthDateTime(-1); Ranger?.UpdateStart(CurrentDate); } @@ -399,7 +400,7 @@ public sealed partial class DatePickerBody private void OnClickNextYear() { ShowTimePicker = false; - CurrentDate = CurrentViewModel == DatePickerViewModel.Year ? CurrentDate.AddYears(20) : CurrentDate.AddYears(1); + CurrentDate = CurrentViewModel == DatePickerViewModel.Year ? CurrentDate.GetSafeYearDateTime(20) : CurrentDate.GetSafeYearDateTime(1); Ranger?.UpdateEnd(CurrentDate); } @@ -409,7 +410,7 @@ public sealed partial class DatePickerBody private void OnClickNextMonth() { ShowTimePicker = false; - CurrentDate = CurrentDate.AddMonths(1); + CurrentDate = CurrentDate.GetSafeMonthDateTime(1); Ranger?.UpdateEnd(CurrentDate); } @@ -473,7 +474,7 @@ public sealed partial class DatePickerBody /// private string GetYearPeriod() { - var start = CurrentDate.AddYears(0 - CurrentDate.Year % 20).Year; + var start = CurrentDate.GetSafeYearDateTime(0 - CurrentDate.Year % 20).Year; return string.Format(YearPeriodText, start, start + 19); } @@ -482,7 +483,7 @@ public sealed partial class DatePickerBody /// /// /// - private DateTime GetYear(int year) => CurrentDate.AddYears(year - (CurrentDate.Year % 20)); + private DateTime GetYear(int year) => CurrentDate.GetSafeYearDateTime(year - (CurrentDate.Year % 20)); /// /// 获取 年视图下月份单元格显示文字 @@ -496,8 +497,8 @@ public sealed partial class DatePickerBody /// /// private string? GetYearClassName(int year) => CssBuilder.Default() - .AddClass("current", CurrentDate.AddYears(year - (CurrentDate.Year % 20)).Year == Value.Year) - .AddClass("today", CurrentDate.AddYears(year - (CurrentDate.Year % 20)).Year == DateTime.Today.Year) + .AddClass("current", CurrentDate.GetSafeYearDateTime(year - (CurrentDate.Year % 20)).Year == Value.Year) + .AddClass("today", CurrentDate.GetSafeYearDateTime(year - (CurrentDate.Year % 20)).Year == DateTime.Today.Year) .Build(); /// @@ -505,7 +506,7 @@ public sealed partial class DatePickerBody /// /// /// - private DateTime GetMonth(int month) => CurrentDate.AddMonths(month - CurrentDate.Month); + private DateTime GetMonth(int month) => CurrentDate.GetSafeMonthDateTime(month - CurrentDate.Month); /// /// 获取 月视图下的月份单元格样式 @@ -521,7 +522,7 @@ public sealed partial class DatePickerBody /// /// /// - private static string? GetDayText(int day) => $"{day}"; + private static string GetDayText(int day) => day.ToString(); /// /// 获取 月视图下月份单元格显示文字 -- Gitee From ba34b1c66f09f1447a12599a17ad8ff22ec22693 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 10 Mar 2022 19:51:54 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=BA=A2=E5=87=BA=E5=88=A4=E6=96=AD=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/DateTimeExtensions.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Extensions/DateTimeExtensions.cs b/src/BootstrapBlazor/Extensions/DateTimeExtensions.cs index adc8fb9ba..88fbf278b 100644 --- a/src/BootstrapBlazor/Extensions/DateTimeExtensions.cs +++ b/src/BootstrapBlazor/Extensions/DateTimeExtensions.cs @@ -96,7 +96,7 @@ internal static class DateTimeExtensions /// /// /// - public static bool IsOverflow(this DateTime dt, int day) + public static bool IsDayOverflow(this DateTime dt, int day) { var ret = false; if (day < 0) @@ -109,4 +109,24 @@ internal static class DateTimeExtensions } return ret; } + + /// + /// + /// + /// + /// + /// + public static bool IsYearOverflow(this DateTime dt, int year) + { + var ret = false; + if (year < 0) + { + ret = DateTime.MinValue.AddYears(0 - year) > dt; + } + else if (year > 0) + { + ret = DateTime.MaxValue.AddYears(0 - year) < dt; + } + return ret; + } } -- Gitee From e2433b4af7f69cb98c7051c55155e7557fb34b36 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 10 Mar 2022 19:52:11 +0800 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=BA=A2=E5=87=BA=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DateTimePicker/DatePickerBody.razor | 26 ++++++++++++++----- .../DateTimePicker/DatePickerBody.razor.cs | 5 ++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor b/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor index 37d006c70..027a29b32 100644 --- a/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor +++ b/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor @@ -67,7 +67,7 @@ { var day = week.GetSafeDayDateTime(index); var text = GetDayText(day.Day); - var isOverflow = week.IsOverflow(index); + var isOverflow = week.IsDayOverflow(index);
@if(isOverflow) @@ -96,10 +96,21 @@ @for (var index = 0; index < 4; index++) { - -
- -
+ var year = index + row * 4; + var isOverflow = CurrentDate.IsYearOverflow(year - (CurrentDate.Year % 20)); + + @if(isOverflow) + { +
+ - +
+ } + else + { +
+ +
+ } } @@ -113,9 +124,10 @@ @for (var index = 0; index < 4; index++) { - + var month = index + row * 4 + 1; +
- +
} diff --git a/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs b/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs index b4789b58c..d356eb08f 100644 --- a/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs +++ b/src/BootstrapBlazor/Components/DateTimePicker/DatePickerBody.razor.cs @@ -490,15 +490,16 @@ public sealed partial class DatePickerBody ///
/// /// - private string? GetYearText(int year) => $"{GetYear(year).Year}"; + private string GetYearText(int year) => GetYear(year).Year.ToString(); /// /// 获取 年视图下的年份单元格样式 /// /// - private string? GetYearClassName(int year) => CssBuilder.Default() + private string? GetYearClassName(int year, bool overflow) => CssBuilder.Default() .AddClass("current", CurrentDate.GetSafeYearDateTime(year - (CurrentDate.Year % 20)).Year == Value.Year) .AddClass("today", CurrentDate.GetSafeYearDateTime(year - (CurrentDate.Year % 20)).Year == DateTime.Today.Year) + .AddClass("disabled", overflow) .Build(); /// -- Gitee From 3defae74b934794267b4b1f2713275638d98409f Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 10 Mar 2022 19:52:43 +0800 Subject: [PATCH 5/5] chore: bump version beta01 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 05e6e48b1..f20ff0f23 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 6.4.3-beta01 + 6.4.3-beta02 -- Gitee