-
Notifications
You must be signed in to change notification settings - Fork 9
/
GovukRadios.scala.html
108 lines (99 loc) · 4.61 KB
/
GovukRadios.scala.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
@*
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*@
@import uk.gov.hmrc.govukfrontend.views.html.components._
@import scala.collection.compat.immutable.LazyList
@import uk.gov.hmrc.govukfrontend.views.html.helpers._
@this(govukFieldset: GovukFieldset, govukHint: GovukHint, govukLabel: GovukLabel, govukFormGroup: GovukFormGroup, govukHintAndErrorMessage: GovukHintAndErrorMessage)
@(params: Radios)
@import params._
@require(items.count(_.checked) <= 1, "only one checked item allowed in a radio button")
@require(value.isEmpty || !items.exists(_.checked) || Seq(value) == items.filter(_.checked).map(_.value), "value conflicts with checked item")
@defining(fieldset.fold("")(fieldset => fieldset.describedBy.getOrElse(""))) { describedBy =>
@defining(govukHintAndErrorMessage(idPrefixStr, describedBy.toOption, hint, errorMessage)(divSnippet)) { innerHtml =>
@govukFormGroup(formGroup, errorMessage) {
@fieldset.fold(innerHtml) { fieldset =>
@govukFieldset(Fieldset(
describedBy = fieldSetDescribedBy(describedBy),
legend = fieldset.legend,
classes = fieldset.classes,
attributes = fieldset.attributes,
html = innerHtml
))
}
}
}
}
@fieldSetDescribedBy(describedBy: String) = @{
val describedByWithHintId = hint.fold(describedBy)(_ => s"$describedBy $idPrefixStr-hint".ltrim)
val describedByWithErrorId = errorMessage.fold(describedByWithHintId)(_ => s"$describedByWithHintId $idPrefixStr-error".ltrim)
describedByWithErrorId.toOption
}
@idPrefixStr = @{idPrefix.getNonEmptyOrElse(name)}
@divSnippet(unusedDescribedBy: String) = {
<div class="@toClasses("govuk-radios", classes)"@toAttributes(attributes) data-module="govuk-radios">
@{formGroup.beforeInput.map(_.asHtml)}
@for((item, index) <- items.zip(LazyList from 1)) {
@defining(itemId(item, index)) { id =>
@defining(s"conditional-$id") { conditionalId =>
@item.divider.filter(_.nonEmpty) match {
case Some(divider) => {<div class="govuk-radios__divider">@divider</div>}
case None => {
@defining(item.hint.exists(_.content.nonEmpty)) { hasHint =>
@defining(s"$id-item-hint") { itemHintId =>
@defining((item.value.nonEmpty && item.value == value) || item.checked) { isChecked =>
@defining {
@if(isChecked) { checked}
@if(item.disabled) { disabled}
@item.conditionalHtml.filter(_.nonEmpty).map {_ => data-aria-controls="@conditionalId"}
@if(hasHint) {aria-describedby="@itemHintId"}
} { inputAttrs =>
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="@id" name="@name" type="radio" value="@item.value"@inputAttrs@toAttributes(item.attributes)>
@govukLabel(Label(
forAttr = Some(id),
classes = toClasses("govuk-radios__label", item.label.map(_.classes).getOrElse("")),
attributes = item.label.map(_.attributes).getOrElse(Map.empty),
content = item.content
))
@if(hasHint) {
@govukHint(Hint(
id = Some(itemHintId),
classes = toClasses("govuk-radios__hint", item.hint.map(_.classes).getOrElse("")),
attributes = item.hint.map(_.attributes).getOrElse(Map.empty),
content = item.hint.map(_.content).getOrElse(Empty)
))
}
</div>
@item.conditionalHtml.filter(_.nonEmpty).map { conditionalHtml =>
<div class="@toClasses("govuk-radios__conditional", "govuk-radios__conditional--hidden".when(!isChecked))" id="@conditionalId">
@conditionalHtml
</div>
}
}}}}
}
}
}}
}
@{formGroup.afterInput.map(_.asHtml)}
</div>
}
@itemId(item: RadioItem, index: Int) = @{
(item.id, index) match {
case (Some(NonEmptyString(id)), _) => id
case (Some(_) | None, 1) => idPrefixStr
case _ => s"$idPrefixStr-$index"
}
}