Added Info text to abp-input tag helper

pull/670/head
Yunus Emre Kalkan 7 years ago
parent 07c2d5ff09
commit 2aa2448b44

@ -60,10 +60,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
var inputTag = GetInputTagHelperOutput(context, output, out isCheckbox);
var inputHtml = RenderTagHelperOutput(inputTag, _encoder);
var label = GetLabelAsHtml(context, output, inputTag, isCheckbox);
var info = GetInfoAsHtml(context, output, inputTag, isCheckbox);
var validation = isCheckbox ? "" : GetValidationAsHtml(context, output, inputTag);
return GetContent(context, output, label, inputHtml, validation, isCheckbox);
return GetContent(context, output, label, inputHtml, validation, info, isCheckbox);
}
protected virtual string GetValidationAsHtml(TagHelperContext context, TagHelperOutput output, TagHelperOutput inputTag)
@ -84,14 +85,14 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
return RenderTagHelper(attributeList, context, validationMessageTagHelper, _encoder, "span", TagMode.StartTagAndEndTag, true);
}
protected virtual string GetContent(TagHelperContext context, TagHelperOutput output, string label, string inputHtml, string validation, bool isCheckbox)
protected virtual string GetContent(TagHelperContext context, TagHelperOutput output, string label, string inputHtml, string validation, string infoHtml, bool isCheckbox)
{
var innerContent = isCheckbox ?
inputHtml + Environment.NewLine + label :
label + Environment.NewLine + inputHtml;
return Environment.NewLine + innerContent + Environment.NewLine +
Environment.NewLine + validation + Environment.NewLine;
Environment.NewLine + validation + Environment.NewLine + infoHtml;
}
protected virtual string SurroundInnerHtmlAndGet(TagHelperContext context, TagHelperOutput output, string innerHtml, bool isCheckbox)
@ -134,6 +135,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
AddFormControlClass(context, output, isCheckbox, inputTagHelperOutput);
AddReadOnlyAttribute(inputTagHelperOutput);
AddPlaceholderAttribute(inputTagHelperOutput);
AddInfoTextId(inputTagHelperOutput);
return inputTagHelperOutput;
}
@ -196,6 +198,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
}
}
protected virtual void AddInfoTextId(TagHelperOutput inputTagHelperOutput)
{
if (GetAttribute<InputInfoText>(TagHelper.AspFor.ModelExplorer) == null)
{
return;
}
var idAttr = inputTagHelperOutput.Attributes.FirstOrDefault(a => a.Name == "id");
if (idAttr == null)
{
return;
}
inputTagHelperOutput.Attributes.Add("aria-describedby", LocalizeText(idAttr.Value + "InfoText"));
}
protected virtual string LocalizeText(string text)
{
IStringLocalizer localizer = null;
@ -233,6 +252,33 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
"</label>";
}
protected virtual string GetInfoAsHtml(TagHelperContext context, TagHelperOutput output, TagHelperOutput inputTag, bool isCheckbox)
{
if (isCheckbox)
{
return "";
}
var infoAttribute = GetAttribute<InputInfoText>(TagHelper.AspFor.ModelExplorer);
if (infoAttribute == null)
{
return "";
}
var idAttr = inputTag.Attributes.FirstOrDefault(a => a.Name == "id");
if (idAttr == null)
{
return "";
}
var id = idAttr.Value + "InfoText";
return "<small id=\""+ id + "\" class=\"form-text text-muted\">" +
LocalizeText(infoAttribute.Text) +
"</small>";
}
protected virtual string GetLabelAsHtmlUsingTagHelper(TagHelperContext context, TagHelperOutput output, bool isCheckbox)
{
var labelTagHelper = new LabelTagHelper(_generator)

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{
[AttributeUsage(AttributeTargets.Property)]
public class InputInfoText : Attribute
{
public string Text { get; set; }
public InputInfoText(string text)
{
Text = text;
}
}
}

@ -74,6 +74,7 @@ public class DynamicFormsModel : PageModel
[TextArea(Rows = 4)]
[Display(Name = "Description")]
[InputInfoText("Describe Yourself")]
public string Description { get; set; }
[Required]

@ -108,6 +108,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo.Pages.Components
[TextArea(Rows = 4)]
[Display(Name = "Description")]
[InputInfoText("Describe Yourself")]
public string Description { get; set; }
[Required]

Loading…
Cancel
Save