Skip to content

Commit

Permalink
Merge pull request #37 from IowaComputerGurus/features/new-stuff
Browse files Browse the repository at this point in the history
Added support for Spinenrs
  • Loading branch information
mitchelsellers authored Nov 15, 2024
2 parents caabea5 + 07976a9 commit 52982b0
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests;

[UsesVerify]
public class ButtonTagHelperTests : LoggingTagHelperTest
public class ButtonTagHelperTests(ITestOutputHelper output) : LoggingTagHelperTest(output)
{
public ButtonTagHelperTests(ITestOutputHelper output) : base(output)
{

}

[Fact]
public async Task Should_Not_Render_If_HideDisplay_Is_True()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Spinner;
using Xunit.Abstractions;

namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests.Spinner;

[UsesVerify]
public class SpinnerTagHelperTests(ITestOutputHelper output) : LoggingTagHelperTest(output)
{
[Fact]
public async Task Should_Render_BorderSpinner_Default()
{
var output = await (new SpinnerTagHelper()).Render();
output.AssertContainsClass("spinner-border");
await VerifyTagHelper(output);
}

[Theory]
[InlineData(SpinnerMode.Border, "spinner-border")]
[InlineData(SpinnerMode.Grow, "spinner-grow")]
public async Task Properly_Sets_Class_For_Spinner(SpinnerMode mode, string expected)
{
var output = await (new SpinnerTagHelper() { SpinnerMode = mode }).Render();
output.AssertContainsClass(expected);
await VerifyTagHelper(output).UseParameters(mode);
}

[Theory]
[InlineData(SpinnerMode.Border, "spinner-border-sm")]
[InlineData(SpinnerMode.Grow, "spinner-grow-sm")]
public async Task Properly_Sets_Class_For_Spinner_Small(SpinnerMode mode, string expected)
{
var output = await (new SpinnerTagHelper() { SpinnerMode = mode, IsSmall = true}).Render();
output.AssertContainsClass(expected);
await VerifyTagHelper(output).UseParameters(mode);
}

[Theory]
[InlineData(BootstrapColor.Info, "text-info")]
[InlineData(BootstrapColor.Success, "text-success")]
[InlineData(BootstrapColor.Danger, "text-danger")]
[InlineData(BootstrapColor.Warning, "text-warning")]
[InlineData(BootstrapColor.Primary, "text-primary")]
[InlineData(BootstrapColor.Secondary, "text-secondary")]
[InlineData(BootstrapColor.Light, "text-light")]
[InlineData(BootstrapColor.Dark, "text-dark")]
public async Task Properly_Sets_Class_For_TextColor(BootstrapColor color, string expected)
{
var output = await (new SpinnerTagHelper() { Color = color}).Render();
output.AssertContainsClass(expected);
await VerifyTagHelper(output).UseParameters(color);
}

[Fact]
public async Task Should_Render_Aria_Hidden_WhenSet()
{
var output = await (new SpinnerTagHelper() { AriaHidden = true }).Render();
await VerifyTagHelper(output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border spinner-border-sm"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-grow spinner-grow-sm"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="HtmlEncode[[spinner-border]]"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="HtmlEncode[[spinner-grow]]"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border text-danger"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border text-dark"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border text-info"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border text-light"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border text-primary"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border text-secondary"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border text-success"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="spinner-border text-warning"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="HtmlEncode[[spinner-border]]" aria-hidden="HtmlEncode[[true]]"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<div class="HtmlEncode[[spinner-border]]"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Text.Encodings.Web;

namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Spinner;

/// <summary>
/// The options for the spinner mode
/// </summary>
public enum SpinnerMode
{
/// <summary>
/// Will render with .spinner-border
/// </summary>
Border = 1,

/// <summary>
/// Will render with .spinner-grow
/// </summary>
Grow = 2
}

/// <summary>
/// A tag helper for working with bootstrap spinners
/// </summary>
[HtmlTargetElement("bs-spinner")]
public class SpinnerTagHelper : TagHelper
{
/// <summary>
/// The mode of the spinner
/// </summary>
public SpinnerMode SpinnerMode { get; set; } = SpinnerMode.Border;

/// <summary>
/// The size of the spinner
/// </summary>
public bool IsSmall { get; set; } = false;

/// <summary>
/// If set to true the element will render with an aria-hidden attribute with a value of true
/// </summary>
public bool AriaHidden { get; set; } = false;

/// <summary>
/// If set will render the spinner with a color
/// </summary>
[HtmlAttributeName("bs-color")]
public BootstrapColor? Color { get; set; }

/// <summary>
/// Processes the tag helper
/// </summary>
/// <param name="context"></param>
/// <param name="output"></param>
public override void Process(TagHelperContext context, TagHelperOutput output)
{
//Add
output.TagName = "div";
var modeClass = SpinnerMode == SpinnerMode.Border ? "spinner-border" : "spinner-grow";
output.AddClass(modeClass, HtmlEncoder.Default);
if (IsSmall)
{
output.AddClass($"{modeClass}-sm", HtmlEncoder.Default);
}

if (Color.HasValue)
{
output.AddClass($"text-{Color.Value.ToString().ToLowerInvariant()}", HtmlEncoder.Default);
}

if (AriaHidden)
{
output.Attributes.Add("aria-hidden", "true");
}
}
}

0 comments on commit 52982b0

Please sign in to comment.