Skip to content

Commit da14ef7

Browse files
authored
Merge pull request #10 from TechnologyEnhancedLearning/Fixes/TD-1382-added-radiolist-viewcomponent
TD-1382 : Added Radio control not dependent on custom enumeration class
2 parents 4d57a00 + 055fd4d commit da14ef7

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace NHSUKViewComponents.Web.ViewComponents
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
using Microsoft.AspNetCore.Mvc;
7+
using NHSUKViewComponents.Web.ViewModels;
8+
9+
public class RadioListViewComponent : ViewComponent
10+
{
11+
public IViewComponentResult Invoke(
12+
string aspFor,
13+
string label,
14+
IEnumerable<RadiosItemViewModel> radios,
15+
bool populateWithCurrentValues,
16+
string hintText,
17+
bool required
18+
)
19+
{
20+
var radiosList = radios.Select(
21+
r => new RadiosItemViewModel(
22+
r.Value,
23+
r.Label,
24+
IsSelectedRadio(aspFor, r, populateWithCurrentValues),
25+
r.HintText
26+
)
27+
);
28+
29+
var viewModel = new RadiosViewModel(
30+
aspFor,
31+
label,
32+
string.IsNullOrEmpty(hintText) ? null : hintText,
33+
radiosList,
34+
required
35+
);
36+
37+
return View(viewModel);
38+
}
39+
40+
private bool IsSelectedRadio(string aspFor, RadiosItemViewModel radioItem, bool populateWithCurrentValue)
41+
{
42+
var model = ViewData.Model;
43+
var property = model.GetType().GetProperty(aspFor);
44+
var value = property?.GetValue(model);
45+
return populateWithCurrentValue && value.Equals(radioItem.Value);
46+
}
47+
}
48+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace NHSUKViewComponents.Web.ViewModels
2+
{
3+
public class RadiosItemViewModel
4+
{
5+
public RadiosItemViewModel(string value, string label, bool selected, string hintText)
6+
{
7+
Value = value;
8+
Label = label;
9+
Selected = selected;
10+
HintText = hintText;
11+
}
12+
13+
public string Value { get; set; }
14+
15+
public string Label { get; set; }
16+
17+
public bool Selected { get; set; }
18+
19+
public string HintText { get; set; }
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace NHSUKViewComponents.Web.ViewModels
2+
{
3+
using System.Collections.Generic;
4+
5+
public class RadiosViewModel
6+
{
7+
public RadiosViewModel(
8+
string aspFor,
9+
string label,
10+
string hintText,
11+
IEnumerable<RadiosItemViewModel> radios,
12+
bool required
13+
)
14+
{
15+
AspFor = aspFor;
16+
Label = !required && !label.EndsWith("(optional)") ? label + " (optional)" : label;
17+
HintText = hintText;
18+
Radios = radios;
19+
Required = required;
20+
}
21+
22+
public string AspFor { get; set; }
23+
24+
public string Label { get; set; }
25+
26+
public string HintText { get; set; }
27+
28+
public IEnumerable<RadiosItemViewModel> Radios { get; set; }
29+
public bool Required { get; set; }
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@using NHSUKViewComponents.Web.Extensions
2+
@using NHSUKViewComponents.Web.ViewModels
3+
4+
@model RadiosViewModel
5+
6+
<div class="nhsuk-form-group">
7+
8+
<fieldset class="nhsuk-fieldset" aria-describedby="@Model.Label.RemoveWhitespace()-hint">
9+
<legend class="nhsuk-fieldset__legend nhsuk-fieldset__legend--m">
10+
<label class="nhsuk-fieldset__heading">
11+
@Model.Label
12+
</label>
13+
</legend>
14+
15+
@if (Model.HintText != null)
16+
{
17+
<div class="nhsuk-hint" id="@Model.Label.RemoveWhitespace()-hint">
18+
@Html.Raw(Model.HintText)
19+
</div>
20+
}
21+
22+
<div class="nhsuk-radios" aria-required="@(Model.Required ? "true" : "false" )">
23+
@foreach (var (radio, index) in Model.Radios.Select((r, i) => (r, i)))
24+
{
25+
var radioId = $"{radio.Value}-{index}";
26+
<div class="nhsuk-radios__item">
27+
<input class="nhsuk-radios__input"
28+
id="@radioId"
29+
name="@Model.AspFor"
30+
type="radio"
31+
value="@radio.Value"
32+
aria-describedby="@radio.Value-item-hint"
33+
@(radio.Selected ? "checked" : string.Empty) />
34+
<label class="nhsuk-label nhsuk-radios__label" for="@radioId">
35+
@radio.Label
36+
</label>
37+
@if (radio.HintText != null)
38+
{
39+
<div class="nhsuk-hint nhsuk-radios__hint" id="@radio.Value-item-hint">
40+
@radio.HintText
41+
</div>
42+
}
43+
</div>
44+
}
45+
46+
</div>
47+
</fieldset>
48+
49+
</div>

0 commit comments

Comments
 (0)