Skip to content

Commit

Permalink
feat(blazor): rework forms templates
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Jan 16, 2023
1 parent fa5c76d commit 8488455
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<MudEditForm TEntity="TEntity" Entity="Entity" @ref="FormInstance">
<Sitko.Core.Blazor.FluentValidation.FluentValidator/>
<Sitko.Core.Blazor.Forms.FormEditContextCatcher Form="this"/>
@ChildContentFragment
@ChildContent(FormContext)
@if (Debug)
{
<MudFormDebug TEntity="TEntity" Form="this"></MudFormDebug>
Expand All @@ -23,7 +23,7 @@ else
}
else
{
@ChildContentFragment
@ChildContent(FormContext)
@if (Debug)
{
<MudFormDebug TEntity="TEntity" Form="this"></MudFormDebug>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@
// ReSharper disable once CheckNamespace
namespace Sitko.Core.Blazor.MudBlazorComponents;

public class MudEntityForm<TEntity> : BaseMudForm<TEntity>
where TEntity : class, new()
{
[EditorRequired] [Parameter] public RenderFragment<BaseMudForm<TEntity>> ChildContent { get; set; } = null!;

protected override RenderFragment ChildContentFragment => ChildContent(this);
}

public abstract partial class BaseMudForm<TEntity>
where TEntity : class, new()
{
protected abstract RenderFragment ChildContentFragment { get; }
[Parameter] [EditorRequired] public RenderFragment<FormContext<TEntity>> ChildContent { get; set; } = null!;
[Parameter] public RenderFragment? LoadingContent { get; set; }
protected MudEditForm<TEntity>? FormInstance { get; set; }
[Inject] public ISnackbar Snackbar { get; set; } = null!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<MudEditForm TEntity="TEntity" Entity="Entity" @ref="FormInstance">
<Sitko.Core.Blazor.FluentValidation.FluentValidator/>
<Sitko.Core.Blazor.Forms.FormEditContextCatcher Form="this"/>
@ChildContentFragment
@ChildContent(FormContext)
@if (Debug)
{
<MudFormDebug TEntity="TEntity" Form="this"></MudFormDebug>
Expand All @@ -24,7 +24,7 @@ else
}
else
{
@ChildContentFragment
@ChildContent(FormContext)
@if (Debug)
{
<MudFormDebug TEntity="TEntity" Form="this"></MudFormDebug>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
using Microsoft.AspNetCore.Components;
using MudBlazor;
using Sitko.Core.Blazor.Forms;
using Sitko.Core.Repository;

// ReSharper disable once CheckNamespace
namespace Sitko.Core.Blazor.MudBlazorComponents;

public class MudRepositoryForm<TEntity, TEntityPk> : BaseMudRepositoryForm<TEntity, TEntityPk,
IRepository<TEntity, TEntityPk>>
where TEntity : class, IEntity<TEntityPk>, new() where TEntityPk : notnull
{
[EditorRequired]
[Parameter]
public RenderFragment<MudRepositoryForm<TEntity, TEntityPk>> ChildContent { get; set; } = null!;

protected override RenderFragment ChildContentFragment => ChildContent(this);
}

public abstract partial class BaseMudRepositoryForm<TEntity, TEntityPk, TRepository>
where TEntity : class, IEntity<TEntityPk>, new()
where TRepository : class, IRepository<TEntity, TEntityPk>
Expand All @@ -24,7 +14,8 @@ public abstract partial class BaseMudRepositoryForm<TEntity, TEntityPk, TReposit
protected MudEditForm<TEntity>? FormInstance { get; set; }
[Parameter] public bool Debug { get; set; }
[Inject] public ISnackbar Snackbar { get; set; } = null!;
protected abstract RenderFragment ChildContentFragment { get; }

[Parameter] [EditorRequired] public RenderFragment<FormContext<TEntity>> ChildContent { get; set; } = null!;

[Parameter] public RenderFragment? LoadingContent { get; set; }

Expand Down Expand Up @@ -53,4 +44,3 @@ public override async Task ResetAsync()
await StopLoadingAsync();
}
}

4 changes: 2 additions & 2 deletions src/Sitko.Core.Blazor/Forms/BaseForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public abstract class BaseForm<TEntity> : BaseForm where TEntity : class, new()
private CompareLogic? comparer;
private TEntity? currentEntity;
protected TEntity? EntitySnapshot { get; private set; }

protected FormContext<TEntity> FormContext { get; private set; } = null!;
public bool IsNew { get; protected set; }

public TEntity Entity
Expand All @@ -76,7 +76,6 @@ public TEntity Entity
private set => currentEntity = value;
}


[Parameter] public Func<TEntity, Task>? OnAfterSave { get; set; }
[Parameter] public Func<TEntity, Task>? OnAfterCreate { get; set; }

Expand Down Expand Up @@ -113,6 +112,7 @@ protected override async Task InitializeAsync()
(IsNew, Entity) = await GetEntityAsync();
await InitializeEntityAsync(Entity);
EntitySnapshot = CreateEntitySnapshot(Entity);
FormContext = new(Entity, this);
}

protected abstract Task<(bool IsNew, TEntity Entity)> GetEntityAsync();
Expand Down
3 changes: 3 additions & 0 deletions src/Sitko.Core.Blazor/Forms/FormContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Sitko.Core.Blazor.Forms;

public record FormContext<TEntity>(TEntity Entity, BaseForm<TEntity> Form) where TEntity : class, new();

0 comments on commit 8488455

Please sign in to comment.