Skip to content
This repository was archived by the owner on Sep 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions DotnetViewComponents/ViewComponents/ButtonViewComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace DotnetViewComponents.ViewComponents
{
using Microsoft.AspNetCore.Mvc;
using DotnetViewComponents.ViewModels;

/// <summary>
/// Represents a view component for rendering a button.
/// </summary>
public class ButtonViewComponent : ViewComponent
{
/// <summary>
/// Invokes the button view component with the specified parameters.
/// </summary>
/// <param name="text">The text to be displayed on the button.</param>
/// <param name="aspController">The asp-controller to dynamically set the controller path of the link.</param>
/// <param name="aspAction">The asp-action to dynamically set the action path of the link..</param>
/// <param name="aspRouteData">The asp-all-route-data to dynamically set the extra parameters to the path of the link.</param>
/// <param name="url">The url for the link.</param>
/// <param name="style">The style attributes for the button.</param><param name="style">The style attributes for the button.</param>
/// <param name="styling">The styling class for the button. Receives predefined styles from <see cref="ButtonStyle"/>. Defaults to <see cref="ButtonStyle.PRIMARY"/> if no input is passed to it.</param>
/// <param name="preventDoubleClick">Indicates whether to prevent double-clicking the button. Defaults to <c>false</c>.</param>
/// <returns>An <see cref="IViewComponentResult"/> that renders the button view.</returns>
public IViewComponentResult Invoke(
string text,
string style,
string styling = ButtonStyle.PRIMARY,
string aspController = null,
string aspAction = null,
Dictionary<string, string> aspRouteData = null,
bool preventDoubleClick = false)
{
var model = new ButtonViewModel(text, aspController, aspAction, aspRouteData, styling, style, preventDoubleClick);

return View(model);
}
}
}
78 changes: 78 additions & 0 deletions DotnetViewComponents/ViewModels/ButtonViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace DotnetViewComponents.ViewModels
{
public class ButtonViewModel
{
/// <summary>
/// Initializes a new instance of the <see cref="ButtonViewModel"/> class.
/// </summary>
/// <param name="text">The text to be displayed on the button.</param>
/// <param name="aspController">The asp-controller to dynamically set the controller path of the link.</param>
/// <param name="aspAction">The asp-action to dynamically set the action path of the link..</param>
/// <param name="aspRouteData">The asp-all-route-data to dynamically set the extra parameters to the path of the link.</param>
/// <param name="url">The url for the link.</param>
/// <param name="style">The style attributes for the button.</param>
/// <param name="styling">The styling class for the button. Defaults to <see cref="ButtonStyle.PRIMARY"/>.</param>
/// <param name="preventDoubleClick">Indicates whether to prevent double-clicking the button. Defaults to <c>false</c>.</param>
public ButtonViewModel(
string text,
string aspController,
string aspAction,
Dictionary<string, string> aspRouteData,
string styling,
string style,
bool preventDoubleClick)
{
Text = text;
AspController = aspController;
AspAction = aspAction;
AspRouteData = aspRouteData;
Styling = styling;
Style = style;
PreventDoubleClick = preventDoubleClick;
}

/// <summary>
/// Gets or sets the text displayed on the button.
/// </summary>
public string Text { get; set; }

/// <summary>
/// Gets or sets the asp-controller of the link.
/// </summary>
public string AspController { get; set; }

/// <summary>
/// Gets or sets the as-action of the link.
/// </summary>
public string AspAction { get; set; }

/// <summary>
/// Gets or sets the asp-all-route-data of the link.
/// </summary>
public Dictionary<string, string> AspRouteData { get; set; }

/// <summary>
/// Gets or sets the styling for the button.
/// </summary>
public string Styling { get; set; }

/// <summary>
/// Gets or sets the style attributes on the button.
/// </summary>
public string Style { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to prevent double-clicking the button.
/// </summary>
public bool PreventDoubleClick { get; set; }

}

public static class ButtonStyle
{
public const string PRIMARY = "nhsuk-button";
public const string SECONDARY = "nhsuk-button nhsuk-button--secondary";
public const string REVERSE = "nhsuk-button nhsuk-button--reverse";
public const string WARNING = "nhsuk-button nhsuk-button--warning";
}
}
19 changes: 19 additions & 0 deletions DotnetViewComponents/Views/Shared/Components/Button/Default.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@using DotnetViewComponents.ViewModels
@model ButtonViewModel

@{
var isLink = !String.IsNullOrWhiteSpace(Model.AspController);
var tagType = isLink ? "a" : "button";
var canSubmit = isLink ? "role=\"button\"" : "type =\"submit\"";
var undecorate = isLink ? "text-decoration: none;" : "";
var url = Url.Action(Model.AspAction, Model.AspController, Model.AspRouteData);
var href = isLink ? $"href=\"{url}\"" : "";
var preventDoubleClick = @Model.PreventDoubleClick ? "data-prevent-double-click=true" : "";

var buttonOpening = $"<{tagType} class=\"{Model.Styling}\" data-module=\"nhsuk-button\" {href} {preventDoubleClick} {canSubmit} style=\"{undecorate}{Model.Style}\">";
var buttonClosing = $"</{tagType}>";
}

@Html.Raw(buttonOpening)
@Model.Text
@Html.Raw(buttonClosing)