Skip to content

Commit

Permalink
api consistency improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Modest-as committed Nov 29, 2021
1 parent cd0be6e commit cb5b42a
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 79 deletions.
6 changes: 6 additions & 0 deletions Crude.Demo.Wasm/Pages/Documentation.razor
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<li>
<code>[CrudePassword]</code> - Indicates that the string property should be rendered as a password field.
</li>
<li>
<code>[CrudePlaceholder]</code> - Indicates that the property should render input placeholder.
</li>
<li>
<code>[CrudeHtmlLabel]</code> - Label replacement text, can be HTML.
</li>
</ol>
</p>

Expand Down
2 changes: 2 additions & 0 deletions Crude.Demo.Wasm/Pages/Example01.razor
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public class RegistrationViewModel
{
[Required]
[Display(Name = ""Display Name"")]
[CrudePlaceholder]
[StringLength(20, MinimumLength = 2)]
public string Name { get; set; }
[Required]
[EmailAddress]
[CrudePlaceholder]
public string Email { get; set; }
[CrudePassword]
Expand Down
4 changes: 2 additions & 2 deletions Crude.Demo.Wasm/Pages/Example07.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
public class AdvancedLoginViewModel
{
[Required]
[CrudeEmptyPlaceholder]
[CrudePlaceholder]
[EmailAddress]
public string Email { get; set; }
[CrudePassword]
[CrudeEmptyPlaceholder]
[CrudePlaceholder]
[CrudeHtmlLabel(""<a href=\""#\"">Forgot your password?</a>"")]
[Required]
[Display(Name = ""Password"")]
Expand Down
4 changes: 2 additions & 2 deletions Crude.Demo.Wasm/ViewModel/AdvancedLoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Crude.Demo.Wasm.ViewModel
public class AdvancedLoginViewModel
{
[Required]
[CrudeEmptyPlaceholder]
[CrudePlaceholder]
[EmailAddress]
public string Email { get; set; }

[CrudePassword]
[CrudeEmptyPlaceholder]
[CrudePlaceholder]
[CrudeHtmlLabel("<a href=\"#\">Forgot your password?</a>")]
[Required]
[Display(Name = "Password")]
Expand Down
2 changes: 2 additions & 0 deletions Crude.Demo.Wasm/ViewModel/RegistrationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ public class RegistrationViewModel
{
[Required]
[Display(Name = "Display Name")]
[CrudePlaceholder]
[StringLength(20, MinimumLength = 2)]
public string Name { get; set; }

[Required]
[CrudePlaceholder]
[EmailAddress]
public string Email { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Crude.Demo.Wasm/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<link href="css/grids-responsive-min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<link href="css/github.css" rel="stylesheet" />
<link href="_content/Crude/styles.css" rel="stylesheet" />
<link href="_content/Crude/Crude.css" rel="stylesheet" />

<script src="js/highlight.pack.js"></script>
<script src="js/highlight.js"></script>
Expand Down
4 changes: 0 additions & 4 deletions Crude/Core/Attributes/CrudeEmptyPlaceholderAttribute.cs

This file was deleted.

4 changes: 4 additions & 0 deletions Crude/Core/Attributes/CrudePlaceholderAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Crude.Core.Attributes
{
public sealed class CrudePlaceholderAttribute : CrudePropertyAttribute { }
}
2 changes: 1 addition & 1 deletion Crude/Core/FieldFragments/FieldFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void AddFieldAttributes<T>(ref int seq, RenderTreeBuilder builder)

var valueChanged = ValueChanged ?? EventCallback.Factory.Create<T>(this, value => Property.SetValue(value));

if (!Property.EmptyPlaceholder)
if (Property.Placeholder)
{
builder.AddAttribute(seq++, "Placeholder", Property.Name);
}
Expand Down
3 changes: 1 addition & 2 deletions Crude/Core/LayoutFragments/CrudeTreeFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public RenderFragment Render(RenderContext context) => builder =>
var seq = 0;

builder.OpenComponent<EditForm>(seq++);
builder.AddAttribute(seq++, "class", "crude-tree");
builder.AddAttribute(seq++, "EditContext", context.EditContext);

var onSubmit = GetOnSubmitButton(context);
Expand All @@ -36,7 +37,6 @@ private static RenderFragment<EditContext> RenderFormContents(RenderContext cont

var seq = 0;

builder.OpenElement(seq++, "crude-tree");
builder.OpenElement(seq++, "crude-tree-header");

builder.OpenComponent<DataAnnotationsValidator>(seq++);
Expand Down Expand Up @@ -87,7 +87,6 @@ private static RenderFragment<EditContext> RenderFormContents(RenderContext cont
}

builder.CloseElement();
builder.CloseElement();
};

private static FieldGroupFragment CreateFieldFragment(CrudeProperty property)
Expand Down
6 changes: 5 additions & 1 deletion Crude/Core/LayoutFragments/FieldGroupFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ public RenderFragment Render(RenderContext context) => builder =>

builder.OpenElement(seq++, "crude-field-fragment");

var cssClass = $"cfn-{_property.SequenceNo}";

if (!string.IsNullOrWhiteSpace(fragment.FragmentType))
{
builder.AddAttribute(seq++, "class", $"cft-{fragment.FragmentType}");
cssClass = $"{cssClass} cft-{fragment.FragmentType}";
}

builder.AddAttribute(seq++, "class", cssClass);

builder.OpenElement(seq++, "label");
builder.AddAttribute(seq++, "for", fragment.Identifier);

Expand Down
111 changes: 46 additions & 65 deletions Crude/Core/Parsers/ViewModelParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static IEnumerable<CrudeProperty> ParseProperties(object viewModel)

var htmlLabel = htmlLabelAttribute?.Html ?? string.Empty;

var emptyPlaceholder = attributes.FirstOrDefault(x => x is CrudeEmptyPlaceholderAttribute) is CrudeEmptyPlaceholderAttribute _;
var placeholder = attributes.FirstOrDefault(x => x is CrudePlaceholderAttribute) is CrudePlaceholderAttribute _;

CrudeEvent? onClick = null;

Expand All @@ -69,19 +69,29 @@ public static IEnumerable<CrudeProperty> ParseProperties(object viewModel)
}
}

items.Add(new CrudeProperty(
name,
order,
onClick,
property,
viewModel,
disabled,
password,
emptyPlaceholder,
htmlLabel));
items.Add(new CrudeProperty
{
Name = name,
Order = order,
OnClick = onClick,
Info = property,
ViewModel = viewModel,
Disabled = disabled,
Password = password,
Placeholder = placeholder,
HtmlLabel = htmlLabel
});
}

return items.OrderBy(x => x.Order);
var seqNo = 0;

foreach (var item in items.OrderBy(x => x.Order))
{
yield return item with
{
SequenceNo = ++seqNo
};
}
}

public static List<CrudeMethod> ParseMethods(object viewModel)
Expand All @@ -107,82 +117,53 @@ public static List<CrudeMethod> ParseMethods(object viewModel)
continue;
}

items.Add(new CrudeMethod(memberInfo, methodInfo, attributes.Cast<CrudeMethodAttribute>().ToList()));
items.Add(new CrudeMethod
{
MemberInfo = memberInfo,
MethodInfo = methodInfo,
Attributes = attributes.Cast<CrudeMethodAttribute>().ToList()
});
}
}

return items;
}
}

internal class CrudeMethod
internal record CrudeMethod
{
public MemberInfo MemberInfo { get; }
public MemberInfo MemberInfo { get; init; } = default!;

public MethodInfo MethodInfo { get; }
public MethodInfo MethodInfo { get; init; } = default!;

public List<CrudeMethodAttribute> Attributes { get; }

public CrudeMethod(MemberInfo memberInfo, MethodInfo methodInfo, List<CrudeMethodAttribute> attributes)
{
MemberInfo = memberInfo;
MethodInfo = methodInfo;
Attributes = attributes;
}
public List<CrudeMethodAttribute> Attributes { get; init; } = default!;
}

internal class CrudeProperty
internal record CrudeProperty
{
public string Name { get; }
public string Name { get; init; } = string.Empty;

public int Order { get; }
public int Order { get; init; }

public CrudePropertyType Type { get; }
public CrudePropertyType Type => Info.PropertyType.IsGenericBaseType(typeof(CrudeTable<>))
? CrudePropertyType.Table
: CrudePropertyType.Field;

public CrudeEvent? OnClick { get; }
public CrudeEvent? OnClick { get; init; }

public PropertyInfo Info { get; }
public PropertyInfo Info { get; init; } = default!;

public object ViewModel { get; }
public object ViewModel { get; init; } = default!;

public bool Disabled { get; }
public bool Disabled { get; init; }

public bool Password { get; }
public bool Password { get; init; }

public bool EmptyPlaceholder { get; }
public bool Placeholder { get; init; }

public string HtmlLabel { get; }
public string HtmlLabel { get; init; } = string.Empty;

public CrudeProperty(
string name,
int order,
CrudeEvent? onClick,
PropertyInfo info,
object viewModel,
bool disabled,
bool password,
bool emptyPlaceholder,
string htmlLabel)
{
Name = name;
Order = order;
OnClick = onClick;
Info = info;
ViewModel = viewModel;
Disabled = disabled;
Password = password;
EmptyPlaceholder = emptyPlaceholder;
HtmlLabel = htmlLabel;

if (Info.PropertyType.IsGenericBaseType(typeof(CrudeTable<>)))
{
Type = CrudePropertyType.Table;
}
else
{
Type = CrudePropertyType.Field;
}
}
public int SequenceNo { get; init; }

public object? GetValue()
{
Expand Down
2 changes: 1 addition & 1 deletion Crude/wwwroot/styles.css → Crude/wwwroot/Crude.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--grid-full-span: 1 / span 3
}

crude-tree {
.crude-tree {
display: grid;
grid-gap: 15px;
align-items: start;
Expand Down

0 comments on commit cb5b42a

Please sign in to comment.