Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TimePicker] Add null check for empty string #2245

Merged
merged 10 commits into from
Jun 24, 2024
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()
vnbaaij marked this conversation as resolved.
Show resolved Hide resolved
{
// 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
Loading