Skip to content

Commit 52982b0

Browse files
Merge pull request #37 from IowaComputerGurus/features/new-stuff
Added support for Spinenrs
2 parents caabea5 + 07976a9 commit 52982b0

17 files changed

+164
-6
lines changed

src/AspNetCore.Utilities.Bootstrap5TagHelpers.Tests/ButtonTagHelperTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests;
66

77
[UsesVerify]
8-
public class ButtonTagHelperTests : LoggingTagHelperTest
8+
public class ButtonTagHelperTests(ITestOutputHelper output) : LoggingTagHelperTest(output)
99
{
10-
public ButtonTagHelperTests(ITestOutputHelper output) : base(output)
11-
{
12-
13-
}
14-
1510
[Fact]
1611
public async Task Should_Not_Render_If_HideDisplay_Is_True()
1712
{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Spinner;
2+
using Xunit.Abstractions;
3+
4+
namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests.Spinner;
5+
6+
[UsesVerify]
7+
public class SpinnerTagHelperTests(ITestOutputHelper output) : LoggingTagHelperTest(output)
8+
{
9+
[Fact]
10+
public async Task Should_Render_BorderSpinner_Default()
11+
{
12+
var output = await (new SpinnerTagHelper()).Render();
13+
output.AssertContainsClass("spinner-border");
14+
await VerifyTagHelper(output);
15+
}
16+
17+
[Theory]
18+
[InlineData(SpinnerMode.Border, "spinner-border")]
19+
[InlineData(SpinnerMode.Grow, "spinner-grow")]
20+
public async Task Properly_Sets_Class_For_Spinner(SpinnerMode mode, string expected)
21+
{
22+
var output = await (new SpinnerTagHelper() { SpinnerMode = mode }).Render();
23+
output.AssertContainsClass(expected);
24+
await VerifyTagHelper(output).UseParameters(mode);
25+
}
26+
27+
[Theory]
28+
[InlineData(SpinnerMode.Border, "spinner-border-sm")]
29+
[InlineData(SpinnerMode.Grow, "spinner-grow-sm")]
30+
public async Task Properly_Sets_Class_For_Spinner_Small(SpinnerMode mode, string expected)
31+
{
32+
var output = await (new SpinnerTagHelper() { SpinnerMode = mode, IsSmall = true}).Render();
33+
output.AssertContainsClass(expected);
34+
await VerifyTagHelper(output).UseParameters(mode);
35+
}
36+
37+
[Theory]
38+
[InlineData(BootstrapColor.Info, "text-info")]
39+
[InlineData(BootstrapColor.Success, "text-success")]
40+
[InlineData(BootstrapColor.Danger, "text-danger")]
41+
[InlineData(BootstrapColor.Warning, "text-warning")]
42+
[InlineData(BootstrapColor.Primary, "text-primary")]
43+
[InlineData(BootstrapColor.Secondary, "text-secondary")]
44+
[InlineData(BootstrapColor.Light, "text-light")]
45+
[InlineData(BootstrapColor.Dark, "text-dark")]
46+
public async Task Properly_Sets_Class_For_TextColor(BootstrapColor color, string expected)
47+
{
48+
var output = await (new SpinnerTagHelper() { Color = color}).Render();
49+
output.AssertContainsClass(expected);
50+
await VerifyTagHelper(output).UseParameters(color);
51+
}
52+
53+
[Fact]
54+
public async Task Should_Render_Aria_Hidden_WhenSet()
55+
{
56+
var output = await (new SpinnerTagHelper() { AriaHidden = true }).Render();
57+
await VerifyTagHelper(output);
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+

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

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

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

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

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

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

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

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

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

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

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

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

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

2+
<div class="HtmlEncode[[spinner-border]]"></div>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Microsoft.AspNetCore.Mvc.TagHelpers;
2+
using Microsoft.AspNetCore.Razor.TagHelpers;
3+
using System.Text.Encodings.Web;
4+
5+
namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Spinner;
6+
7+
/// <summary>
8+
/// The options for the spinner mode
9+
/// </summary>
10+
public enum SpinnerMode
11+
{
12+
/// <summary>
13+
/// Will render with .spinner-border
14+
/// </summary>
15+
Border = 1,
16+
17+
/// <summary>
18+
/// Will render with .spinner-grow
19+
/// </summary>
20+
Grow = 2
21+
}
22+
23+
/// <summary>
24+
/// A tag helper for working with bootstrap spinners
25+
/// </summary>
26+
[HtmlTargetElement("bs-spinner")]
27+
public class SpinnerTagHelper : TagHelper
28+
{
29+
/// <summary>
30+
/// The mode of the spinner
31+
/// </summary>
32+
public SpinnerMode SpinnerMode { get; set; } = SpinnerMode.Border;
33+
34+
/// <summary>
35+
/// The size of the spinner
36+
/// </summary>
37+
public bool IsSmall { get; set; } = false;
38+
39+
/// <summary>
40+
/// If set to true the element will render with an aria-hidden attribute with a value of true
41+
/// </summary>
42+
public bool AriaHidden { get; set; } = false;
43+
44+
/// <summary>
45+
/// If set will render the spinner with a color
46+
/// </summary>
47+
[HtmlAttributeName("bs-color")]
48+
public BootstrapColor? Color { get; set; }
49+
50+
/// <summary>
51+
/// Processes the tag helper
52+
/// </summary>
53+
/// <param name="context"></param>
54+
/// <param name="output"></param>
55+
public override void Process(TagHelperContext context, TagHelperOutput output)
56+
{
57+
//Add
58+
output.TagName = "div";
59+
var modeClass = SpinnerMode == SpinnerMode.Border ? "spinner-border" : "spinner-grow";
60+
output.AddClass(modeClass, HtmlEncoder.Default);
61+
if (IsSmall)
62+
{
63+
output.AddClass($"{modeClass}-sm", HtmlEncoder.Default);
64+
}
65+
66+
if (Color.HasValue)
67+
{
68+
output.AddClass($"text-{Color.Value.ToString().ToLowerInvariant()}", HtmlEncoder.Default);
69+
}
70+
71+
if (AriaHidden)
72+
{
73+
output.Attributes.Add("aria-hidden", "true");
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)