mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-05 05:27:37 +08:00
fix(module: datepicker): DatePicker DisabledDate works incorect (#1298)
* fix(module: datepicker): DatePicker DisabledDate works incorect * test(module: dateHelperTests): modify test case Co-authored-by: James Yeung <shunjiey@hotmail.com>
This commit is contained in:
parent
3bf616b735
commit
32d1f27708
@ -6,6 +6,8 @@ namespace AntDesign
|
||||
public static class DateHelper
|
||||
{
|
||||
private static readonly System.Globalization.Calendar _calendar = CultureInfo.InvariantCulture.Calendar;
|
||||
private const int DECADE_YEAR_COUNT = 10;
|
||||
private const int QUARTER_MONTH_COUNT = 3;
|
||||
|
||||
public static bool IsSameDate(DateTime date, DateTime compareDate)
|
||||
{
|
||||
@ -43,10 +45,21 @@ namespace AntDesign
|
||||
|
||||
public static string GetDayOfQuarter(DateTime date)
|
||||
{
|
||||
int offset = date.Month % 3 > 0 ? 1 : 0;
|
||||
int quarter = date.Month / 3 + offset;
|
||||
return $"Q{GetQuarter(date)}";
|
||||
}
|
||||
|
||||
return $"Q{quarter}";
|
||||
public static int GetQuarter(DateTime date)
|
||||
{
|
||||
int offset = date.Month % QUARTER_MONTH_COUNT > 0 ? 1 : 0;
|
||||
int quarter = date.Month / QUARTER_MONTH_COUNT + offset;
|
||||
|
||||
return quarter;
|
||||
}
|
||||
|
||||
public static DateTime GetStartDateOfQuarter(DateTime date)
|
||||
{
|
||||
int quarter = GetQuarter(date);
|
||||
return new DateTime(date.Year, 1 + ((quarter - 1) * QUARTER_MONTH_COUNT), 1);
|
||||
}
|
||||
|
||||
public static int GetWeekOfYear(DateTime date)
|
||||
@ -54,6 +67,78 @@ namespace AntDesign
|
||||
return _calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// for example,
|
||||
/// when currentDateTime is 2020-01-04 05:34:55 then:
|
||||
/// the next date shouble be 2030-01-01 00:00:00, it's the start date of next 10 years
|
||||
///
|
||||
/// when currentDateTime is 2023-01-04 05:34:55 then:
|
||||
/// the next date shouble be 2030-01-01 00:00:00, it's the start date of next 10 years
|
||||
///
|
||||
/// when currentDateTime is 2018-01-04 05:34:55 then:
|
||||
/// the next date shouble be 2020-01-01 00:00:00, it's the start date of next 10 years
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetNextStartDateOfDecade(DateTime date)
|
||||
{
|
||||
int year = date.Year / DECADE_YEAR_COUNT * DECADE_YEAR_COUNT;
|
||||
|
||||
if (year < DateTime.MinValue.Year)
|
||||
{
|
||||
year = DateTime.MinValue.Year;
|
||||
}
|
||||
|
||||
return AddYearsSafely(new DateTime(year, 1, 1), DECADE_YEAR_COUNT);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// for example, when currentDateTime is 2020-01-04 05:34:55 then:
|
||||
/// the next date shouble be 2021-01-01 00:00:00, it's the start date of next year
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetNextStartDateOfYear(DateTime date)
|
||||
{
|
||||
return AddYearsSafely(new DateTime(date.Year, 1, 1), 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// for example, when currentDateTime is 2020-01-04 05:34:55 then:
|
||||
/// the next date shouble be 2020-04-01 00:00:00, it's the start date of the next quarter in 2020
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetNextStartDateOfQuarter(DateTime date)
|
||||
{
|
||||
var nextQuarterDate = AddMonthsSafely(new DateTime(date.Year, date.Month, 1), QUARTER_MONTH_COUNT);
|
||||
|
||||
return GetStartDateOfQuarter(nextQuarterDate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// for example, when currentDateTime is 2020-01-04 05:34:55 then:
|
||||
/// the next date shouble be 2020-02-01 00:00:00 , it's the start date of next month
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetNextStartDateOfMonth(DateTime date)
|
||||
{
|
||||
return AddMonthsSafely(new DateTime(date.Year, date.Month, 1), 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// for example, when currentDateTime is 2020-01-04 05:34:55 then:
|
||||
/// the next date shouble be 2021-01-05 00:00:00, it's the start date of next day
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetNextStartDateOfDay(DateTime date)
|
||||
{
|
||||
return AddDaysSafely(new DateTime(date.Year, date.Month, date.Day), 1);
|
||||
}
|
||||
|
||||
|
||||
public static DateTime CombineNewDate(
|
||||
DateTime date,
|
||||
int? year = null,
|
||||
|
@ -174,12 +174,7 @@ namespace AntDesign.Internal
|
||||
string selectedCls = isSelected ? $"{PrefixCls}-cell-selected" : "";
|
||||
string inRangeCls = isInRange ? $"{PrefixCls}-cell-in-range" : "";
|
||||
|
||||
string disabledCls = "";
|
||||
if (DisabledDate != null && DisabledDate(currentColDate))
|
||||
{
|
||||
disabledCls = $"{PrefixCls}-cell-disabled";
|
||||
}
|
||||
|
||||
string disabledCls = GetDisabledCls(currentColDate);
|
||||
string rangeStartCls = GetRangeStartCls(currentColDate);
|
||||
string rangeEndCls = GetRangeEndCls(currentColDate);
|
||||
string rangeHoverCls = GetRangeHoverCls(currentColDate);
|
||||
@ -418,6 +413,20 @@ namespace AntDesign.Internal
|
||||
return cls.ToString();
|
||||
}
|
||||
|
||||
private string GetDisabledCls(DateTime currentColDate)
|
||||
{
|
||||
string disabledCls = "";
|
||||
|
||||
var nextStartDate = GetNextStartDate(currentColDate);
|
||||
|
||||
if (DisabledDate?.Invoke(DateHelper.AddDaysSafely(nextStartDate, -1)) == true)
|
||||
{
|
||||
disabledCls = $"{PrefixCls}-cell-disabled";
|
||||
}
|
||||
|
||||
return disabledCls;
|
||||
}
|
||||
|
||||
private DateTime? FormatDateByPicker(DateTime? dateTime)
|
||||
{
|
||||
return DateHelper.FormatDateByPicker(dateTime, Picker);
|
||||
@ -459,6 +468,21 @@ namespace AntDesign.Internal
|
||||
};
|
||||
}
|
||||
|
||||
private DateTime GetNextStartDate(DateTime currentDateTime)
|
||||
{
|
||||
return Picker switch
|
||||
{
|
||||
DatePickerType.Decade => DateHelper.GetNextStartDateOfDecade(currentDateTime),
|
||||
DatePickerType.Year => DateHelper.GetNextStartDateOfYear(currentDateTime),
|
||||
DatePickerType.Quarter => DateHelper.GetNextStartDateOfQuarter(currentDateTime),
|
||||
DatePickerType.Month => DateHelper.GetNextStartDateOfMonth(currentDateTime),
|
||||
DatePickerType.Week => DateHelper.GetNextStartDateOfDay(currentDateTime),
|
||||
DatePickerType.Date => DateHelper.GetNextStartDateOfDay(currentDateTime),
|
||||
DatePickerType.Time => DateHelper.GetNextStartDateOfDay(currentDateTime),
|
||||
_ => currentDateTime,
|
||||
};
|
||||
}
|
||||
|
||||
private bool ShouldStopRenderDate(DateTime preDate, DateTime nextDate)
|
||||
{
|
||||
return Picker switch
|
||||
|
100
tests/Core/Helpers/DateHelperTests.cs
Normal file
100
tests/Core/Helpers/DateHelperTests.cs
Normal file
@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace AntDesign.Tests.Core.Helpers
|
||||
{
|
||||
public class DateHelperTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(GetNextStartDateOfDecade_Values))]
|
||||
public void GetNextStartDateOfDecade(DateTime currentDateTime, DateTime exceptedDateTime)
|
||||
{
|
||||
Assert.Equal(exceptedDateTime, DateHelper.GetNextStartDateOfDecade(currentDateTime));
|
||||
}
|
||||
|
||||
public static List<object[]> GetNextStartDateOfDecade_Values => new()
|
||||
{
|
||||
new object[] { DateTime.Parse("2020-01-04 05:34:55"), DateTime.Parse("2030-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("2018-01-04 05:34:55"), DateTime.Parse("2020-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("2015-07-24 05:34:55"), DateTime.Parse("2020-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1800-07-24 05:34:55"), DateTime.Parse("1810-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1867-07-24 05:34:55"), DateTime.Parse("1870-01-01 00:00:00") },
|
||||
new object[] { DateTime.MinValue, DateTime.MinValue.AddYears(10) },
|
||||
new object[] { DateTime.MaxValue, DateTime.Parse($"{DateTime.MaxValue.Year}-01-01 00:00:00") },
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetNextStartDateOfYear_Values))]
|
||||
public void GetNextStartDateOfYear(DateTime currentDateTime, DateTime exceptedDateTime)
|
||||
{
|
||||
Assert.Equal(exceptedDateTime, DateHelper.GetNextStartDateOfYear(currentDateTime));
|
||||
}
|
||||
|
||||
public static List<object[]> GetNextStartDateOfYear_Values => new()
|
||||
{
|
||||
new object[] { DateTime.Parse("2020-01-04 05:34:55"), DateTime.Parse("2021-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("2018-01-04 05:34:55"), DateTime.Parse("2019-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("2015-07-24 05:34:55"), DateTime.Parse("2016-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1800-07-24 05:34:55"), DateTime.Parse("1801-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1867-07-24 05:34:55"), DateTime.Parse("1868-01-01 00:00:00") },
|
||||
new object[] { DateTime.MinValue, DateTime.MinValue.AddYears(1) },
|
||||
new object[] { DateTime.MaxValue, DateTime.Parse($"{DateTime.MaxValue.Year}-01-01 00:00:00") },
|
||||
};
|
||||
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetNextStartDateOfQuarter_Values))]
|
||||
public void GetNextStartDateOfQuarter(DateTime currentDateTime, DateTime exceptedDateTime)
|
||||
{
|
||||
Assert.Equal(exceptedDateTime, DateHelper.GetNextStartDateOfQuarter(currentDateTime));
|
||||
}
|
||||
|
||||
public static List<object[]> GetNextStartDateOfQuarter_Values => new()
|
||||
{
|
||||
new object[] { DateTime.Parse("2020-01-04 05:34:55"), DateTime.Parse("2020-04-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("2018-01-04 05:34:55"), DateTime.Parse("2018-04-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("2015-07-24 05:34:55"), DateTime.Parse("2015-10-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1800-11-24 05:34:55"), DateTime.Parse("1801-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1867-07-24 05:34:55"), DateTime.Parse("1867-10-01 00:00:00") },
|
||||
new object[] { DateTime.MinValue, DateTime.MinValue.AddMonths(3) },
|
||||
new object[] { DateTime.MaxValue, DateTime.Parse($"{DateTime.MaxValue.Year}-10-01 00:00:00") },
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetNextStartDateOfMonth_Values))]
|
||||
public void GetNextStartDateOfMonth(DateTime currentDateTime, DateTime exceptedDateTime)
|
||||
{
|
||||
Assert.Equal(exceptedDateTime, DateHelper.GetNextStartDateOfMonth(currentDateTime));
|
||||
}
|
||||
|
||||
public static List<object[]> GetNextStartDateOfMonth_Values => new()
|
||||
{
|
||||
new object[] { DateTime.Parse("2020-01-04 05:34:55"), DateTime.Parse("2020-02-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("2018-01-04 05:34:55"), DateTime.Parse("2018-02-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("2015-07-24 05:34:55"), DateTime.Parse("2015-08-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1800-12-24 05:34:55"), DateTime.Parse("1801-01-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1867-07-24 05:34:55"), DateTime.Parse("1867-08-01 00:00:00") },
|
||||
new object[] { DateTime.MinValue, DateTime.MinValue.AddMonths(1) },
|
||||
new object[] { DateTime.MaxValue, DateTime.Parse($"{DateTime.MaxValue.Year}-12-01 00:00:00") },
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetNextStartDateOfDay_Values))]
|
||||
public void GetNextStartDateOfDay(DateTime currentDateTime, DateTime exceptedDateTime)
|
||||
{
|
||||
Assert.Equal(exceptedDateTime, DateHelper.GetNextStartDateOfDay(currentDateTime));
|
||||
}
|
||||
|
||||
public static List<object[]> GetNextStartDateOfDay_Values => new()
|
||||
{
|
||||
new object[] { DateTime.Parse("2020-01-04 05:34:55"), DateTime.Parse("2020-01-05 00:00:00") },
|
||||
new object[] { DateTime.Parse("2018-01-04 05:34:55"), DateTime.Parse("2018-01-05 00:00:00") },
|
||||
new object[] { DateTime.Parse("2015-07-31 05:34:55"), DateTime.Parse("2015-08-01 00:00:00") },
|
||||
new object[] { DateTime.Parse("1800-07-24 05:34:55"), DateTime.Parse("1800-07-25 00:00:00") },
|
||||
new object[] { DateTime.Parse("1867-12-24 05:34:55"), DateTime.Parse("1867-12-25 00:00:00") },
|
||||
new object[] { DateTime.MinValue, DateTime.MinValue.AddDays(1) },
|
||||
new object[] { DateTime.MaxValue, DateTime.Parse($"{DateTime.MaxValue.Year}-12-31 00:00:00") },
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user