Skip to content

Commit

Permalink
Merge pull request #27 from IowaComputerGurus/feature/updated-for-req…
Browse files Browse the repository at this point in the history
…uired

Added Ability to Designate Required Fields
  • Loading branch information
mitchelsellers committed Mar 5, 2024
2 parents 32548d2 + 942b401 commit 223091b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 35 deletions.
34 changes: 10 additions & 24 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,6 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Cache SonarCloud packages
uses: actions/[email protected]
with:
path: ~\sonar\cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar

- name: Cache SonarCloud scanner
id: cache-sonar-scanner
uses: actions/[email protected]
with:
path: .\.sonar\scanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner

- name: Install SonarCloud scanner
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
shell: powershell
run: |
New-Item -Path .\.sonar\scanner -ItemType Directory
dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner

- name: Install GitVersion
run: dotnet tool install --global GitVersion.Tool
Expand All @@ -62,8 +40,16 @@ jobs:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
shell: powershell
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers" /o:"iowacomputergurus-github" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /n:"AspNetCore Utilities Bootstrap5 Tag Helpers"
dotnet restore "${{ env.solution-path }}"
dotnet build "${{ env.solution-path }}" --no-restore --configuration Release -p:version=${{ steps.gitversion.outputs.majorMinorPatch }}
dotnet test "${{ env.solution-path }}" --no-build --configuration Release --collect "XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover --logger "trx;LogFileName=unittests.trx"
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
- name: Push Coverage to Codacy
shell: bash
env:
CODACY_ORGANIZATION_PROVIDER: ${{ secrets.CODACY_ORGANIZATION_PROVIDER }}
CODACY_USERNAME: ${{ secrets.CODACY_USERNAME }}
CODACY_PROJECT_NAME: ${{ secrets.CODACY_PROJECT_NAME }}
CODACY_API_TOKEN: ${{ secrets.CODACY_API_TOKEN }}
run: |
bash <(curl -Ls https://coverage.codacy.com/get.sh) report $(find . -name '*.opencover.xml' -printf '-r %p ')
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ A collection of TagHelpers for ASP.NET Core that make utilizing the Bootstrap 5.

![](https://img.shields.io/nuget/v/icg.aspnetcore.utilities.bootstrap5taghelpers.svg) ![](https://img.shields.io/nuget/dt/icg.aspnetcore.utilities.bootstrap5taghelpers.svg)

## SonarCloud Analysis

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers&metric=alert_status)](https://sonarcloud.io/dashboard?id=IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers&metric=coverage)](https://sonarcloud.io/dashboard?id=IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers&metric=security_rating)](https://sonarcloud.io/dashboard?id=IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers)
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers&metric=sqale_index)](https://sonarcloud.io/dashboard?id=IowaComputerGurus_aspnetcore.utilities.bootstrap5taghelpers)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/6bfab3e64b8d4138aefc6152d39bd753)](https://app.codacy.com/gh/IowaComputerGurus/aspnetcore.utilities.bootstrap5taghelpers/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)

## Usage Expectations

Expand Down Expand Up @@ -77,4 +72,13 @@ At this time tag helpers have been implemented for the following elements.

If you find that we are missing a particular tag helper, we welcome contributions!

##
## Special Features

The tag helpers automatically inspect form elements and add a "required" css class in addition to the `form-label` class default allowing for indication of required fields easily with CSS such as the following.

```` css
label.required:after {
content: " *";
color: red;
}
````
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public class SampleModel
public string ReadOnlyField { get; set; } = "Readonly Field Example";

[Display(Name = "Select List Item")]
public SampleEnum SelectedListItem { get; set; }
public SampleEnum? SelectedListItem { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" integrity="sha512-9usAa10IRO0HhonpyAIVpjrylPvoDwiPUiKdWk5t3PyolY1cOd4DSE0Ga+ri4AuTroPR5aQvXU9xC6qOPnzFeg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<style>
label.required:after {
content: " *";
color: red;
}
</style>
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ public async Task Renders_PlainTextReadOnly()
await VerifyTagHelper(output);
}

[Fact]
public async Task Renders_RequiredClassWhenNeeded()
{
var metadataProvider = new TestModelMetadataProvider();
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);

var tagHelper = GetTagHelper(htmlGenerator, model: new TestModel(), propertyName: nameof(TestModel.RequiredIntField));
tagHelper.PlainTextReadOnly = true;
var output = await tagHelper.Render();
await VerifyTagHelper(output);
}

internal override FormInputTagHelper TagHelperFactory(IHtmlGenerator htmlGenerator, ModelExpression modelExpression, ViewContext viewContext)
=> new(htmlGenerator) { For = modelExpression, ViewContext = viewContext };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests.Form;
using System.ComponentModel.DataAnnotations;

namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests.Form;
public class TestModel
{
public string TextField { get; set; }

[Required]
public int? RequiredIntField { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

<div class="mb-3">
<label class="HtmlEncode[[form-label required]]" for="HtmlEncode[[RequiredIntField]]">HtmlEncode[[RequiredIntField]]</label>
<input type="HtmlEncode[[number]]" id="HtmlEncode[[RequiredIntField]]" name="HtmlEncode[[RequiredIntField]]" value="HtmlEncode[[ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests.Form.TestModel]]" class="HtmlEncode[[form-control-plaintext]]" readonly="HtmlEncode[[readonly]]">
</div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Form;

Expand Down Expand Up @@ -34,12 +36,15 @@ public static void EndFormGroup(this IFormElementMixin element, TagHelperOutput

public static void AddLabel(this IFormElementMixin element, TagHelperOutput output)
{
//Find out if required to add special class
var isRequired = element.For.ModelExplorer.Metadata.ValidatorMetadata.Any(o => o is RequiredAttribute);
var targetClass = isRequired ? "form-label required" : "form-label";
//Generate our label
var label = element.HtmlGenerator.GenerateLabel(
element.ViewContext,
element.For.ModelExplorer,
element.For.Name, null,
new { @class = "form-label" });
new { @class = targetClass });
output.PreElement.AppendHtml(label);
}

Expand Down

0 comments on commit 223091b

Please sign in to comment.