Skip to content

Commit

Permalink
Replace <InputDate> with <DateOnlyPicker>
Browse files Browse the repository at this point in the history
Based on MudDatePicker. See: MudBlazor/MudBlazor#6178 (comment)
  • Loading branch information
codemonkey85 committed Apr 18, 2024
1 parent 7a98b5d commit c6e7fd1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 22 deletions.
12 changes: 12 additions & 0 deletions Pkmds.Web/Components/DateOnlyPicker.razor
Original file line number Diff line number Diff line change
@@ -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
*@

<MudDatePicker @ref="datePickerRef"
Label="@Label"
Class="d-inline-flex"
ReadOnly="@ReadOnly"
HelperText="@HelperText"
@bind-Date="DateBindTarget" />
59 changes: 59 additions & 0 deletions Pkmds.Web/Components/DateOnlyPicker.razor.cs
Original file line number Diff line number Diff line change
@@ -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<DateOnly?> DateChanged { get; set; }

[Parameter, EditorRequired]
public string? Label { get; set; }

[Parameter]
public Expression<Func<DateOnly?>>? 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<DateTime?, string>).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));
}
}
28 changes: 7 additions & 21 deletions Pkmds.Web/Components/EditForms/Tabs/MetTab.razor
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ EntityContext.MaxInvalid &&
Height="22" />
<MudText>
@ball.Text
</MudText>
</MudStack>
</MudSelectItem>
</MudText>
</MudStack>
</MudSelectItem>
}
</MudSelect>

Expand All @@ -58,15 +58,8 @@ EntityContext.MaxInvalid &&
<MudPaper Elevation="0"
Outlined="@true"
Class="my-2">
<MudStack>
<MudInputLabel ForId="metDate">
Met Date
</MudInputLabel>
<InputDate @bind-Value="@Pokemon.MetDate"
style="color: var(--mud-palette-text-primary); background-color: var(--mud-palette-surface);"
id="metDate"
DisplayName="Met Date" />
</MudStack>
<DateOnlyPicker @bind-Date="@Pokemon.MetDate"
Label="Met Date" />
</MudPaper>

@*
Expand All @@ -86,14 +79,7 @@ EntityContext.MaxInvalid &&
<MudPaper Elevation="0"
Outlined="@true"
Class="my-2">
<MudStack>
<MudInputLabel ForId="eggDate">
Egg Date
</MudInputLabel>
<InputDate @bind-Value="@Pokemon.EggMetDate"
style="color: var(--mud-palette-text-primary); background-color: var(--mud-palette-surface);"
id="eggDate"
DisplayName="Egg Date" />
</MudStack>
<DateOnlyPicker @bind-Date="@Pokemon.EggMetDate"
Label="Met Date" />
</MudPaper>
}
3 changes: 2 additions & 1 deletion Pkmds.Web/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit c6e7fd1

Please sign in to comment.