diff --git a/Pkmds.Web/Components/DateOnlyPicker.razor b/Pkmds.Web/Components/DateOnlyPicker.razor new file mode 100644 index 00000000..c7d408b9 --- /dev/null +++ b/Pkmds.Web/Components/DateOnlyPicker.razor @@ -0,0 +1,12 @@ +@* + HACK: This component works as an adapter between MudDatePicker which only accepts DateTime? and any DateOnly? object. + Remove once issue https://github.com/MudBlazor/MudBlazor/issues/6178 is fixed. + Thanks to: https://github.com/MudBlazor/MudBlazor/issues/6178#issuecomment-2056451836 +*@ + + diff --git a/Pkmds.Web/Components/DateOnlyPicker.razor.cs b/Pkmds.Web/Components/DateOnlyPicker.razor.cs new file mode 100644 index 00000000..8ab9078a --- /dev/null +++ b/Pkmds.Web/Components/DateOnlyPicker.razor.cs @@ -0,0 +1,59 @@ +namespace Pkmds.Web.Components; + +public partial class DateOnlyPicker +{ + [CascadingParameter] + public EditContext? EditContext { get; set; } + + [Parameter, EditorRequired] + public DateOnly? Date { get; set; } + + [Parameter] + public EventCallback DateChanged { get; set; } + + [Parameter, EditorRequired] + public string? Label { get; set; } + + [Parameter] + public Expression>? For { get; set; } + + [Parameter] + public bool ReadOnly { get; set; } + + [Parameter] + public string? HelperText { get; set; } + + private MudDatePicker? datePickerRef; + + private DateTime? DateBindTarget + { + get => Date?.ToDateTime(TimeOnly.MinValue); + set + { + if (value is not null) + { + Date = DateOnly.FromDateTime((DateTime)value); + DateChanged.InvokeAsync(Date); + } + } + } + + protected override void OnAfterRender(bool firstRender) + { + if (!firstRender || For is null) + { + return; + } + + if (EditContext is null) + { + throw new Exception("Using 'For' without an 'EditContext' is not supported. Are you missing an 'EditForm'?"); + } + + // Get the private field _fieldidentifier by reflection. + var fieldIdentifierField = typeof(MudFormComponent).GetField("_fieldIdentifier", BindingFlags.Instance | BindingFlags.NonPublic); + + // Set the field identifier with our DateOnly? expression, avoiding the type issue between DateOnly vs DateTime + fieldIdentifierField?.SetValue(datePickerRef, FieldIdentifier.Create(For)); + } +} diff --git a/Pkmds.Web/Components/EditForms/Tabs/MetTab.razor b/Pkmds.Web/Components/EditForms/Tabs/MetTab.razor index d0d70617..1797f51a 100644 --- a/Pkmds.Web/Components/EditForms/Tabs/MetTab.razor +++ b/Pkmds.Web/Components/EditForms/Tabs/MetTab.razor @@ -41,9 +41,9 @@ EntityContext.MaxInvalid && Height="22" /> @ball.Text - - - + + + } @@ -58,15 +58,8 @@ EntityContext.MaxInvalid && - - - Met Date - - - + @* @@ -86,14 +79,7 @@ EntityContext.MaxInvalid && - - - Egg Date - - - + } diff --git a/Pkmds.Web/GlobalUsings.cs b/Pkmds.Web/GlobalUsings.cs index 38815e3c..e60a5954 100644 --- a/Pkmds.Web/GlobalUsings.cs +++ b/Pkmds.Web/GlobalUsings.cs @@ -1,4 +1,5 @@ -global using System.Reflection; +global using System.Linq.Expressions; +global using System.Reflection; global using System.Text; global using KristofferStrube.Blazor.FileSystemAccess; global using Microsoft.AspNetCore.Components;