-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace <InputDate> with <DateOnlyPicker>
Based on MudDatePicker. See: MudBlazor/MudBlazor#6178 (comment)
- Loading branch information
1 parent
7a98b5d
commit c6e7fd1
Showing
4 changed files
with
80 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters