Skip to content
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
48 changes: 48 additions & 0 deletions NHSUKViewComponents.Web/ViewComponents/RadioListViewComponent.cs
Original file line number Diff line number Diff line change
@@ -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<RadiosItemViewModel> 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);
}
}
}
21 changes: 21 additions & 0 deletions NHSUKViewComponents.Web/ViewModels/RadiosItemViewModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
31 changes: 31 additions & 0 deletions NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace NHSUKViewComponents.Web.ViewModels
{
using System.Collections.Generic;

public class RadiosViewModel
{
public RadiosViewModel(
string aspFor,
string label,
string hintText,
IEnumerable<RadiosItemViewModel> 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<RadiosItemViewModel> Radios { get; set; }
public bool Required { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@using NHSUKViewComponents.Web.Extensions
@using NHSUKViewComponents.Web.ViewModels

@model RadiosViewModel

<div class="nhsuk-form-group">

<fieldset class="nhsuk-fieldset" aria-describedby="@Model.Label.RemoveWhitespace()-hint">
<legend class="nhsuk-fieldset__legend nhsuk-fieldset__legend--m">
<label class="nhsuk-fieldset__heading">
@Model.Label
</label>
</legend>

@if (Model.HintText != null)
{
<div class="nhsuk-hint" id="@Model.Label.RemoveWhitespace()-hint">
@Html.Raw(Model.HintText)
</div>
}

<div class="nhsuk-radios" aria-required="@(Model.Required ? "true" : "false" )">
@foreach (var (radio, index) in Model.Radios.Select((r, i) => (r, i)))
{
var radioId = $"{radio.Value}-{index}";
<div class="nhsuk-radios__item">
<input class="nhsuk-radios__input"
id="@radioId"
name="@Model.AspFor"
type="radio"
value="@radio.Value"
aria-describedby="@radio.Value-item-hint"
@(radio.Selected ? "checked" : string.Empty) />
<label class="nhsuk-label nhsuk-radios__label" for="@radioId">
@radio.Label
</label>
@if (radio.HintText != null)
{
<div class="nhsuk-hint nhsuk-radios__hint" id="@radio.Value-item-hint">
@radio.HintText
</div>
}
</div>
}

</div>
</fieldset>

</div>