Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<FluentTimePicker @bind-Value="@SelectedValue" Label="Meeting time:" />
<p>Selected Time: @(SelectedValue?.ToString("HH:mm"))</p>
<FluentTimePicker @bind-Value="@SelectedValue" Label="Meeting time:" />
<FluentTimePicker @bind-Value="@SelectedValue" Label="With seconds:" TimeDisplay="@TimeDisplay.HourMinuteSeconds" />

<p>Selected Time: @(SelectedValue?.ToString("HH:mm:ss"))</p>

@code
{
private DateTime? SelectedValue = null;
}
}
2 changes: 1 addition & 1 deletion src/Core/Components/DateTime/FluentTimePicker.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
autofocus="@Autofocus"
appearance="@Appearance.ToAttributeValue()"
type="time"
current-value="@Value?.ToString("HH:mm")"
current-value="@CurrentValueAsString"
@onchange="@ChangeHandlerAsync"
@oninput="@InputHandlerAsync"
readonly="@ReadOnly"
Expand Down
60 changes: 59 additions & 1 deletion src/Core/Components/DateTime/FluentTimePicker.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@
// ------------------------------------------------------------------------

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components.Extensions;
using Microsoft.FluentUI.AspNetCore.Components.Utilities;
using Microsoft.JSInterop;

namespace Microsoft.FluentUI.AspNetCore.Components;

public partial class FluentTimePicker : FluentInputBase<DateTime?>
{
private const string JAVASCRIPT_FILE = "./_content/Microsoft.FluentUI.AspNetCore.Components/Components/DateTime/FluentTimePicker.razor.js";

/// <summary />
[Inject]
private LibraryConfiguration LibraryConfiguration { get; set; } = default!;

/// <summary />
[Inject]
private IJSRuntime JSRuntime { get; set; } = default!;

/// <summary />
private IJSObjectReference? Module { get; set; }

/// <summary />
protected override string? StyleValue => new StyleBuilder(Style).Build();

Expand All @@ -19,16 +35,39 @@ public partial class FluentTimePicker : FluentInputBase<DateTime?>
[Parameter]
public virtual FluentInputAppearance Appearance { get; set; } = FluentInputAppearance.Outline;

/// <summary>
/// Gets or sets the time format.
/// </summary>
[Parameter]
public TimeDisplay TimeDisplay { get; set; } = TimeDisplay.HourMinute;

/// <summary />
protected override string? FormatValueAsString(DateTime? value)
{
var format = TimeDisplay switch
{
TimeDisplay.HourMinute => "HH:mm",
TimeDisplay.HourMinuteSeconds => "HH:mm:ss",
_ => "HH:mm",
};

var result = value?.ToString(format, CultureInfo.InvariantCulture);

return result;
}

/// <summary />
protected override bool TryParseValueFromString(string? value, out DateTime? result, [NotNullWhen(false)] out string? validationErrorMessage)
{
var acceptedFormats = new string[] { "HH:mm", "HH:mm:ss", "HH:mm:ss.fff" };

DateTime currentValue = Value ?? DateTime.MinValue;

if (string.IsNullOrWhiteSpace(value))
{
result = null;
}
else if (value != null && DateTime.TryParse(value, out var valueConverted))
else if (DateTime.TryParseExact(value, acceptedFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out var valueConverted))
{
result = currentValue.Date + valueConverted.TimeOfDay;
}
Expand All @@ -40,4 +79,23 @@ protected override bool TryParseValueFromString(string? value, out DateTime? res
validationErrorMessage = null;
return true;
}

/// <summary />
protected override void OnInitialized()
{
if (string.IsNullOrEmpty(Id) && TimeDisplay == TimeDisplay.HourMinuteSeconds)
{
Id = Identifier.NewId();
}
}

/// <summary />
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && TimeDisplay == TimeDisplay.HourMinuteSeconds)
{
Module ??= await JSRuntime.InvokeAsync<IJSObjectReference>("import", JAVASCRIPT_FILE.FormatCollocatedUrl(LibraryConfiguration));
await Module.InvokeVoidAsync("setControlAttribute", Id, "step", 1);
}
}
}
7 changes: 7 additions & 0 deletions src/Core/Components/DateTime/FluentTimePicker.razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function setControlAttribute(id, attrName, value) {
const fieldElement = document.getElementById(id)?.shadowRoot?.querySelector(".control");

if (!!fieldElement) {
fieldElement?.setAttribute(attrName, value);
}
}
22 changes: 22 additions & 0 deletions src/Core/Enums/TimeDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ------------------------------------------------------------------------
// This file is licensed to you under the MIT License.
// ------------------------------------------------------------------------

namespace Microsoft.FluentUI.AspNetCore.Components;

/// <summary>
/// Specifies the format for displaying time in input elements with time component.
/// This is a hint to the browser, so results may vary.
/// </summary>
public enum TimeDisplay
{
/// <summary>
/// Show hours and minutes
/// </summary>
HourMinute,

/// <summary>
/// Show hours, minutes and seconds
/// </summary>
HourMinuteSeconds,
}
Loading