Skip to content

InputTime component with form support #26324

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/Components/Web/src/Forms/InputTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Microsoft.AspNetCore.Components.Rendering;

namespace Microsoft.AspNetCore.Components.Forms
{
/// <summary>
/// An input component for editing time values.
/// Supported types are <see cref="DateTime"/> and <see cref="DateTimeOffset"/>.
/// </summary>
public class InputTime<TValue> : InputBase<TValue>
{
private const string TimeFormat = "HH:mm:ss"; // Compatible with HTML time inputs

/// <summary>
/// Gets or sets the error message used when displaying an a parsing error.
/// </summary>
[Parameter] public string ParsingErrorMessage { get; set; } = "The {0} field must be a time.";

/// <inheritdoc />
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "input");
builder.AddMultipleAttributes(1, AdditionalAttributes);
builder.AddAttribute(2, "type", "time");
builder.AddAttribute(3, "class", CssClass);
builder.AddAttribute(4, "value", BindConverter.FormatValue(CurrentValueAsString));
builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder<string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
builder.CloseElement();
}

/// <inheritdoc />
protected override string FormatValueAsString(TValue? value)
{
switch (value)
{
case DateTime dateTimeValue:
return BindConverter.FormatValue(dateTimeValue, TimeFormat, CultureInfo.InvariantCulture);
case DateTimeOffset dateTimeOffsetValue:
return BindConverter.FormatValue(dateTimeOffsetValue, TimeFormat, CultureInfo.InvariantCulture);
default:
return string.Empty; // Handles null for Nullable<DateTime>, etc.
}
}

/// <inheritdoc />
protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(false)] out TValue result, [NotNullWhen(false)] out string? validationErrorMessage)
{
// Unwrap nullable types. We don't have to deal with receiving empty values for nullable
// types here, because the underlying InputBase already covers that.
var targetType = Nullable.GetUnderlyingType(typeof(TValue)) ?? typeof(TValue);

bool success;
if (targetType == typeof(DateTime))
{
success = TryParseDateTime(value, out result);
}
else if (targetType == typeof(DateTimeOffset))
{
success = TryParseDateTimeOffset(value, out result);
}
else
{
throw new InvalidOperationException($"The type '{targetType}' is not a supported time type.");
}

if (success)
{
Debug.Assert(result != null);
validationErrorMessage = null;
return true;
}
else
{
validationErrorMessage = string.Format(ParsingErrorMessage, DisplayName ?? FieldIdentifier.FieldName);
return false;
}
}

private static bool TryParseDateTime(string? value, [MaybeNullWhen(false)] out TValue result)
{
var success = BindConverter.TryConvertToDateTime(value, CultureInfo.InvariantCulture, TimeFormat, out var parsedValue);
if (success)
{
result = (TValue)(object)parsedValue;
return true;
}
else
{
result = default;
return false;
}
}

private static bool TryParseDateTimeOffset(string? value, [MaybeNullWhen(false)] out TValue result)
{
var success = BindConverter.TryConvertToDateTimeOffset(value, CultureInfo.InvariantCulture, TimeFormat, out var parsedValue);
if (success)
{
result = (TValue)(object)parsedValue;
return true;
}
else
{
result = default;
return false;
}
}
}
}