Skip to content

Commit 8095c8c

Browse files
Merge pull request #45 from IowaComputerGurus/feature/form-check-support
Added support for checkboxes
2 parents 4ba3a81 + 223e59a commit 8095c8c

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

src/AspNetCore.Utilities.Bootstrap5TagHelpers.Sample/Models/SampleModel.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,23 @@ public class SampleModel
3939

4040
[Display(Name = "Select List Item")]
4141
public SampleEnum? SelectedListItem { get; set; }
42+
43+
[Display(Name = "Set Default Password")]
44+
public bool SetDefaultPassword { get; set; }
45+
46+
[Display(Name = "Agree to Terms (Disabled)")]
47+
public bool AgreeToTerms { get; set; } = true;
48+
49+
[Display(Name = "Item 1")]
50+
public bool Item1 { get; set; }
51+
52+
[Display(Name = "Item 2")]
53+
public bool Item2 { get; set; }
54+
55+
[Display(Name = "Item 3")]
56+
public bool Item3 { get; set; }
57+
58+
[Display(Name = "Item 4")]
59+
public bool Item4 { get; set; }
4260
}
4361
}

src/AspNetCore.Utilities.Bootstrap5TagHelpers.Sample/Views/Home/StandardForm.cshtml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@
2828
</form-input>
2929
<form-select asp-for="SelectedListItem" asp-items="Html.GetEnumSelectList<SampleEnum>()">
3030
</form-select>
31-
31+
<form-checkbox asp-for="SetDefaultPassword" is-switch="true"></form-checkbox>
32+
<form-checkbox asp-for="AgreeToTerms" disabled="true"></form-checkbox>
33+
<div class="mb-3">
34+
<label>Select Your Options</label>
35+
<form-checkbox asp-for="Item1" container-class=""></form-checkbox>
36+
<form-checkbox asp-for="Item2" container-class=""></form-checkbox>
37+
<form-checkbox asp-for="Item3" container-class=""></form-checkbox>
38+
<form-checkbox asp-for="Item4" container-class=""></form-checkbox>
39+
</div>
40+
3241
<bs-button type="Submit" value="Click Here To Test Validation">Click Here To Test Validation</bs-button>
3342
</form>
3443
</div>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Microsoft.AspNetCore.Mvc.TagHelpers;
2+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
3+
using Microsoft.AspNetCore.Razor.TagHelpers;
4+
using Microsoft.VisualBasic;
5+
using System;
6+
using System.Text.Encodings.Web;
7+
8+
namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Form;
9+
10+
/// <summary>
11+
/// TagHelper for rending Bootstrap form compliant checkbox controls with support for ASP.NET Core model Binding. Will
12+
/// include Label, Field, and validation.
13+
/// </summary>
14+
[RestrictChildren("form-checkbox")]
15+
public class FormCheckboxTagHelper : InputTagHelper, IFormElementMixin
16+
{
17+
/// <inheritdoc />
18+
public IHtmlGenerator HtmlGenerator { get; }
19+
20+
/// <summary>
21+
/// The CSS class that should be applied to the containing div, in addition to that of the form-check that is required
22+
/// </summary>
23+
public string ContainerClass { get; set; } = "mb-3";
24+
25+
/// <summary>
26+
/// Indicator if the input should be rendered as disabled
27+
/// </summary>
28+
public bool Disabled { get; set; } = false;
29+
30+
/// <summary>
31+
/// Controls if this should be rendered as a switch
32+
/// </summary>
33+
public bool IsSwitch { get; set; } = false;
34+
35+
36+
/// <summary>
37+
/// Public constructor that will receive the incoming generator to leverage existing Microsoft Tag Helpers
38+
/// </summary>
39+
/// <param name="generator"></param>
40+
public FormCheckboxTagHelper(IHtmlGenerator generator) : base(generator)
41+
{
42+
HtmlGenerator = generator;
43+
}
44+
45+
/// <summary>
46+
/// Used to actually process the tag helper
47+
/// </summary>
48+
/// <param name="context"></param>
49+
/// <param name="output"></param>
50+
public override void Process(TagHelperContext context, TagHelperOutput output)
51+
{
52+
//Call our base implementation
53+
base.Process(context, output);
54+
55+
//Set our tag name
56+
output.TagName = "input";
57+
58+
//Add the form-control class
59+
if (Disabled)
60+
{
61+
output.Attributes.Add("disabled", "");
62+
}
63+
64+
output.AddClass("form-check-input", HtmlEncoder.Default);
65+
66+
//Add before div
67+
var groupClass = $"form-check {ContainerClass}";
68+
if (IsSwitch)
69+
{
70+
groupClass += " form-switch";
71+
output.Attributes.Add("role", "switch");
72+
}
73+
this.StartFormGroup(output, groupClass);
74+
75+
//Generate our label if not inline
76+
this.AddLabel(output, "form-check-label");
77+
78+
//Now, add validation message AFTER the field if it is not disabled
79+
if (!Disabled)
80+
{
81+
this.AddValidationMessage(output);
82+
}
83+
84+
//Close wrapping div
85+
this.EndFormGroup(output);
86+
}
87+
}

src/AspNetCore.Utilities.Bootstrap5TagHelpers/Form/FormElementMixin.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,24 @@ public static void StartFormGroup(this IFormElementMixin element, TagHelperOutpu
3434
public static void EndFormGroup(this IFormElementMixin element, TagHelperOutput output)
3535
=> output.PostElement.AppendHtml("</div>");
3636

37-
public static void AddLabel(this IFormElementMixin element, TagHelperOutput output)
37+
public static void AddLabel(this IFormElementMixin element, TagHelperOutput output, string cssClass = "form-label", bool isPostElement = false)
3838
{
3939
//Find out if required to add special class
4040
var isRequired = element.For.ModelExplorer.Metadata.ValidatorMetadata.Any(o => o is RequiredAttribute);
41-
var targetClass = isRequired ? "form-label required" : "form-label";
41+
var targetClass = isRequired ? $"{cssClass} required" : cssClass;
4242
//Generate our label
4343
var label = element.HtmlGenerator.GenerateLabel(
4444
element.ViewContext,
4545
element.For.ModelExplorer,
4646
element.For.Name, null,
4747
new { @class = targetClass });
48-
output.PreElement.AppendHtml(label);
48+
if (isPostElement)
49+
output.PostElement.AppendHtml(label);
50+
else
51+
output.PreElement.AppendHtml(label);
4952
}
5053

54+
5155
public static void AddValidationMessage(this IFormElementMixin element, TagHelperOutput output)
5256
{
5357
var validationMsg = element.HtmlGenerator.GenerateValidationMessage(

0 commit comments

Comments
 (0)