mirror of
https://github.com/radzenhq/radzen-blazor.git
synced 2026-04-05 22:01:04 +00:00
Setting a Min value in the DatePicker to any time on today's date disabled the current day
Fix #2516
This commit is contained in:
@@ -1378,5 +1378,50 @@ namespace Radzen.Blazor.Tests
|
||||
Assert.False(string.IsNullOrEmpty(formattedValue));
|
||||
Assert.Contains("2567", formattedValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DatePicker_ShowTime_MinToday_DoesNotDisableToday()
|
||||
{
|
||||
using var ctx = new TestContext();
|
||||
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
|
||||
ctx.JSInterop.SetupModule("_content/Radzen.Blazor/Radzen.Blazor.js");
|
||||
|
||||
var today = DateTime.Now;
|
||||
var min = new DateTime(today.Year, today.Month, today.Day, 14, 0, 0);
|
||||
|
||||
var component = ctx.RenderComponent<RadzenDatePicker<DateTime>>(parameters =>
|
||||
{
|
||||
parameters.Add(p => p.Value, today);
|
||||
parameters.Add(p => p.ShowTime, true);
|
||||
parameters.Add(p => p.Min, min);
|
||||
});
|
||||
|
||||
// Find today's cell - it should not have rz-state-disabled
|
||||
var todayCell = component.FindAll("td span.rz-calendar-today");
|
||||
Assert.NotEmpty(todayCell);
|
||||
Assert.DoesNotContain("rz-state-disabled", todayCell.First().ClassName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DatePicker_NoShowTime_MinToday_DisablesToday()
|
||||
{
|
||||
using var ctx = new TestContext();
|
||||
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
|
||||
ctx.JSInterop.SetupModule("_content/Radzen.Blazor/Radzen.Blazor.js");
|
||||
|
||||
var today = DateTime.Now;
|
||||
var min = new DateTime(today.Year, today.Month, today.Day, 14, 0, 0);
|
||||
|
||||
var component = ctx.RenderComponent<RadzenDatePicker<DateTime>>(parameters =>
|
||||
{
|
||||
parameters.Add(p => p.Value, today);
|
||||
parameters.Add(p => p.Min, min);
|
||||
});
|
||||
|
||||
// Without ShowTime, today should be disabled since midnight < 14:00
|
||||
var todayCell = component.FindAll("td span.rz-calendar-today");
|
||||
Assert.NotEmpty(todayCell);
|
||||
Assert.Contains("rz-state-disabled", todayCell.First().ClassName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,8 +219,25 @@ namespace Radzen.Blazor
|
||||
DateTime? _valueBeforeTimeEdit;
|
||||
bool _hasUncommittedTimeChange;
|
||||
|
||||
DateTime ClampToMinMax(DateTime value)
|
||||
{
|
||||
if (Min.HasValue && value < Min.Value)
|
||||
{
|
||||
return Min.Value;
|
||||
}
|
||||
|
||||
if (Max.HasValue && value > Max.Value)
|
||||
{
|
||||
return Max.Value;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
async Task UpdateValueFromTime(DateTime newValue)
|
||||
{
|
||||
newValue = ClampToMinMax(newValue);
|
||||
|
||||
if (ShowTimeOkButton)
|
||||
{
|
||||
if (!_hasUncommittedTimeChange)
|
||||
@@ -573,7 +590,18 @@ namespace Radzen.Blazor
|
||||
|
||||
DateRenderEventArgs DateAttributes(DateTime value)
|
||||
{
|
||||
var args = new DateRenderEventArgs() { Date = value, Disabled = (Min.HasValue && value < Min.Value) || (Max.HasValue && value > Max.Value) };
|
||||
bool disabled;
|
||||
|
||||
if (ShowTime)
|
||||
{
|
||||
disabled = (Min.HasValue && value.Date < Min.Value.Date) || (Max.HasValue && value.Date > Max.Value.Date);
|
||||
}
|
||||
else
|
||||
{
|
||||
disabled = (Min.HasValue && value < Min.Value) || (Max.HasValue && value > Max.Value);
|
||||
}
|
||||
|
||||
var args = new DateRenderEventArgs() { Date = value, Disabled = disabled };
|
||||
|
||||
if (DateRender != null)
|
||||
{
|
||||
@@ -1468,12 +1496,12 @@ namespace Radzen.Blazor
|
||||
}
|
||||
else if (ShowTimeOkButton)
|
||||
{
|
||||
CurrentDate = new DateTime(newValue.Year, newValue.Month, newValue.Day, CurrentDate.Hour, CurrentDate.Minute, CurrentDate.Second);
|
||||
CurrentDate = ClampToMinMax(new DateTime(newValue.Year, newValue.Month, newValue.Day, CurrentDate.Hour, CurrentDate.Minute, CurrentDate.Second));
|
||||
await OnOkClick(!ShowTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
var v = new DateTime(newValue.Year, newValue.Month, newValue.Day, CurrentDate.Hour, CurrentDate.Minute, CurrentDate.Second);
|
||||
var v = ClampToMinMax(new DateTime(newValue.Year, newValue.Month, newValue.Day, CurrentDate.Hour, CurrentDate.Minute, CurrentDate.Second));
|
||||
if (v != DateTimeValue)
|
||||
{
|
||||
DateTimeValue = v;
|
||||
|
||||
Reference in New Issue
Block a user