Skip to content

Commit

Permalink
[DatePicker] Change OnSelectedDateAsync logic (#2233)
Browse files Browse the repository at this point in the history
* Fixes #2197

* Minify code

* Remove ??

* Add UNIT-Test

---------

Co-authored-by: Denis Voituron <dvoituron@outlook.com>
Co-authored-by: Vincent Baaij <vnbaaij@outlook.com>
  • Loading branch information
3 people committed Jun 20, 2024
1 parent b8e1aa5 commit 804ebc0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Core/Components/DateTime/FluentDatePicker.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ protected async Task OnSelectedDateAsync(DateTime? value)
{
Opened = false;

var updatedValue = Value?.TimeOfDay != TimeSpan.Zero
? (value ?? DateTime.MinValue).Date + Value?.TimeOfDay
: value;
DateTime? updatedValue = value;

if (Value is not null && value is not null)
{
updatedValue = Value?.TimeOfDay != TimeSpan.Zero
? value?.Date + Value?.TimeOfDay
: value;
}

await OnSelectedDateHandlerAsync(updatedValue);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Core/DateTime/FluentDatePickerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,27 @@ public void FluentCalendar_DisabledDate()
// Assert
calendar.Verify();
}

[Fact]
public void FluentDatePicker_ChangeValueWhenDefaultIsNull()
{
// Arrange
using var ctx = new TestContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
ctx.Services.AddSingleton(LibraryConfiguration);

// Act
var picker = ctx.RenderComponent<FluentDatePicker>(parameters =>
{
parameters.Add(p => p.Value, null);
});

var textField = picker.Find("fluent-text-field");

// Set a new date value
textField.Change("2022-03-12");

// Assert
Assert.Equal(System.DateTime.Parse("2022-03-12"), picker.Instance.Value);
}
}

0 comments on commit 804ebc0

Please sign in to comment.