From 9652175c689218a1d431a587df5fe550f764e1a8 Mon Sep 17 00:00:00 2001 From: Mitchel Sellers Date: Mon, 17 Feb 2025 13:59:42 -0600 Subject: [PATCH] Added support for Alerts Headers #44 --- .../Views/Home/Index.cshtml | 38 +++++++++++-------- .../AlertTagHelper.cs | 18 ++++++++- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/AspNetCore.Utilities.Bootstrap5TagHelpers.Sample/Views/Home/Index.cshtml b/src/AspNetCore.Utilities.Bootstrap5TagHelpers.Sample/Views/Home/Index.cshtml index 200c116..474c069 100644 --- a/src/AspNetCore.Utilities.Bootstrap5TagHelpers.Sample/Views/Home/Index.cshtml +++ b/src/AspNetCore.Utilities.Bootstrap5TagHelpers.Sample/Views/Home/Index.cshtml @@ -73,21 +73,29 @@ - - Warning Dismissible - - - This is a dismissible warning alert! - - - - - <alert alert-color="Warning" dismissible="false"> - This is a dismissible warning alert! - </alert> - - - + + Warning Dismissible + + + This is a dismissible warning alert! + + + + + <alert alert-color="Warning" dismissible="false"> + This is a dismissible warning alert! + </alert> + + + + + Alert With Header + + + Look at me! I have a header! + + + diff --git a/src/AspNetCore.Utilities.Bootstrap5TagHelpers/AlertTagHelper.cs b/src/AspNetCore.Utilities.Bootstrap5TagHelpers/AlertTagHelper.cs index fcf955b..453d5db 100644 --- a/src/AspNetCore.Utilities.Bootstrap5TagHelpers/AlertTagHelper.cs +++ b/src/AspNetCore.Utilities.Bootstrap5TagHelpers/AlertTagHelper.cs @@ -26,6 +26,11 @@ public class AlertTagHelper : TagHelper /// public bool Dismissible { get; set; } = false; + /// + /// If supplied this will render as a heading inside the alert. + /// + public string? HeadingText { get; set; } + /// /// Processes the tag helper /// @@ -50,10 +55,21 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu output.AddClass("fade", HtmlEncoder.Default); output.AddClass("show", HtmlEncoder.Default); } + output.Attributes.Add("role", "alert"); + if (!string.IsNullOrEmpty(HeadingText)) + { + var headingBuilder = new TagBuilder("h4"); + headingBuilder.AddCssClass("alert-heading"); + headingBuilder.InnerHtml.Append(HeadingText); + output.PreContent.AppendHtml(headingBuilder); + } + if (!Dismissible) + { return; + } var buttonBuilder = new TagBuilder("button"); buttonBuilder.Attributes.Add("type", "button"); @@ -62,7 +78,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu buttonBuilder.Attributes.Add("aria-label", "Close"); //Get existing content - var existing = await output.GetChildContentAsync(); + TagHelperContent existing = await output.GetChildContentAsync(); output.Content.AppendHtml(existing.GetContent()); output.Content.AppendHtml(buttonBuilder); }