Skip to content

Commit

Permalink
[TimePicker] Add null check for empty string (#2245)
Browse files Browse the repository at this point in the history
* Add null check

* Add UNIT-Test

* Remove Newtonsoft.Json.Linq using

* use if, else if, else instead

* Update UNIT-Test

---------

Co-authored-by: Vincent Baaij <[email protected]>
Co-authored-by: Denis Voituron <[email protected]>
  • Loading branch information
3 people authored Jun 24, 2024
1 parent 4842438 commit 119657d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Core/Components/DateTime/FluentTimePicker.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ protected override bool TryParseValueFromString(string? value, out DateTime? res
{
DateTime currentValue = Value ?? DateTime.MinValue;

if (value != null && DateTime.TryParse(value, out var valueConverted))
if (string.IsNullOrWhiteSpace(value))
{
result = null;
}
else if (value != null && DateTime.TryParse(value, out var valueConverted))
{
result = currentValue.Date + valueConverted.TimeOfDay;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Core/DateTime/FluentTimePickerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ public void FluentTimePicker_TryParseValueFromString(string? value, string? time
}
}

[Fact]
public void FluentTimePicker_EmptyStringToNull()
{
// Arrange
var picker = new TestTimePicker()
{
Value = System.DateTime.Today, // Default date
};

// Set a value
picker.CallTryParseValueFromString("01:23", out var initialDate, out var _);
Assert.Equal("01:23:00", initialDate?.ToString("HH:mm:ss"));

// Reset
picker.CallTryParseValueFromString(string.Empty, out var resultDate, out var _);
Assert.Null(resultDate?.ToString("HH:mm:ss"));
}

// Temporary class to expose protected method
private class TestTimePicker : FluentTimePicker
{
Expand Down

0 comments on commit 119657d

Please sign in to comment.