Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

NopTextAreaTagHelper.cs

using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Nop.Web.Framework.TagHelpers.Admin;

/// 
/// "nop-textarea" tag helper
/// 
[HtmlTargetElement("nop-textarea", Attributes = FOR_ATTRIBUTE_NAME)]
public partial class NopTextAreaTagHelper : TextAreaTagHelper
{
    #region Constants

    protected const string FOR_ATTRIBUTE_NAME = "asp-for";
    protected const string REQUIRED_ATTRIBUTE_NAME = "asp-required";
    protected const string CUSTOM_HTML_ATTRIBUTES = "html-attributes";

    #endregion

    #region Ctor

    public NopTextAreaTagHelper(IHtmlGenerator generator) : base(generator)
    {
    }

    #endregion

    #region Methods

    /// 
    /// Asynchronously executes the tag helper with the given context and output
    /// 
    /// Contains information associated with the current HTML tag
    /// A stateful HTML element used to generate an HTML tag
    /// A task that represents the asynchronous operation
    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        ArgumentNullException.ThrowIfNull(context);

        ArgumentNullException.ThrowIfNull(output);

        //tag details
        output.TagName = "textarea";
        output.TagMode = TagMode.StartTagAndEndTag;

        //merge classes
        var classValue = output.Attributes.ContainsName("class")
            ? $"{output.Attributes["class"].Value} form-control"
            : "form-control";
        output.Attributes.SetAttribute("class", classValue);

        //set custom html attributes
        var htmlAttributesDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(CustomHtmlAttributes);
        if (htmlAttributesDictionary?.Count > 0)
        {
            foreach (var (key, value) in htmlAttributesDictionary)
            {
                output.Attributes.Add(key, value);
            }
        }

        //additional parameters
        var rowsNumber = output.Attributes.ContainsName("rows") ? output.Attributes["rows"].Value : 4;
        output.Attributes.SetAttribute("rows", rowsNumber);
        var colsNumber = output.Attributes.ContainsName("cols") ? output.Attributes["cols"].Value : 20;
        output.Attributes.SetAttribute("cols", colsNumber);

        //required asterisk
        if (bool.TryParse(IsRequired, out var required) && required)
        {
            output.PreElement.SetHtmlContent("
"); output.PostElement.SetHtmlContent("
*
"); } await base.ProcessAsync(context, output); } #endregion #region Properties /// /// Custom html attributes /// [HtmlAttributeName(CUSTOM_HTML_ATTRIBUTES)] public object CustomHtmlAttributes { set; get; } /// /// Indicates whether the field is required /// [HtmlAttributeName(REQUIRED_ATTRIBUTE_NAME)] public string IsRequired { set; get; } #endregion }