Skip to content
Draft
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
Expand Up @@ -37,6 +37,13 @@
<form-checkbox asp-for="Item3" container-class=""></form-checkbox>
<form-checkbox asp-for="Item4" container-class=""></form-checkbox>
</div>
<div class="mb-3">
<label>Inline Options</label>
<form-checkbox asp-for="Item1" container-class="" is-inline="true"></form-checkbox>
<form-checkbox asp-for="Item2" container-class="" is-inline="true"></form-checkbox>
<form-checkbox asp-for="Item3" container-class="" is-inline="true"></form-checkbox>
<form-checkbox asp-for="Item4" container-class="" is-inline="true"></form-checkbox>
</div>

<bs-button type="Submit" value="Click Here To Test Validation">Click Here To Test Validation</bs-button>
</form>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Form;
using ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests.FromFramework;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Xunit.Abstractions;

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

[UsesVerify]
public sealed class FormCheckboxTagHelperTests : ModelTagHelperTest<FormCheckboxTagHelper, TestModel>
{
public FormCheckboxTagHelperTests(ITestOutputHelper output) : base(output)
{

}

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

var tagHelper = GetTagHelper(htmlGenerator, model: false, propertyName: nameof(TestModel.CheckboxField));
var output = await tagHelper.Render();
await VerifyTagHelper(output);
}

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

var tagHelper = GetTagHelper(htmlGenerator, model: false, propertyName: nameof(TestModel.CheckboxField));
tagHelper.IsInline = true;
var output = await tagHelper.Render();
await VerifyTagHelper(output);
}

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

var tagHelper = GetTagHelper(htmlGenerator, model: false, propertyName: nameof(TestModel.CheckboxField));
tagHelper.IsSwitch = true;
var output = await tagHelper.Render();
await VerifyTagHelper(output);
}

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

var tagHelper = GetTagHelper(htmlGenerator, model: false, propertyName: nameof(TestModel.CheckboxField));
tagHelper.IsInline = true;
tagHelper.IsSwitch = true;
var output = await tagHelper.Render();
await VerifyTagHelper(output);
}

internal override FormCheckboxTagHelper TagHelperFactory(IHtmlGenerator htmlGenerator, ModelExpression modelExpression, ViewContext viewContext)
=> new(htmlGenerator) { For = modelExpression, ViewContext = viewContext };
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public class TestModel

[Required]
public int? RequiredIntField { get; set; }

public bool CheckboxField { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

<div class="form-check mb-3">
<label class="HtmlEncode[[form-check-label]]" for="HtmlEncode[[CheckboxField]]">HtmlEncode[[CheckboxField]]</label>
<input type="HtmlEncode[[checkbox]]" id="HtmlEncode[[CheckboxField]]" name="HtmlEncode[[CheckboxField]]" value="HtmlEncode[[true]]" class="HtmlEncode[[form-check-input]]">
<input name="HtmlEncode[[CheckboxField]]" type="HtmlEncode[[hidden]]" value="HtmlEncode[[false]]">
<span class="HtmlEncode[[text-danger field-validation-valid]]" data-valmsg-for="HtmlEncode[[CheckboxField]]" data-valmsg-replace="HtmlEncode[[true]]"></span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

<div class="form-check mb-3 form-check-inline">
<label class="HtmlEncode[[form-check-label]]" for="HtmlEncode[[CheckboxField]]">HtmlEncode[[CheckboxField]]</label>
<input type="HtmlEncode[[checkbox]]" id="HtmlEncode[[CheckboxField]]" name="HtmlEncode[[CheckboxField]]" value="HtmlEncode[[true]]" class="HtmlEncode[[form-check-input]]">
<input name="HtmlEncode[[CheckboxField]]" type="HtmlEncode[[hidden]]" value="HtmlEncode[[false]]">
<span class="HtmlEncode[[text-danger field-validation-valid]]" data-valmsg-for="HtmlEncode[[CheckboxField]]" data-valmsg-replace="HtmlEncode[[true]]"></span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

<div class="form-check mb-3 form-switch form-check-inline">
<label class="HtmlEncode[[form-check-label]]" for="HtmlEncode[[CheckboxField]]">HtmlEncode[[CheckboxField]]</label>
<input type="HtmlEncode[[checkbox]]" id="HtmlEncode[[CheckboxField]]" name="HtmlEncode[[CheckboxField]]" value="HtmlEncode[[true]]" class="HtmlEncode[[form-check-input]]" role="HtmlEncode[[switch]]">
<input name="HtmlEncode[[CheckboxField]]" type="HtmlEncode[[hidden]]" value="HtmlEncode[[false]]">
<span class="HtmlEncode[[text-danger field-validation-valid]]" data-valmsg-for="HtmlEncode[[CheckboxField]]" data-valmsg-replace="HtmlEncode[[true]]"></span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

<div class="form-check mb-3 form-switch">
<label class="HtmlEncode[[form-check-label]]" for="HtmlEncode[[CheckboxField]]">HtmlEncode[[CheckboxField]]</label>
<input type="HtmlEncode[[checkbox]]" id="HtmlEncode[[CheckboxField]]" name="HtmlEncode[[CheckboxField]]" value="HtmlEncode[[true]]" class="HtmlEncode[[form-check-input]]" role="HtmlEncode[[switch]]">
<input name="HtmlEncode[[CheckboxField]]" type="HtmlEncode[[hidden]]" value="HtmlEncode[[false]]">
<span class="HtmlEncode[[text-danger field-validation-valid]]" data-valmsg-for="HtmlEncode[[CheckboxField]]" data-valmsg-replace="HtmlEncode[[true]]"></span>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
/// </summary>
public bool IsSwitch { get; set; } = false;

/// <summary>
/// Controls if this should be rendered inline
/// </summary>
public bool IsInline { get; set; } = false;

Check notice on line 38 in src/AspNetCore.Utilities.Bootstrap5TagHelpers/Form/FormCheckboxTagHelper.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/AspNetCore.Utilities.Bootstrap5TagHelpers/Form/FormCheckboxTagHelper.cs#L38

Remove this initialization to 'IsInline', the compiler will do that for you.


/// <summary>
/// Public constructor that will receive the incoming generator to leverage existing Microsoft Tag Helpers
Expand Down Expand Up @@ -70,6 +75,10 @@
groupClass += " form-switch";
output.Attributes.Add("role", "switch");
}
if (IsInline)
{
groupClass += " form-check-inline";
}
this.StartFormGroup(output, groupClass);

//Generate our label if not inline
Expand Down