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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class DateInputViewComponent : ViewComponent
/// <param name="yearId"></param>
/// <param name="cssClass">Leave blank for no custom css class.</param>
/// <param name="hintTextLines">Leave blank for no hint.</param>
/// <param name="isPageHeading">Leave blank/false for set label as page body .</param>
/// <returns></returns>
public IViewComponentResult Invoke(
string id,
Expand All @@ -26,7 +27,8 @@ public IViewComponentResult Invoke(
string monthId,
string yearId,
string cssClass,
IEnumerable<string>? hintTextLines
IEnumerable<string>? hintTextLines,
bool? isPageHeading = false
)
{
var model = ViewData.Model;
Expand Down Expand Up @@ -59,7 +61,8 @@ public IViewComponentResult Invoke(
yearErrors?.Count > 0,
nonEmptyErrors,
string.IsNullOrEmpty(cssClass) ? null : cssClass,
hintTextLines.Any() ? hintTextLines : null
hintTextLines.Any() ? hintTextLines : null,
isPageHeading
);
return View(viewModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,33 @@ public IViewComponentResult Invoke(
string hintText,
bool required,
string? requiredClientSideErrorMessage = default,
string cssClass = default
string cssClass = default,
string? optionalRadio = default
)
{
var model = ViewData.Model;
var property = model.GetType().GetProperty(aspFor);
var errorMessages = ViewData.ModelState[property?.Name]?.Errors.Select(e => e.ErrorMessage) ??
new string[] { };

var radiosList = radios.Select(
var radiosList = radios.Where(x=>x.Label != optionalRadio).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,
string.IsNullOrEmpty(optionalRadio) ? null : radios.Where(x => x.Label == optionalRadio)
.Select(r => new RadiosItemViewModel(r.Value, r.Label, IsSelectedRadio(aspFor, r, populateWithCurrentValues), r.HintText))
.FirstOrDefault(),
errorMessages,
required,
string.IsNullOrEmpty(requiredClientSideErrorMessage) ? null : requiredClientSideErrorMessage,
Expand Down
6 changes: 5 additions & 1 deletion NHSUKViewComponents.Web/ViewModels/DateInputViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public DateInputViewModel(
bool hasYearError,
IEnumerable<string> errorMessages,
string? cssClass = null,
IEnumerable<string>? hintTextLines = null
IEnumerable<string>? hintTextLines = null,
bool? isPageHeading = false
)
{
Id = id;
Expand All @@ -35,6 +36,7 @@ public DateInputViewModel(
YearValue = yearValue;
CssClass = cssClass;
HintTextLines = hintTextLines;
IsPageHeading = isPageHeading;
HasDayError = hasDayError;
HasMonthError = hasMonthError;
HasYearError = hasYearError;
Expand All @@ -51,6 +53,8 @@ public DateInputViewModel(
public string? YearValue { get; set; }
public string? CssClass { get; set; }
public IEnumerable<string>? HintTextLines { get; set; }
public bool? IsPageHeading { get; set; }

public bool HasError => HasDayError || HasMonthError || HasYearError;
public IEnumerable<string> ErrorMessages { get; set; }
}
Expand Down
3 changes: 3 additions & 0 deletions NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public RadiosViewModel(
string label,
string hintText,
IEnumerable<RadiosItemViewModel> radios,
RadiosItemViewModel optionalRadio,
IEnumerable<string> errorMessages,
bool required,
string? requiredClientSideErrorMessage = default,
Expand All @@ -21,6 +22,7 @@ public RadiosViewModel(
Label = !required && !label.EndsWith("(optional)") ? label + " (optional)" : label;
HintText = hintText;
Radios = radios;
OptionalRadio = optionalRadio;
ErrorMessages = errorMessageList;
HasError = errorMessageList.Any();
Required = required;
Expand All @@ -38,6 +40,7 @@ public RadiosViewModel(
public readonly bool HasError;

public IEnumerable<RadiosItemViewModel> Radios { get; set; }
public RadiosItemViewModel OptionalRadio { get; set; }
public bool Required { get; set; }
public string RequiredClientSideErrorMessage { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@

<div class="@Model.CssClass @errorCss" id="@Model.Id">
<fieldset class="nhsuk-fieldset" aria-describedby="@Model.Id-hint" role="group">
<legend class="nhsuk-fieldset__legend nhsuk-label">
@Model.Label
<legend class="nhsuk-fieldset__legend @(Model.IsPageHeading.GetValueOrDefault() ? "nhsuk-fieldset__legend--l":"nhsuk-label")">
@if(Model.IsPageHeading.GetValueOrDefault()==true)
{
<h1 class="nhsuk-fieldset__heading">
@Model.Label
</h1>
}
else
{
@Model.Label
}

</legend>
@if (Model.HintTextLines != null) {
@foreach (var hintText in Model.HintTextLines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@using NHSUKViewComponents.Web.ViewModels

@model RadiosViewModel
@{
int counter = 0;

}

<div class="nhsuk-form-group @(Model.HasError ? "nhsuk-form-group--error" : "")">

Expand Down Expand Up @@ -40,6 +44,7 @@
<div class="nhsuk-radios" aria-required="@(Model.Required ? "true" : "false" )">
@foreach (var (radio, index) in Model.Radios.Select((r, i) => (r, i)))
{
counter = index;
var radioId = $"{radio.Value}-{index}";
if (!string.IsNullOrWhiteSpace(Model.Class))
{
Expand Down Expand Up @@ -91,6 +96,31 @@
}

}
@if(Model.OptionalRadio != null)
{
<div class="nhsuk-radios__divider nhsuk-u-padding-left-2">or</div>
var radioId = $"{Model.OptionalRadio.Value}-{++counter}";
<div class="nhsuk-radios__item">
<input class="nhsuk-radios__input"
id="@radioId"
name="@Model.AspFor"
type="radio"
value="@Model.OptionalRadio.Value"
aria-describedby="@Model.OptionalRadio.Value-item-hint"
data-val-required="@(Model.Required ? Model.RequiredClientSideErrorMessage : "" )"
data-val="@(Model.Required ? "true" : "false" )"
@(Model.OptionalRadio.Selected ? "checked" : string.Empty) />
<label class="nhsuk-label nhsuk-radios__label" for="@radioId">
@Model.OptionalRadio.Label
</label>
@if (Model.OptionalRadio.HintText != null)
{
<div class="nhsuk-hint nhsuk-radios__hint" id="@Model.OptionalRadio.Value-item-hint">
@Model.OptionalRadio.HintText
</div>
}
</div>
}

</div>
</fieldset>
Expand Down