From 055fd4d2364d07c18f3f6ce01c2d249bf32f6716 Mon Sep 17 00:00:00 2001 From: OluwatobiAwe Date: Tue, 18 Apr 2023 12:38:50 +0100 Subject: [PATCH] TD-1382 : Added Radio button not dependent on custom DLS class --- .../ViewComponents/RadioListViewComponent.cs | 48 ++++++++++++++++++ .../ViewModels/RadiosItemViewModel.cs | 21 ++++++++ .../ViewModels/RadiosViewModel.cs | 31 ++++++++++++ .../Components/RadioList/Default.cshtml | 49 +++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 NHSUKViewComponents.Web/ViewComponents/RadioListViewComponent.cs create mode 100644 NHSUKViewComponents.Web/ViewModels/RadiosItemViewModel.cs create mode 100644 NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs create mode 100644 NHSUKViewComponents.Web/Views/Shared/Components/RadioList/Default.cshtml diff --git a/NHSUKViewComponents.Web/ViewComponents/RadioListViewComponent.cs b/NHSUKViewComponents.Web/ViewComponents/RadioListViewComponent.cs new file mode 100644 index 0000000..8e079da --- /dev/null +++ b/NHSUKViewComponents.Web/ViewComponents/RadioListViewComponent.cs @@ -0,0 +1,48 @@ +namespace NHSUKViewComponents.Web.ViewComponents +{ + using System.Collections.Generic; + using System.Linq; + + using Microsoft.AspNetCore.Mvc; + using NHSUKViewComponents.Web.ViewModels; + + public class RadioListViewComponent : ViewComponent + { + public IViewComponentResult Invoke( + string aspFor, + string label, + IEnumerable radios, + bool populateWithCurrentValues, + string hintText, + bool required + ) + { + var radiosList = radios.Select( + r => new RadiosItemViewModel( + r.Value, + r.Label, + IsSelectedRadio(aspFor, r, populateWithCurrentValues), + r.HintText + ) + ); + + var viewModel = new RadiosViewModel( + aspFor, + label, + string.IsNullOrEmpty(hintText) ? null : hintText, + radiosList, + required + ); + + return View(viewModel); + } + + private bool IsSelectedRadio(string aspFor, RadiosItemViewModel radioItem, bool populateWithCurrentValue) + { + var model = ViewData.Model; + var property = model.GetType().GetProperty(aspFor); + var value = property?.GetValue(model); + return populateWithCurrentValue && value.Equals(radioItem.Value); + } + } +} diff --git a/NHSUKViewComponents.Web/ViewModels/RadiosItemViewModel.cs b/NHSUKViewComponents.Web/ViewModels/RadiosItemViewModel.cs new file mode 100644 index 0000000..23dc6ea --- /dev/null +++ b/NHSUKViewComponents.Web/ViewModels/RadiosItemViewModel.cs @@ -0,0 +1,21 @@ +namespace NHSUKViewComponents.Web.ViewModels +{ + public class RadiosItemViewModel + { + public RadiosItemViewModel(string value, string label, bool selected, string hintText) + { + Value = value; + Label = label; + Selected = selected; + HintText = hintText; + } + + public string Value { get; set; } + + public string Label { get; set; } + + public bool Selected { get; set; } + + public string HintText { get; set; } + } +} diff --git a/NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs b/NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs new file mode 100644 index 0000000..ffc6e20 --- /dev/null +++ b/NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs @@ -0,0 +1,31 @@ +namespace NHSUKViewComponents.Web.ViewModels +{ + using System.Collections.Generic; + + public class RadiosViewModel + { + public RadiosViewModel( + string aspFor, + string label, + string hintText, + IEnumerable radios, + bool required + ) + { + AspFor = aspFor; + Label = !required && !label.EndsWith("(optional)") ? label + " (optional)" : label; + HintText = hintText; + Radios = radios; + Required = required; + } + + public string AspFor { get; set; } + + public string Label { get; set; } + + public string HintText { get; set; } + + public IEnumerable Radios { get; set; } + public bool Required { get; set; } + } +} diff --git a/NHSUKViewComponents.Web/Views/Shared/Components/RadioList/Default.cshtml b/NHSUKViewComponents.Web/Views/Shared/Components/RadioList/Default.cshtml new file mode 100644 index 0000000..5ede878 --- /dev/null +++ b/NHSUKViewComponents.Web/Views/Shared/Components/RadioList/Default.cshtml @@ -0,0 +1,49 @@ +@using NHSUKViewComponents.Web.Extensions +@using NHSUKViewComponents.Web.ViewModels + +@model RadiosViewModel + +
+ +
+ + + + + @if (Model.HintText != null) + { +
+ @Html.Raw(Model.HintText) +
+ } + +
+ @foreach (var (radio, index) in Model.Radios.Select((r, i) => (r, i))) + { + var radioId = $"{radio.Value}-{index}"; +
+ + + @if (radio.HintText != null) + { +
+ @radio.HintText +
+ } +
+ } + +
+
+ +