Input & Select Tag Helpers label refactoring

pull/279/head
yekalkan 7 years ago
parent 4af2d25639
commit eef0064a17

@ -8,6 +8,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{
public ModelExpression AspFor { get; set; }
public string Label { get; set; }
[HtmlAttributeName("disabled")]
public bool IsDisabled { get; set; }

@ -50,7 +50,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{
var inputTag = GetInputTag(context, output, out isCheckbox);
var inputHtml = RenderTagHelperOutput(inputTag, _encoder);
var label = GetLabelAsHtml(context, output, isCheckbox);
var label = GetLabelAsHtml(context, output, inputTag, isCheckbox);
var validation = isCheckbox ? "" : GetValidationAsHtml(context, output);
@ -112,7 +112,21 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
return attributes.Any(a => a.Value != null && a.Name == "type" && a.Value.ToString() == "checkbox");
}
protected virtual string GetLabelAsHtml(TagHelperContext context, TagHelperOutput output, bool isCheckbox)
protected virtual string GetLabelAsHtml(TagHelperContext context, TagHelperOutput output, TagHelperOutput inputTag, bool isCheckbox)
{
if (string.IsNullOrEmpty(TagHelper.Label))
{
return GetLabelAsHtmlUsingTagHelper(context,output,isCheckbox);
}
var checkboxClass = isCheckbox ? "class=\"form-check-label\" " : "";
return "<label " + checkboxClass + GetIdAttributeAsString(inputTag) + ">"
+ TagHelper.Label +
"</label>";
}
protected virtual string GetLabelAsHtmlUsingTagHelper(TagHelperContext context, TagHelperOutput output, bool isCheckbox)
{
var labelTagHelper = new LabelTagHelper(_generator)
{

@ -9,6 +9,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{
public ModelExpression AspFor { get; set; }
public string Label { get; set; }
public IEnumerable<SelectListItem> AspItems { get; set; }
[HtmlAttributeNotBound]

@ -17,13 +17,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{
private readonly IHtmlGenerator _generator;
private readonly HtmlEncoder _encoder;
private readonly IStringLocalizer<AbpUiResource> _localizer;
public AbpSelectTagHelperService(IHtmlGenerator generator, HtmlEncoder encoder, IStringLocalizer<AbpUiResource> localizer)
public AbpSelectTagHelperService(IHtmlGenerator generator, HtmlEncoder encoder)
{
_generator = generator;
_encoder = encoder;
_localizer = localizer;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
@ -51,7 +49,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{
var selectTag = GetSelectTag(context, output);
var selectAsHtml = RenderTagHelperOutput(selectTag, _encoder);
var label = GetLabelAsHtml(context, output);
var label = GetLabelAsHtml(context, output, selectTag);
return label + Environment.NewLine + selectAsHtml;
}
@ -95,6 +93,16 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
return selectItems;
}
protected virtual string GetLabelAsHtml(TagHelperContext context, TagHelperOutput output, TagHelperOutput selectTag)
{
if (string.IsNullOrEmpty(TagHelper.Label))
{
return GetLabelAsHtmlUsingTagHelper(context, output);
}
return "<label " + GetIdAttributeAsString(selectTag) + ">" + TagHelper.Label + "</label>";
}
protected virtual bool GetSelectItemsIfProvidedByEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems)
{
selectItems = explorer.Metadata.IsEnum ? explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static)
@ -143,17 +151,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
return TagHelper.AspFor.ModelExplorer.Model?.ToString();
}
protected virtual string GetLabelAsHtml(TagHelperContext context, TagHelperOutput output)
protected virtual string GetLabelAsHtmlUsingTagHelper(TagHelperContext context, TagHelperOutput output)
{
var labelTagHelper = new LabelTagHelper(_generator)
{
For = TagHelper.AspFor,
ViewContext = TagHelper.ViewContext
};
var attributeList = new TagHelperAttributeList();
return RenderTagHelper(attributeList, context, labelTagHelper, _encoder, "span", TagMode.StartTagAndEndTag, true);
return RenderTagHelper(new TagHelperAttributeList(), context, labelTagHelper, _encoder, "span", TagMode.StartTagAndEndTag, true);
}
}
}
Loading…
Cancel
Save