private TagBuilder GenerateLabel(bool isSubItem = false) { return(_htmlGenerator.GenerateLabel( ViewContext, For.ModelExplorer, For.Name, labelText: LabelOverride, htmlAttributes: new { @class = isSubItem ? "subitem control-label" : "control-label" })); }
public override void Process(TagHelperContext context, TagHelperOutput output) { using (var writer = new StringWriter()) { writer.Write(@"<div class=""form-group"">"); var label = _generator.GenerateLabel( ViewContext, For.ModelExplorer, For.Name, null, new { @class = "control-label" }); label.WriteTo(writer, NullHtmlEncoder.Default); var textArea = _generator.GenerateTextBox(ViewContext, For.ModelExplorer, For.Name, For.Model, null, new { @class = "form-control" }); textArea.WriteTo(writer, NullHtmlEncoder.Default); var validationMsg = _generator.GenerateValidationMessage( ViewContext, For.ModelExplorer, For.Name, null, ViewContext.ValidationMessageElement, new { @class = "text-danger" }); validationMsg.WriteTo(writer, NullHtmlEncoder.Default); writer.Write(@"</div>"); output.Content.SetHtmlContent(writer.ToString()); } }
protected virtual IHtmlContent GenerateLabel( ModelExplorer modelExplorer, string expression, string labelText, object htmlAttributes) { if (modelExplorer == null) { throw new ArgumentNullException(nameof(modelExplorer)); } var tagBuilder = _htmlGenerator.GenerateLabel( ViewContext, modelExplorer, expression: expression, labelText: labelText, htmlAttributes: htmlAttributes); if (tagBuilder == null) { return(HtmlString.Empty); } return(tagBuilder); }
public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagMode = TagMode.SelfClosing; output.TagName = null; int i = 0; foreach (var item in Items) { i++; TagBuilder div = new TagBuilder("div"); div.MergeAttribute("class", "form-group"); output.PostContent.AppendHtml(div.RenderStartTag()); string radioId = For.Name + i; output.PostContent.AppendHtml( _generator.GenerateRadioButton( ViewContext, For.ModelExplorer, For.Name, item.Value, (int)For.Model == item.Value, new { Id = radioId })); output.PostContent.AppendHtml( _generator.GenerateLabel( ViewContext, For.ModelExplorer, radioId, item.Text, null)); output.PostContent.AppendHtml(div.RenderEndTag()); } }
public override void Process(TagHelperContext context, TagHelperOutput output) { using (var writer = new StringWriter()) { writer.Write(@"<div class=""form-group"">"); var label = _generator.GenerateLabel(ViewContext, For.ModelExplorer, For.Name, null, new { @class = "control-label" }); label.WriteTo(writer, NullHtmlEncoder.Default); var rows = Rows == 0 ? 8 : Rows; object htmlAttributes = null; if (!Disabled) { htmlAttributes = new { @class = "form-control" } } ; else { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } }; var textArea = _generator.GenerateTextArea(ViewContext, For.ModelExplorer, For.Name, rows, 100, htmlAttributes); textArea.WriteTo(writer, NullHtmlEncoder.Default); var validationMsg = _generator.GenerateValidationMessage(ViewContext, For.ModelExplorer, For.Name, null, ViewContext.ValidationMessageElement, new { @class = "text-danger" }); validationMsg.WriteTo(writer, NullHtmlEncoder.Default); writer.Write(@"</div>"); output.Content.SetHtmlContent(writer.ToString()); } } }
private void WriteLabel(TextWriter writer) { var tagBuilder = htmlGenerator.GenerateLabel( ViewContext, For.ModelExplorer, For.Name, labelText: LabelText, htmlAttributes: new { @class = "Flabel-text" }); tagBuilder.WriteTo(writer, htmlEncoder); }
private void WriteLabel(TextWriter writer, bool isSubItem = false) { var tagBuilder = _htmlGenerator.GenerateLabel( ViewContext, For.ModelExplorer, For.Name, labelText: LabelOverride, htmlAttributes: new { @class = isSubItem ? "subitem control-label" : "control-label" }); tagBuilder.WriteTo(writer, _htmlEncoder); }
public TagBuilder GenerateLabel(ViewContext viewContext, ModelExplorer modelExplorer, string expression, string labelText, object htmlAttributes) { var builder = _htmlGenerator.GenerateLabel(viewContext, modelExplorer, expression, labelText, htmlAttributes); if (modelExplorer.Metadata.IsRequired && _options.AddAstertix(viewContext)) { var newLabelText = Render(builder.InnerHtml) + " *"; builder.InnerHtml.SetContent(newLabelText); } return(builder); }
public override void Process(TagHelperContext context, TagHelperOutput output) { using (var writer = new StringWriter()) { writer.Write(@"<div class=""form-group"">"); var label = _generator.GenerateLabel( ViewContext, For.ModelExplorer, For.Name, null, new { @class = "control-label" }); label.WriteTo(writer, NullHtmlEncoder.Default); object htmlAttributes = null; if (Disabled) { htmlAttributes = new { @class = "form-control", @disabled = "disabled" }; } else { htmlAttributes = new { @class = "form-control" }; } var selectList = _generator.GenerateSelect( ViewContext, For.ModelExplorer, "---", For.Name, ItemList, false, htmlAttributes); selectList.WriteTo(writer, NullHtmlEncoder.Default); var validationMsg = _generator.GenerateValidationMessage( ViewContext, For.ModelExplorer, For.Name, null, ViewContext.ValidationMessageElement, new { @class = "text-danger" }); validationMsg.WriteTo(writer, NullHtmlEncoder.Default); writer.Write(@"</div>"); output.Content.SetHtmlContent(writer.ToString()); } }
private TagBuilder GetLabelBuilder() { var builder = _htmlGenerator.GenerateLabel( ViewContext, For.ModelExplorer, For.Name, null, null); builder.AddCssClass(TagHelperConstants.NhsLabel); builder.Attributes[TagHelperConstants.DataTestId] = LabelDataTestId ?? $"{For.Name}-label"; return(builder); }
/// <inheritdoc /> public override void Process(TagHelperContext context, TagHelperOutput output) { output.SuppressOutput(); foreach (var enumItem in For.Metadata.EnumNamesAndValues) { var id = VariantId(enumItem); var name = For.Metadata.EnumGroupedDisplayNamesAndValues.FirstOrDefault(v => v.Value == enumItem.Value).Key.Name; var radio = _generator.GenerateRadioButton(ViewContext, For.ModelExplorer, For.Name, enumItem.Key, false, new { id }); var label = _generator.GenerateLabel(ViewContext, For.ModelExplorer, For.Name, name, new { @for = id }); output.PreElement.AppendHtml(radio); output.PreElement.AppendHtml(label); } }
public TagBuilder GenerateLabel(ViewContext viewContext, ModelMetadata modelMetadata, ControlTagHelperOptions options, string name, string classes = null, object htmlAttributes = null) { var metadata = modelMetadata.AsMetadata(); var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); var builder = _htmlGenerator.GenerateLabel(viewContext, modelMetadata, name, metadata.Get <DisplayName>(), htmlAttributes); if (metadata.Get <Required>()) { builder.InnerHtml += "*"; } classes = GetClasses(options, classes, ControlTagItem.Label); builder.AddCssClass(classes); return(builder); }
protected virtual HtmlString GenerateLabel([NotNull] ModelMetadata metadata, string htmlFieldName, string labelText, object htmlAttributes) { var tagBuilder = _htmlGenerator.GenerateLabel( ViewContext, metadata, name: htmlFieldName, labelText: labelText, htmlAttributes: htmlAttributes); if (tagBuilder == null) { return(HtmlString.Empty); } return(tagBuilder.ToHtmlString(TagRenderMode.Normal)); }
/// <inheritdoc /> public override void Process(TagHelperContext context, TagHelperOutput output) { AssertModelIsEnum(); output.SuppressOutput(); foreach (var enumItem in For.Metadata.EnumNamesAndValues) { var id = VariantId(enumItem); var name = For.Metadata.EnumGroupedDisplayNamesAndValues.FirstOrDefault(v => v.Value == enumItem.Value).Key.Name; var radio = _generator.GenerateRadioButton(ViewContext, For.ModelExplorer, For.Metadata.PropertyName, enumItem.Key, false, new { id, @class = "custom-control-input" }); var label = _generator.GenerateLabel(ViewContext, For.ModelExplorer, For.Name, name, new { @for = id, @class = "custom-control-label" }); var container = new TagBuilder("div"); container.Attributes.Add("class", "custom-control custom-radio"); container.InnerHtml.AppendHtml(radio); container.InnerHtml.AppendHtml(label); output.Content.AppendHtml(container); } }
protected virtual HtmlString GenerateLabel( [NotNull] ModelExplorer modelExplorer, string expression, string labelText, object htmlAttributes) { var tagBuilder = _htmlGenerator.GenerateLabel( ViewContext, modelExplorer, expression: expression, labelText: labelText, htmlAttributes: htmlAttributes); if (tagBuilder == null) { return(HtmlString.Empty); } return(tagBuilder.ToHtmlString(TagRenderMode.Normal)); }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { output.TagName = string.Empty; output.TagMode = TagMode.SelfClosing; using (var sw = new StringWriter()) { var wrapper = new StringWriter(); var labelBuilder = new TagBuilder("label"); var inputBuilder = new TagBuilder("input"); var validationBuilder = new TagBuilder("validation"); var format = context.AllAttributes["asp-format"] != null ? context.AllAttributes["asp-format"].Value.ToString() : ""; var labelCss = context.AllAttributes["has-label"] != null ? context.AllAttributes["label-css"].Value.ToString() : ""; var inputCss = context.AllAttributes["input-css"] != null ? context.AllAttributes["input-css"].Value.ToString() : ""; var vmodel = context.AllAttributes["VModelName"] != null ? context.AllAttributes["VModelName"].Value.ToString() : ""; var validationCss = Expression.Metadata.IsRequired || (context.AllAttributes["is-required"] != null && bool.Parse(context.AllAttributes["is-required"].Value.ToString())) ? context.AllAttributes["required-css"].Value.ToString() : ""; if (Expression.Metadata.IsRequired) { inputCss += " requiredField"; } switch (Expression.Metadata.DataTypeName) { case "int": inputCss += " inputOnly"; break; case "double": inputCss += " doubleOnly"; break; case "decimal": inputCss += " doubleOnly"; break; default: break; } // generate label if exist if (context.AllAttributes["has-label"] != null && bool.Parse(context.AllAttributes["has-label"].Value.ToString())) { labelBuilder = _generator.GenerateLabel(ViewContext, Expression.ModelExplorer, Expression.Name, Expression.Metadata.GetDisplayName(), new { @class = LabelCss }); labelBuilder.WriteTo(sw, NullHtmlEncoder.Default); } // generate actual input inputBuilder = _generator.GenerateTextBox(ViewContext, Expression.ModelExplorer, Expression.Name, Expression.Model, Expression.Metadata.DisplayFormatString, new { @class = inputCss, PlaceHolder = Expression.Metadata.GetDisplayName() }); foreach (var validationAttribute in Expression.Metadata.ValidatorMetadata) { if (validationAttribute.GetType().Name == "RegularExpressionAttribute") { var reAttr = (RegularExpressionAttribute)validationAttribute; inputBuilder.Attributes.Add("pattern", reAttr.Pattern); } } foreach (var attribute in context.AllAttributes) { if (HtmlTagHelpers.IsAttributeAddible(attribute.Name)) { if (attribute.Name == "place-holder") { inputBuilder.Attributes.Remove("placeholder"); inputBuilder.Attributes.Add("placeholder", attribute.Value.ToString()); } else if (attribute.Name == "v-model-name") { inputBuilder.Attributes.Remove("v-model"); inputBuilder.Attributes.Add("v-model", attribute.Value.ToString()); } else { inputBuilder.Attributes.Add(attribute.Name, attribute.Value.ToString()); } } } inputBuilder.WriteTo(sw, NullHtmlEncoder.Default); // generate validation if exist if (Expression.Metadata.IsRequired || (context.AllAttributes["is-required"] != null && bool.Parse(context.AllAttributes["is-required"].Value.ToString()))) { validationBuilder = _generator.GenerateValidationMessage(ViewContext, Expression.ModelExplorer, Expression.Name, null, ViewContext.ValidationMessageElement, new { @class = validationCss }); validationBuilder.WriteTo(sw, NullHtmlEncoder.Default); } // generate wrapper parent element if exist if (context.AllAttributes["has-parent"] != null && bool.Parse(context.AllAttributes["has-parent"].Value.ToString())) { wrapper.Write("<div class='" + context.AllAttributes["parent-css"].Value + "'>"); wrapper.Write(sw.ToString()); wrapper.Write("</div>"); } output.Content.SetHtmlContent(wrapper.ToString()); //output.PreContent.SetHtmlContent("<div class=''>"); //output.PostContent.SetHtmlContent("</div>"); return(base.ProcessAsync(context, output)); } }
public override void Process(TagHelperContext context, TagHelperOutput output) { var text = output.Attributes.SingleOrDefault(a => a.Name == "text"); var isDisabled = output.Attributes.SingleOrDefault(a => a.Name == "disabled") != null; var isReadOnly = output.Attributes.SingleOrDefault(a => a.Name == "readonly") != null; var isRequired = output.Attributes.SingleOrDefault(a => a.Name == "required") != null; output.Attributes.RemoveAll("disabled"); output.Attributes.RemoveAll("readonly"); output.Attributes.RemoveAll("required"); var labelAttributes = new Dictionary <string, object> { { "class", "pmd-checkbox checkbox-pmd-ripple-effect" } }; var checkboxAttributes = new Dictionary <string, object> { { "type", "checkbox" } }; if (isDisabled) { checkboxAttributes.Add("disabled", "disabled"); } if (isReadOnly) { checkboxAttributes.Add("readonly", "readonly"); } if (isRequired) { checkboxAttributes.Add("required", "required"); } var inputPre = ""; var inputPost = ""; var value = For.ModelExplorer.Model == null ? false : (bool)For.ModelExplorer.Model; var labelText = text == null ? For.Metadata.DisplayName : text.Value.ToString(); var input = _generator.GenerateCheckBox(ViewContext, For.ModelExplorer, For.Name, value, checkboxAttributes); var label = _generator.GenerateLabel(ViewContext, For.ModelExplorer, For.Name, "", labelAttributes); var hidden = _generator.GenerateHiddenForCheckbox(ViewContext, For.ModelExplorer, For.Name); // Strip end tag from label var labelStart = ""; using (var writer = new StringWriter()) { label.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); labelStart = writer.ToString(); labelStart = labelStart.Replace("</label>", ""); } inputPre = $"{labelStart}{inputPre}"; inputPost = $"{inputPost}<span class=\"pmd-checkbox-label\"> </span><span class=\"pmd-checkbox\">{labelText}</span></label>"; output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; output.Attributes.SetAttribute("class", "checkbox pmd-default-theme"); string textboxOutput; using (var writer = new StringWriter()) { writer.Write(inputPre); input.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); hidden.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); writer.Write(inputPost); textboxOutput = writer.ToString(); } output.Content.SetHtmlContent(textboxOutput); }
public override void Process(TagHelperContext context, TagHelperOutput output) { var placeholder = output.HasAttribute("placeholder") ? output.GetAttribute("placeholder") : ""; output.AddClass("form-group pmd-textfield"); var labelAttributes = new Dictionary <string, object>(); var selectAttributes = new Dictionary <string, object> { { "class", "select-simple form-control pmd-select2" } }; if (IsLg) { output.AddClass("form-group-lg"); } if (IsSm) { output.AddClass("form-group-sm"); } if (IsFloating) { output.AddClass("pmd-textfield-floating-label"); } if (HasWarning) { output.AddClass("has-warning"); } if (HasSuccess) { output.AddClass("has-success"); } if (HasError) { output.AddClass("has-error"); } if (Disabled) { selectAttributes.Add("disabled", "disabled"); } if (ReadOnly) { selectAttributes.Add("readonly", "readonly"); } if (Required) { selectAttributes.Add("required", "required"); } var inputPre = ""; var inputPost = ""; var labelClass = "control-label"; if (Icon != null) { inputPre = $"{inputPre}<div class=\"input-group\"><div class=\"input-group-addon\"><i class=\"material-icons md-dark pmd-sm\">{Icon}</i></div>"; inputPost = $"{inputPost}</div>"; labelClass = $"{labelClass} pmd-input-group-label"; } labelAttributes.Add("class", labelClass); var metadata = For.Metadata; var modelExplorer = For.ModelExplorer; var modelType = For.ModelExplorer.ModelType; if (Nullable.GetUnderlyingType(For.ModelExplorer.ModelType) != null) { modelType = Nullable.GetUnderlyingType(For.ModelExplorer.ModelType); } if (modelType.IsEnum) { var selectListItems = new List <SelectListItem>(); var enumNames = Enum.GetNames(modelType); var enumValues = Enum.GetValues(modelType); var attributes = modelType.GetCustomAttribute <DisplayAttribute>(); int i = 0; foreach (var name in enumNames) { var display = modelType.GetMember(name).First().GetCustomAttribute <DisplayAttribute>(); selectListItems.Add(new SelectListItem() { Text = display == null ? name : display.Name, Value = enumValues.GetValue(i).ToString() }); i++; } Items = selectListItems; } var select = _generator.GenerateSelect(ViewContext, modelExplorer, placeholder, For.Name, Items, Multiple, selectAttributes); var label = _generator.GenerateLabel(ViewContext, modelExplorer, For.Name, metadata.DisplayName, labelAttributes); var hidden = _generator.GenerateHidden(ViewContext, modelExplorer, For.Name, null, true, null); output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; string selectOutput; using (var writer = new StringWriter()) { label.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); writer.Write(inputPre); select.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); writer.Write(inputPost); hidden.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); selectOutput = writer.ToString(); } output.Content.SetHtmlContent(selectOutput); }
public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; output.AddClass("govuk-date-input__item", HtmlEncoder.Default); output.PreContent.SetHtmlContent($@"<div class=""govuk-form-group"" id=""{DateBoxId}"">"); using (var writer = new StringWriter()) { if (!HideLabel) { var labelBuilder = _htmlGenerator.GenerateLabel( ViewContext, For.ModelExplorer, For.Name, null, new { @class = "govuk-label govuk-date-input__label" }); labelBuilder.WriteTo(writer, _htmlEncoder); } var textboxBuilder = _htmlGenerator.GenerateTextBox( ViewContext, For.ModelExplorer, For.Name, For.Model, null, new { @class = "govuk-input govuk-date-input__input", type = "number" }); if (!string.IsNullOrEmpty(this.AutoComplete)) { textboxBuilder.MergeAttribute("autocomplete", AutoComplete); } if (!string.IsNullOrEmpty(this.WidthCssClass)) { textboxBuilder.AddCssClass(WidthCssClass); } if (!string.IsNullOrEmpty(this.MaxLength)) { textboxBuilder.MergeAttribute("max-length", MaxLength); } if (!string.IsNullOrEmpty(For.Name)) { textboxBuilder.MergeAttribute("aria-describedby", For.Name); } if (!string.IsNullOrEmpty(DateBoxId)) { textboxBuilder.MergeAttribute("aria-labelledby", DateBoxId); } textboxBuilder.WriteTo(writer, _htmlEncoder); output.Content.SetHtmlContent(writer.ToString()); output.PostContent.SetHtmlContent(@"</div>"); } }
public override void Process(TagHelperContext context, TagHelperOutput output) { var isLg = output.Attributes.SingleOrDefault(a => a.Name == "lg") != null; var isSm = output.Attributes.SingleOrDefault(a => a.Name == "sm") != null; var isFloating = output.Attributes.SingleOrDefault(a => a.Name == "float") != null; var isDisabled = output.Attributes.SingleOrDefault(a => a.Name == "disabled") != null; var isReadOnly = output.Attributes.SingleOrDefault(a => a.Name == "readonly") != null; var isRequired = output.Attributes.SingleOrDefault(a => a.Name == "required") != null; var hasWarning = output.Attributes.SingleOrDefault(a => a.Name == "has-warning") != null; var hasSuccess = output.Attributes.SingleOrDefault(a => a.Name == "has-success") != null; var hasError = output.Attributes.SingleOrDefault(a => a.Name == "has-error") != null; var icon = output.Attributes.SingleOrDefault(a => a.Name == "icon"); var binding = output.Attributes.SingleOrDefault(a => a.Name == "for"); output.Attributes.RemoveAll("lg"); output.Attributes.RemoveAll("sm"); output.Attributes.RemoveAll("float"); output.Attributes.RemoveAll("disabled"); output.Attributes.RemoveAll("readonly"); output.Attributes.RemoveAll("required"); output.Attributes.RemoveAll("has-warning"); output.Attributes.RemoveAll("has-success"); output.Attributes.RemoveAll("has-error"); output.Attributes.RemoveAll("icon"); output.Attributes.RemoveAll("for"); var formGroupClass = "form-group pmd-textfield"; var labelAttributes = new Dictionary <string, object>(); var textboxAttributes = new Dictionary <string, object> { { "type", "text" }, { "class", "form-control" } }; if (isLg) { formGroupClass = $"{formGroupClass} form-group-lg"; } if (isSm) { formGroupClass = $"{formGroupClass} form-group-sm"; } if (isFloating) { formGroupClass = $"{formGroupClass} pmd-textfield-floating-label"; } if (hasWarning) { formGroupClass = $"{formGroupClass} has-warning"; } if (hasSuccess) { formGroupClass = $"{formGroupClass} has-sucess"; } if (hasError) { formGroupClass = $"{formGroupClass} has-error"; } if (isDisabled) { textboxAttributes.Add("disabled", "disabled"); } if (isReadOnly) { textboxAttributes.Add("readonly", "readonly"); } if (isRequired) { textboxAttributes.Add("required", "required"); } var inputPre = ""; var inputPost = ""; var labelClass = "control-label"; if (icon != null) { inputPre = $"{inputPre}<div class=\"input-group\"><div class=\"input-group-addon\"><i class=\"material-icons md-dark pmd-sm\">{icon.Value}</i></div>"; inputPost = $"{inputPost}</div>"; labelClass = $"{labelClass} pmd-input-group-label"; } labelAttributes.Add("class", labelClass); var metadata = For.Metadata; var modelExplorer = For.ModelExplorer; var input = _generator.GenerateTextBox(ViewContext, modelExplorer, For.Name, modelExplorer.Model, null, textboxAttributes); var label = _generator.GenerateLabel(ViewContext, modelExplorer, For.Name, metadata.DisplayName, labelAttributes); output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; output.Attributes.SetAttribute("class", formGroupClass); string textboxOutput; using (var writer = new StringWriter()) { label.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); writer.Write(inputPre); input.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); writer.Write(inputPost); textboxOutput = writer.ToString(); } output.Content.SetHtmlContent(textboxOutput); }
public virtual TagBuilder GenerateLabel(ModelExpression f) => generator.GenerateLabel(viewContext, f.ModelExplorer, f.Name, null, new { @class = styles.Label });
public TagBuilder GenerateLabel(ViewContext viewContext, ModelExplorer modelExplorer, string expression, string labelText, object htmlAttributes) { return(_htmlGenerator.GenerateLabel(viewContext, modelExplorer, expression, labelText, htmlAttributes)); }
public override void Process(TagHelperContext context, TagHelperOutput output) { var isLg = output.Attributes.SingleOrDefault(a => a.Name == "lg") != null; var isSm = output.Attributes.SingleOrDefault(a => a.Name == "sm") != null; var isFloating = output.Attributes.SingleOrDefault(a => a.Name == "float") != null; var isDisabled = output.Attributes.SingleOrDefault(a => a.Name == "disabled") != null; var isReadOnly = output.Attributes.SingleOrDefault(a => a.Name == "readonly") != null; var isRequired = output.Attributes.SingleOrDefault(a => a.Name == "required") != null; var hasWarning = output.Attributes.SingleOrDefault(a => a.Name == "has-warning") != null; var hasSuccess = output.Attributes.SingleOrDefault(a => a.Name == "has-success") != null; var hasError = output.Attributes.SingleOrDefault(a => a.Name == "has-error") != null; var icon = output.Attributes.SingleOrDefault(a => a.Name == "icon"); var placeholder = output.Attributes.SingleOrDefault(a => a.Name == "placeholder"); var binding = output.Attributes.SingleOrDefault(a => a.Name == "for"); output.Attributes.RemoveAll("lg"); output.Attributes.RemoveAll("sm"); output.Attributes.RemoveAll("float"); output.Attributes.RemoveAll("disabled"); output.Attributes.RemoveAll("readonly"); output.Attributes.RemoveAll("required"); output.Attributes.RemoveAll("has-warning"); output.Attributes.RemoveAll("has-success"); output.Attributes.RemoveAll("has-error"); output.Attributes.RemoveAll("icon"); output.Attributes.RemoveAll("placeholder"); output.Attributes.RemoveAll("for"); var formGroupClass = "form-group pmd-textfield"; var labelAttributes = new Dictionary <string, object>(); var selectAttributes = new Dictionary <string, object> { { "class", "form-control pmd-select2-tags tags" } }; if (isLg) { formGroupClass = $"{formGroupClass} form-group-lg"; } if (isSm) { formGroupClass = $"{formGroupClass} form-group-sm"; } if (isFloating) { formGroupClass = $"{formGroupClass} pmd-textfield-floating-label"; } if (hasWarning) { formGroupClass = $"{formGroupClass} has-warning"; } if (hasSuccess) { formGroupClass = $"{formGroupClass} has-sucess"; } if (hasError) { formGroupClass = $"{formGroupClass} has-error"; } if (isDisabled) { selectAttributes.Add("disabled", "disabled"); } if (isReadOnly) { selectAttributes.Add("readonly", "readonly"); } if (isRequired) { selectAttributes.Add("required", "required"); } var inputPre = ""; var inputPost = ""; var labelClass = "control-label"; string placeholderString = placeholder != null?placeholder.Value.ToString() : ""; if (icon != null) { inputPre = $"{inputPre}<div class=\"input-group\"><div class=\"input-group-addon\"><i class=\"material-icons md-dark pmd-sm\">{icon.Value}</i></div>"; inputPost = $"{inputPost}</div>"; labelClass = $"{labelClass} pmd-input-group-label"; } labelAttributes.Add("class", labelClass); var metadata = For.Metadata; var modelExplorer = For.ModelExplorer; var items = new List <SelectListItem>(); if (modelExplorer.Model != null) { foreach (var item in (string[])modelExplorer.Model) { items.Add(new SelectListItem() { Text = item, Value = item }); } } var select = _generator.GenerateSelect(ViewContext, modelExplorer, placeholderString, For.Name, items, true, selectAttributes); var label = _generator.GenerateLabel(ViewContext, modelExplorer, For.Name, metadata.DisplayName, labelAttributes); var hidden = _generator.GenerateHidden(ViewContext, modelExplorer, For.Name, null, true, null); output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; output.Attributes.SetAttribute("class", formGroupClass); string selectOutput; using (var writer = new StringWriter()) { label.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); writer.Write(inputPre); select.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); writer.Write(inputPost); //hidden.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); selectOutput = writer.ToString(); } output.Content.SetHtmlContent(selectOutput); }
private TagBuilder GenerateLabel() => _generator.GenerateLabel(ViewContext, For.ModelExplorer, For.Name, For.Metadata.DisplayName ?? For.Metadata.Name.BreakUpCamelCase(), new { @class = "control-label" });
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { output.TagName = string.Empty; output.TagMode = TagMode.SelfClosing; using (var sw = new StringWriter()) { var wrapper = new StringWriter(); var labelBuilder = new TagBuilder("label"); var selectBuilder = new TagBuilder("select"); var validationBuilder = new TagBuilder("validation"); var format = context.AllAttributes["asp-format"] != null ? context.AllAttributes["asp-format"].Value.ToString() : ""; var labelCss = context.AllAttributes["has-label"] != null ? context.AllAttributes["label-css"].Value.ToString() : ""; var inputCss = context.AllAttributes["input-css"] != null ? context.AllAttributes["input-css"].Value.ToString() : ""; var validationCss = Expression.Metadata.IsRequired || (context.AllAttributes["is-required"] != null && bool.Parse(context.AllAttributes["is-required"].Value.ToString())) ? context.AllAttributes["required-css"].Value.ToString() : ""; if (Expression.Metadata.IsRequired) { inputCss += " requiredField"; } // generate label if exist if (context.AllAttributes["has-label"] != null && bool.Parse(context.AllAttributes["has-label"].Value.ToString())) { labelBuilder = _generator.GenerateLabel(ViewContext, Expression.ModelExplorer, Expression.Name, Expression.Metadata.GetDisplayName(), new { @class = LabelCss }); labelBuilder.WriteTo(sw, NullHtmlEncoder.Default); } // generate actual input selectBuilder = _generator.GenerateSelect(ViewContext, Expression.ModelExplorer, "Please select", Expression.Name, SelectList, false, new { @class = inputCss, PlaceHolder = Expression.Metadata.GetDisplayName() }); foreach (var attribute in context.AllAttributes) { if (HtmlTagHelpers.IsAttributeAddible(attribute.Name)) { if (attribute.Name == "v-model-name") { selectBuilder.Attributes.Remove("v-model"); selectBuilder.Attributes.Add("v-model", attribute.Value.ToString()); } else if (attribute.Name == "v-get-text") { selectBuilder.Attributes.Remove("data-gettext"); selectBuilder.Attributes.Add("data-gettext", attribute.Value.ToString()); } else { selectBuilder.Attributes.Add(attribute.Name, attribute.Value.ToString()); } } } selectBuilder.WriteTo(sw, NullHtmlEncoder.Default); // generate validation if exist if (Expression.Metadata.IsRequired || (context.AllAttributes["is-required"] != null && bool.Parse(context.AllAttributes["is-required"].Value.ToString()))) { validationBuilder = _generator.GenerateValidationMessage(ViewContext, Expression.ModelExplorer, Expression.Name, null, ViewContext.ValidationMessageElement, new { @class = validationCss }); validationBuilder.WriteTo(sw, NullHtmlEncoder.Default); } // generate wrapper parent element if exist if (context.AllAttributes["has-parent"] != null && bool.Parse(context.AllAttributes["has-parent"].Value.ToString())) { wrapper.Write("<div class='" + context.AllAttributes["parent-css"].Value + "'>"); wrapper.Write(sw.ToString()); wrapper.Write("</div>"); } output.Content.SetHtmlContent(wrapper.ToString()); //output.PreContent.SetHtmlContent("<div class=''>"); //output.PostContent.SetHtmlContent("</div>"); return(base.ProcessAsync(context, output)); } }