Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.CodeAnalysis;

namespace Microsoft.DotNet.Tools.Scaffold.AspNet.Models;

/// <summary>
Expand All @@ -20,9 +22,15 @@ internal class BlazorCrudModel : CrudModel
/// Used in BlazorCrud Create/Edit templates.
/// </summary>
/// <param name="inputType">The .NET type name (e.g., "string", "int").</param>
/// <param name="isEnum">Whether the type is an enum.</param>
/// <returns>The Blazor input component type (e.g., "InputText").</returns>
public string GetInputType(string inputType)
public string GetInputType(string inputType, bool isEnum = false)
{
if (isEnum)
{
return "InputSelect";
}

if (string.IsNullOrEmpty(inputType))
{
return "InputText";
Expand Down Expand Up @@ -58,6 +66,24 @@ public string GetInputType(string inputType)
}
}

/// <summary>
/// Determines whether a Roslyn type symbol represents an enum or nullable enum.
/// </summary>
public bool IsEnumType(ITypeSymbol typeSymbol)
{
return typeSymbol.TypeKind == TypeKind.Enum || IsNullableEnumType(typeSymbol);
}

/// <summary>
/// Determines whether a Roslyn type symbol represents a nullable enum.
/// </summary>
public bool IsNullableEnumType(ITypeSymbol typeSymbol)
{
return typeSymbol is INamedTypeSymbol namedType &&
namedType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T &&
namedType.TypeArguments[0].TypeKind == TypeKind.Enum;
}

//used to get correct form class to add to BlazorCrud\Create.tt and BlazorCrud\Edit.tt template
/// <summary>
/// Gets the correct CSS class for a Blazor input component based on the .NET type name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public virtual string TransformText()
string modelPropertyName = property.Name;
string modelPropertyNameLowercase = modelPropertyName.ToLowerInvariant();
string propertyShortTypeName = property.Type.ToDisplayString().Replace("?", string.Empty);
var inputTypeName = Model.GetInputType(propertyShortTypeName);
var isEnum = Model.IsEnumType(property.Type);
var isNullableEnum = Model.IsNullableEnumType(property.Type);
var inputTypeName = Model.GetInputType(propertyShortTypeName, isEnum);
var inputClass = Model.GetInputClassType(propertyShortTypeName);
var ariaRequiredAttributeHtml = property.HasRequiredAttribute() ? "aria-required=\"true\"" : string.Empty;
var divWhitespace = new string(' ', 16);
Expand All @@ -80,7 +82,36 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
this.Write("\" class=\"form-label\">");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
this.Write(":</label> \r\n <");
this.Write(":</label> \r\n");

if (isEnum) {

this.Write(" <");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(" id=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
this.Write("\" @bind-Value=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelName));
this.Write(".");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
this.Write("\" class=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(inputClass));
this.Write("\" ");
this.Write(this.ToStringHelper.ToStringWithCulture(ariaRequiredAttributeHtml));
this.Write(">\r\n");
if (isNullableEnum) {

this.Write(" <option value=\"\">-- select --</option>\r\n");
}

this.Write(" @foreach (var value in Enum.GetValues<");
this.Write(this.ToStringHelper.ToStringWithCulture(propertyShortTypeName));
this.Write(">())\r\n {\r\n <option value=\"@value\">@value</option>\r\n }\r\n </");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(">\r\n");
} else {

this.Write(" <");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(" id=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
Expand All @@ -92,7 +123,10 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(inputClass));
this.Write("\" ");
this.Write(this.ToStringHelper.ToStringWithCulture(ariaRequiredAttributeHtml));
this.Write("/> \r\n <ValidationMessage For=\"() => ");
this.Write("/> \r\n");
}

this.Write(" <ValidationMessage For=\"() => ");
this.Write(this.ToStringHelper.ToStringWithCulture(modelName));
this.Write(".");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,29 @@
string modelPropertyName = property.Name;
string modelPropertyNameLowercase = modelPropertyName.ToLowerInvariant();
string propertyShortTypeName = property.Type.ToDisplayString().Replace("?", string.Empty);
var inputTypeName = Model.GetInputType(propertyShortTypeName);
var isEnum = Model.IsEnumType(property.Type);
var isNullableEnum = Model.IsNullableEnumType(property.Type);
var inputTypeName = Model.GetInputType(propertyShortTypeName, isEnum);
var inputClass = Model.GetInputClassType(propertyShortTypeName);
var ariaRequiredAttributeHtml = property.HasRequiredAttribute() ? "aria-required=\"true\"" : string.Empty;
var divWhitespace = new string(' ', 16);
var requiredSpanAttributeHtml = property.HasRequiredAttribute() ? $"\r\n{divWhitespace}<span class=\"text-danger\">*</span>" : string.Empty;
#>
<div class="mb-3"><#= requiredSpanAttributeHtml #>
<label for="<#= modelPropertyNameLowercase #>" class="form-label"><#= modelPropertyName #>:</label>
<# if (isEnum) { #>
<<#= inputTypeName #> id="<#= modelPropertyNameLowercase #>" @bind-Value="<#= modelName #>.<#= modelPropertyName #>" class="<#= inputClass #>" <#= ariaRequiredAttributeHtml #>>
<# if (isNullableEnum) { #>
<option value="">-- select --</option>
<# } #>
@foreach (var value in Enum.GetValues<<#= propertyShortTypeName #>>())
{
<option value="@value">@value</option>
}
</<#= inputTypeName #>>
<# } else { #>
<<#= inputTypeName #> id="<#= modelPropertyNameLowercase #>" @bind-Value="<#= modelName #>.<#= modelPropertyName #>" class="<#= inputClass #>" <#= ariaRequiredAttributeHtml #>/>
<# } #>
<ValidationMessage For="() => <#= modelName #>.<#= modelPropertyName #>" class="text-danger" />
</div>
<# } #>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public virtual string TransformText()
string modelPropertyName = property.Name;
string modelPropertyNameLowercase = modelPropertyName.ToLowerInvariant();
string propertyShortTypeName = property.Type.ToDisplayString().Replace("?", string.Empty);
var inputTypeName = Model.GetInputType(propertyShortTypeName);
var isEnum = Model.IsEnumType(property.Type);
var isNullableEnum = Model.IsNullableEnumType(property.Type);
var inputTypeName = Model.GetInputType(propertyShortTypeName, isEnum);
var inputClass = Model.GetInputClassType(propertyShortTypeName);
var ariaRequiredAttributeHtml = property.HasRequiredAttribute() ? "aria-required=\"true\"" : string.Empty;
var divWhitespace = new string(' ', 16);
Expand All @@ -95,7 +97,36 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
this.Write("\" class=\"form-label\">");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
this.Write(":</label>\r\n <");
this.Write(":</label>\r\n");

if (isEnum) {

this.Write(" <");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(" id=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
this.Write("\" @bind-Value=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelName));
this.Write(".");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
this.Write("\" class=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(inputClass));
this.Write("\" ");
this.Write(this.ToStringHelper.ToStringWithCulture(ariaRequiredAttributeHtml));
this.Write(">\r\n");
if (isNullableEnum) {

this.Write(" <option value=\"\">-- select --</option>\r\n");
}

this.Write(" @foreach (var value in Enum.GetValues<");
this.Write(this.ToStringHelper.ToStringWithCulture(propertyShortTypeName));
this.Write(">())\r\n {\r\n <option value=\"@value\">@value</option>\r\n }\r\n </");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(">\r\n");
} else {

this.Write(" <");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(" id=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
Expand All @@ -107,7 +138,10 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(inputClass));
this.Write("\" ");
this.Write(this.ToStringHelper.ToStringWithCulture(ariaRequiredAttributeHtml));
this.Write("/>\r\n <ValidationMessage For=\"() => ");
this.Write("/>\r\n");
}

this.Write(" <ValidationMessage For=\"() => ");
this.Write(this.ToStringHelper.ToStringWithCulture(modelName));
this.Write(".");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,29 @@ else
string modelPropertyName = property.Name;
string modelPropertyNameLowercase = modelPropertyName.ToLowerInvariant();
string propertyShortTypeName = property.Type.ToDisplayString().Replace("?", string.Empty);
var inputTypeName = Model.GetInputType(propertyShortTypeName);
var isEnum = Model.IsEnumType(property.Type);
var isNullableEnum = Model.IsNullableEnumType(property.Type);
var inputTypeName = Model.GetInputType(propertyShortTypeName, isEnum);
var inputClass = Model.GetInputClassType(propertyShortTypeName);
var ariaRequiredAttributeHtml = property.HasRequiredAttribute() ? "aria-required=\"true\"" : string.Empty;
var divWhitespace = new string(' ', 16);
var requiredSpanAttributeHtml = property.HasRequiredAttribute() ? $"\r\n{divWhitespace}<span class=\"text-danger\">*</span>" : string.Empty;
#>
<div class="mb-3"><#= requiredSpanAttributeHtml #>
<label for="<#= modelPropertyNameLowercase #>" class="form-label"><#= modelPropertyName #>:</label>
<# if (isEnum) { #>
<<#= inputTypeName #> id="<#= modelPropertyNameLowercase #>" @bind-Value="<#= modelName #>.<#= modelPropertyName #>" class="<#= inputClass #>" <#= ariaRequiredAttributeHtml #>>
<# if (isNullableEnum) { #>
<option value="">-- select --</option>
<# } #>
@foreach (var value in Enum.GetValues<<#= propertyShortTypeName #>>())
{
<option value="@value">@value</option>
}
</<#= inputTypeName #>>
<# } else { #>
<<#= inputTypeName #> id="<#= modelPropertyNameLowercase #>" @bind-Value="<#= modelName #>.<#= modelPropertyName #>" class="<#= inputClass #>" <#= ariaRequiredAttributeHtml #>/>
<# } #>
<ValidationMessage For="() => <#= modelName #>.<#= modelPropertyName #>" class="text-danger" />
</div>
<# } #>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public virtual string TransformText()
string modelPropertyName = property.Name;
string modelPropertyNameLowercase = modelPropertyName.ToLowerInvariant();
string propertyShortTypeName = property.Type.ToDisplayString().Replace("?", string.Empty);
var inputTypeName = Model.GetInputType(propertyShortTypeName);
var isEnum = Model.IsEnumType(property.Type);
var isNullableEnum = Model.IsNullableEnumType(property.Type);
var inputTypeName = Model.GetInputType(propertyShortTypeName, isEnum);
var inputClass = Model.GetInputClassType(propertyShortTypeName);
var ariaRequiredAttributeHtml = property.HasRequiredAttribute() ? "aria-required=\"true\"" : string.Empty;
var divWhitespace = new string(' ', 16);
Expand All @@ -80,7 +82,9 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
this.Write("\" class=\"form-label\">");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
this.Write(":</label> \r\n <");
this.Write(":</label> \r\n");
if (isEnum) {
this.Write(" <");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(" id=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
Expand All @@ -92,7 +96,32 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(inputClass));
this.Write("\" ");
this.Write(this.ToStringHelper.ToStringWithCulture(ariaRequiredAttributeHtml));
this.Write("/> \r\n <ValidationMessage For=\"() => ");
this.Write(">\r\n");
if (isNullableEnum) {
this.Write(" <option value=\"\">-- select --</option>\r\n");
}
this.Write(" @foreach (var value in Enum.GetValues<");
this.Write(this.ToStringHelper.ToStringWithCulture(propertyShortTypeName));
this.Write(">())\r\n {\r\n <option value=\"@value\">@valu" +
"e</option>\r\n }\r\n </");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(">\r\n");
} else {
this.Write(" <");
this.Write(this.ToStringHelper.ToStringWithCulture(inputTypeName));
this.Write(" id=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyNameLowercase));
this.Write("\" @bind-Value=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(modelName));
this.Write(".");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
this.Write("\" class=\"");
this.Write(this.ToStringHelper.ToStringWithCulture(inputClass));
this.Write("\" ");
this.Write(this.ToStringHelper.ToStringWithCulture(ariaRequiredAttributeHtml));
this.Write("/> \r\n");
}
this.Write(" <ValidationMessage For=\"() => ");
this.Write(this.ToStringHelper.ToStringWithCulture(modelName));
this.Write(".");
this.Write(this.ToStringHelper.ToStringWithCulture(modelPropertyName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,29 @@
string modelPropertyName = property.Name;
string modelPropertyNameLowercase = modelPropertyName.ToLowerInvariant();
string propertyShortTypeName = property.Type.ToDisplayString().Replace("?", string.Empty);
var inputTypeName = Model.GetInputType(propertyShortTypeName);
var isEnum = Model.IsEnumType(property.Type);
var isNullableEnum = Model.IsNullableEnumType(property.Type);
var inputTypeName = Model.GetInputType(propertyShortTypeName, isEnum);
var inputClass = Model.GetInputClassType(propertyShortTypeName);
var ariaRequiredAttributeHtml = property.HasRequiredAttribute() ? "aria-required=\"true\"" : string.Empty;
var divWhitespace = new string(' ', 16);
var requiredSpanAttributeHtml = property.HasRequiredAttribute() ? $"\r\n{divWhitespace}<span class=\"text-danger\">*</span>" : string.Empty;
#>
<div class="mb-3"><#= requiredSpanAttributeHtml #>
<label for="<#= modelPropertyNameLowercase #>" class="form-label"><#= modelPropertyName #>:</label>
<# if (isEnum) { #>
<<#= inputTypeName #> id="<#= modelPropertyNameLowercase #>" @bind-Value="<#= modelName #>.<#= modelPropertyName #>" class="<#= inputClass #>" <#= ariaRequiredAttributeHtml #>>
<# if (isNullableEnum) { #>
<option value="">-- select --</option>
<# } #>
@foreach (var value in Enum.GetValues<<#= propertyShortTypeName #>>())
{
<option value="@value">@value</option>
}
</<#= inputTypeName #>>
<# } else { #>
<<#= inputTypeName #> id="<#= modelPropertyNameLowercase #>" @bind-Value="<#= modelName #>.<#= modelPropertyName #>" class="<#= inputClass #>" <#= ariaRequiredAttributeHtml #>/>
<# } #>
<ValidationMessage For="() => <#= modelName #>.<#= modelPropertyName #>" class="text-danger" />
</div>
<# } #>
Expand Down
Loading