Skip to content

added Ignore and Hidden attributes #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/QuickForm/Attributes/HiddenAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace QuickForm.Attributes;

/// <summary>
/// For attributes that have an input field, but should not be displayed.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class HiddenAttribute : Attribute
{
}
9 changes: 9 additions & 0 deletions src/QuickForm/Attributes/IgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace QuickForm.Attributes;

/// <summary>
/// For attributes that don't have NotMapped, but still need to be ignored.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class IgnoreAttribute : Attribute
{
}
8 changes: 8 additions & 0 deletions src/QuickForm/Components/IQuickFormField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ public interface IQuickFormField
/// the string parameter is the class to be applied to the input element.
/// </note>
public RenderFragment<string> ValidationMessages { get; }

/// <summary>
/// Gets if the input together with its label and everything else should be hidden.
/// </summary>
/// <note>
/// The input element will still be rendered, but it will be hidden.
/// </note>
public bool IsHidden { get; }
}
11 changes: 10 additions & 1 deletion src/QuickForm/Components/QuickForm.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@

@foreach (var field in Fields)
{
@ChildContent(field)
if (field.IsHidden)
{
<div style="display:none">
@ChildContent(field)
</div>
}
else
{
@ChildContent(field)
}
}

@if (SubmitButtonTemplate is not null)
Expand Down
4 changes: 4 additions & 0 deletions src/QuickForm/Components/QuickFormField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using QuickForm.Attributes;
using QuickForm.Internal;

namespace QuickForm.Components;
Expand All @@ -19,6 +20,8 @@ internal sealed class QuickFormField<TEntity> : IQuickFormField

public string? ValidFeedback => PropertyInfo.ValidFeedbackText();

public bool IsHidden => PropertyInfo.GetCustomAttribute<HiddenAttribute>() is not null;

public RenderFragment<string> InputComponent
{
get
Expand Down Expand Up @@ -142,6 +145,7 @@ internal static IEnumerable<QuickFormField<TEntity>> FromForm(QuickForm<TEntity>
return typeof(TEntity)
.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
.Where(prop => prop.GetCustomAttribute<NotMappedAttribute>() is null)
.Where(prop => prop.GetCustomAttribute<IgnoreAttribute>() is null)
.Select(prop => new QuickFormField<TEntity>(form, prop));
}
}
19 changes: 10 additions & 9 deletions test/QuickForm.Example/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
{
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:443;http://localhost:80",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:443;http://localhost:80",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
}