From d5dd9f554a4d16867b1cb589240367ecd55ec544 Mon Sep 17 00:00:00 2001 From: Mladen Macanovic Date: Wed, 13 Nov 2024 10:33:18 +0100 Subject: [PATCH] DataGridCellDatePicker: parse valid date format --- .../_DataGridCellDatePicker.razor | 76 --------------- .../_DataGridCellDatePicker.razor.cs | 95 +++++++++++++++++++ 2 files changed, 95 insertions(+), 76 deletions(-) diff --git a/Source/Extensions/Blazorise.DataGrid/_DataGridCellDatePicker.razor b/Source/Extensions/Blazorise.DataGrid/_DataGridCellDatePicker.razor index f5c93699e1..542906ad43 100644 --- a/Source/Extensions/Blazorise.DataGrid/_DataGridCellDatePicker.razor +++ b/Source/Extensions/Blazorise.DataGrid/_DataGridCellDatePicker.razor @@ -12,80 +12,4 @@ { @DatePickerFragment } -} - -@code { - private RenderFragment DatePickerFragment => builder => - { - var type = typeof( DatePicker<> ).MakeGenericType( new[] { valueType } ); - - builder.OpenComponent( 0, type ); - builder.AddAttribute( 1, nameof( DatePicker.Date ), valueType switch - { - Type typeDateTime when typeDateTime == typeof( DateTime ) => (DateTime)( CellValue ?? (DateTime)default ), - Type typeDateTimeNull when typeDateTimeNull == typeof( DateTime? ) => (DateTime?)( CellValue ?? (DateTime?)default ), - Type typeDateOnly when typeDateOnly == typeof( DateOnly ) => (DateOnly)( CellValue ?? (DateOnly)default ), - Type typeDateOnlyNull when typeDateOnlyNull == typeof( DateOnly? ) => (DateOnly?)( CellValue ?? (DateOnly?)default ), - Type typeDateTimeOffset when typeDateTimeOffset == typeof( DateTimeOffset ) => (DateTimeOffset)( CellValue ?? (DateTimeOffset)default ), - Type typeDateTimeOffsetNull when typeDateTimeOffsetNull == typeof( DateTimeOffset? ) => (DateTimeOffset?)( CellValue ?? (DateTimeOffset?)default ), - _ => throw new InvalidOperationException( $"Unsupported type {valueType}" ) - } ); - builder.AddAttribute( 2, nameof( DatePicker.DateChanged ), valueType switch - { - Type typeDateTime when typeDateTime == typeof( DateTime ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateTimeNull when typeDateTimeNull == typeof( DateTime? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateOnly when typeDateOnly == typeof( DateOnly ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateOnlyNull when typeDateOnlyNull == typeof( DateOnly? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateTimeOffset when typeDateTimeOffset == typeof( DateTimeOffset ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateTimeOffsetNull when typeDateTimeOffsetNull == typeof( DateTimeOffset? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - _ => throw new InvalidOperationException( $"Unsupported type {valueType}" ) - } ); - builder.AddAttribute( 3, nameof( BaseInputComponent.ReadOnly ), Column.Readonly ); - builder.AddAttribute( 4, nameof( DatePicker.Pattern ), Column.ValidationPattern ); - builder.AddAttribute( 5, nameof( DatePicker.InputMode ), Column.InputMode ); - builder.AddAttribute( 6, nameof( DatePicker.Min ), Column.Min ); - builder.AddAttribute( 7, nameof( DatePicker.Max ), Column.Max ); - builder.AddAttribute( 8, nameof( DatePicker.FirstDayOfWeek ), Column.FirstDayOfWeek ); - builder.AddAttribute( 9, nameof( DatePicker.InputFormat ), Column.InputFormat ); - builder.AddAttribute( 10, nameof( DatePicker.DisabledDates ), Column.DisabledDates ); - builder.AddAttribute( 11, nameof( DatePicker.DisabledDays ), Column.DisabledDays ); - builder.AddAttribute( 12, nameof( DatePicker.Inline ), Column.Inline ); - builder.AddAttribute( 13, nameof( DatePicker.DisableMobile ), Column.DisableMobile ); - builder.AddAttribute( 14, nameof( DatePicker.StaticPicker ), Column.StaticPicker ); - builder.AddAttribute( 15, nameof( DatePicker.DisplayFormat ), Column.DisplayFormat ); - builder.CloseComponent(); - }; - - private RenderFragment DateEditFragment => builder => - { - var type = typeof( DateEdit<> ).MakeGenericType( new[] { valueType } ); - - builder.OpenComponent( 0, type ); - builder.AddAttribute( 1, nameof( DateEdit.Date ), valueType switch - { - Type typeDateTime when typeDateTime == typeof( DateTime ) => (DateTime)( CellValue ?? (DateTime)default ), - Type typeDateTimeNull when typeDateTimeNull == typeof( DateTime? ) => (DateTime?)( CellValue ?? (DateTime?)default ), - Type typeDateOnly when typeDateOnly == typeof( DateOnly ) => (DateOnly)( CellValue ?? (DateOnly)default ), - Type typeDateOnlyNull when typeDateOnlyNull == typeof( DateOnly? ) => (DateOnly?)( CellValue ?? (DateOnly?)default ), - Type typeDateTimeOffset when typeDateTimeOffset == typeof( DateTimeOffset ) => (DateTimeOffset)( CellValue ?? (DateTimeOffset)default ), - Type typeDateTimeOffsetNull when typeDateTimeOffsetNull == typeof( DateTimeOffset? ) => (DateTimeOffset?)( CellValue ?? (DateTimeOffset?)default ), - _ => throw new InvalidOperationException( $"Unsupported type {valueType}" ) - } ); - builder.AddAttribute( 2, nameof( DateEdit.DateChanged ), valueType switch - { - Type typeDateTime when typeDateTime == typeof( DateTime ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateTimeNull when typeDateTimeNull == typeof( DateTime? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateOnly when typeDateOnly == typeof( DateOnly ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateOnlyNull when typeDateOnlyNull == typeof( DateOnly? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateTimeOffset when typeDateTimeOffset == typeof( DateTimeOffset ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - Type typeDateTimeOffsetNull when typeDateTimeOffsetNull == typeof( DateTimeOffset? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), - _ => throw new InvalidOperationException( $"Unsupported type {valueType}" ) - } ); - builder.AddAttribute( 3, nameof( BaseInputComponent.ReadOnly ), Column.Readonly ); - builder.AddAttribute( 4, nameof( DateEdit.Pattern ), Column.ValidationPattern ); - builder.AddAttribute( 5, nameof( DateEdit.InputMode ), Column.InputMode ); - builder.AddAttribute( 6, nameof( DateEdit.Min ), Column.Min ); - builder.AddAttribute( 7, nameof( DateEdit.Max ), Column.Max ); - builder.CloseComponent(); - }; } \ No newline at end of file diff --git a/Source/Extensions/Blazorise.DataGrid/_DataGridCellDatePicker.razor.cs b/Source/Extensions/Blazorise.DataGrid/_DataGridCellDatePicker.razor.cs index a44aec3313..bb853af32b 100644 --- a/Source/Extensions/Blazorise.DataGrid/_DataGridCellDatePicker.razor.cs +++ b/Source/Extensions/Blazorise.DataGrid/_DataGridCellDatePicker.razor.cs @@ -1,5 +1,6 @@ #region Using directives using System; +using System.Globalization; using System.Threading.Tasks; using Blazorise.Modules; using Microsoft.AspNetCore.Components; @@ -13,6 +14,8 @@ namespace Blazorise.DataGrid; /// public partial class _DataGridCellDatePicker : ComponentBase { + #region Members + protected string elementId; /// @@ -20,11 +23,25 @@ public partial class _DataGridCellDatePicker : ComponentBase /// private Type valueType; + private string dateDisplayFormat; + + #endregion + + #region Methods + /// protected override void OnInitialized() { valueType = Column.GetValueType( default ); elementId = IdGenerator.Generate; + + // this is a woraround for https://github.com/Megabit/Blazorise/issues/5837 + // in 2.0 we need to remove formating as {0:dd.MM.yyyy}, and on support dd.MM.yyyy + if ( Column.DisplayFormat is not null && Column.DisplayFormat.StartsWith( "{0:" ) && Column.DisplayFormat.EndsWith( "}" ) ) + { + dateDisplayFormat = Column.DisplayFormat.Substring( 3, Column.DisplayFormat.Length - 4 ); + } + base.OnInitialized(); } @@ -49,6 +66,83 @@ public async Task Focus() await JSUtilitiesModule.Focus( default, elementId, true ); } + #endregion + + #region Properties + + private RenderFragment DatePickerFragment => builder => + { + var type = typeof( DatePicker<> ).MakeGenericType( new[] { valueType } ); + + builder.OpenComponent( 0, type ); + builder.AddAttribute( 1, nameof( DatePicker.Date ), valueType switch + { + Type typeDateTime when typeDateTime == typeof( DateTime ) => (DateTime)( CellValue ?? (DateTime)default ), + Type typeDateTimeNull when typeDateTimeNull == typeof( DateTime? ) => (DateTime?)( CellValue ?? (DateTime?)default ), + Type typeDateOnly when typeDateOnly == typeof( DateOnly ) => (DateOnly)( CellValue ?? (DateOnly)default ), + Type typeDateOnlyNull when typeDateOnlyNull == typeof( DateOnly? ) => (DateOnly?)( CellValue ?? (DateOnly?)default ), + Type typeDateTimeOffset when typeDateTimeOffset == typeof( DateTimeOffset ) => (DateTimeOffset)( CellValue ?? (DateTimeOffset)default ), + Type typeDateTimeOffsetNull when typeDateTimeOffsetNull == typeof( DateTimeOffset? ) => (DateTimeOffset?)( CellValue ?? (DateTimeOffset?)default ), + _ => throw new InvalidOperationException( $"Unsupported type {valueType}" ) + } ); + builder.AddAttribute( 2, nameof( DatePicker.DateChanged ), valueType switch + { + Type typeDateTime when typeDateTime == typeof( DateTime ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateTimeNull when typeDateTimeNull == typeof( DateTime? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateOnly when typeDateOnly == typeof( DateOnly ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateOnlyNull when typeDateOnlyNull == typeof( DateOnly? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateTimeOffset when typeDateTimeOffset == typeof( DateTimeOffset ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateTimeOffsetNull when typeDateTimeOffsetNull == typeof( DateTimeOffset? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + _ => throw new InvalidOperationException( $"Unsupported type {valueType}" ) + } ); + builder.AddAttribute( 3, nameof( BaseInputComponent.ReadOnly ), Column.Readonly ); + builder.AddAttribute( 4, nameof( DatePicker.Pattern ), Column.ValidationPattern ); + builder.AddAttribute( 5, nameof( DatePicker.InputMode ), Column.InputMode ); + builder.AddAttribute( 6, nameof( DatePicker.Min ), Column.Min ); + builder.AddAttribute( 7, nameof( DatePicker.Max ), Column.Max ); + builder.AddAttribute( 8, nameof( DatePicker.FirstDayOfWeek ), Column.FirstDayOfWeek ); + builder.AddAttribute( 9, nameof( DatePicker.InputFormat ), Column.InputFormat ); + builder.AddAttribute( 10, nameof( DatePicker.DisabledDates ), Column.DisabledDates ); + builder.AddAttribute( 11, nameof( DatePicker.DisabledDays ), Column.DisabledDays ); + builder.AddAttribute( 12, nameof( DatePicker.Inline ), Column.Inline ); + builder.AddAttribute( 13, nameof( DatePicker.DisableMobile ), Column.DisableMobile ); + builder.AddAttribute( 14, nameof( DatePicker.StaticPicker ), Column.StaticPicker ); + builder.AddAttribute( 15, nameof( DatePicker.DisplayFormat ), dateDisplayFormat ); + builder.CloseComponent(); + }; + + private RenderFragment DateEditFragment => builder => + { + var type = typeof( DateEdit<> ).MakeGenericType( new[] { valueType } ); + + builder.OpenComponent( 0, type ); + builder.AddAttribute( 1, nameof( DateEdit.Date ), valueType switch + { + Type typeDateTime when typeDateTime == typeof( DateTime ) => (DateTime)( CellValue ?? (DateTime)default ), + Type typeDateTimeNull when typeDateTimeNull == typeof( DateTime? ) => (DateTime?)( CellValue ?? (DateTime?)default ), + Type typeDateOnly when typeDateOnly == typeof( DateOnly ) => (DateOnly)( CellValue ?? (DateOnly)default ), + Type typeDateOnlyNull when typeDateOnlyNull == typeof( DateOnly? ) => (DateOnly?)( CellValue ?? (DateOnly?)default ), + Type typeDateTimeOffset when typeDateTimeOffset == typeof( DateTimeOffset ) => (DateTimeOffset)( CellValue ?? (DateTimeOffset)default ), + Type typeDateTimeOffsetNull when typeDateTimeOffsetNull == typeof( DateTimeOffset? ) => (DateTimeOffset?)( CellValue ?? (DateTimeOffset?)default ), + _ => throw new InvalidOperationException( $"Unsupported type {valueType}" ) + } ); + builder.AddAttribute( 2, nameof( DateEdit.DateChanged ), valueType switch + { + Type typeDateTime when typeDateTime == typeof( DateTime ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateTimeNull when typeDateTimeNull == typeof( DateTime? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateOnly when typeDateOnly == typeof( DateOnly ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateOnlyNull when typeDateOnlyNull == typeof( DateOnly? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateTimeOffset when typeDateTimeOffset == typeof( DateTimeOffset ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + Type typeDateTimeOffsetNull when typeDateTimeOffsetNull == typeof( DateTimeOffset? ) => EventCallback.Factory.Create( this, ( OnCellValueChanged ) ), + _ => throw new InvalidOperationException( $"Unsupported type {valueType}" ) + } ); + builder.AddAttribute( 3, nameof( BaseInputComponent.ReadOnly ), Column.Readonly ); + builder.AddAttribute( 4, nameof( DateEdit.Pattern ), Column.ValidationPattern ); + builder.AddAttribute( 5, nameof( DateEdit.InputMode ), Column.InputMode ); + builder.AddAttribute( 6, nameof( DateEdit.Min ), Column.Min ); + builder.AddAttribute( 7, nameof( DateEdit.Max ), Column.Max ); + builder.CloseComponent(); + }; [CascadingParameter] public DataGrid ParentDataGrid { get; set; } @@ -73,4 +167,5 @@ public async Task Focus() /// [Parameter] public EventCallback CellValueChanged { get; set; } + #endregion } \ No newline at end of file