-
Notifications
You must be signed in to change notification settings - Fork 9
/
GovukCharacterCount.scala.html
90 lines (80 loc) · 3.54 KB
/
GovukCharacterCount.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
@*
* 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.immutable.ListMap
@this(govukTextarea: GovukTextarea,
govukHint: GovukHint)
@(params: CharacterCount)
@import params._
@require(name.nonEmpty && id.nonEmpty, "name and id should be non-empty")
@govukPluralisedI18nAttributes(translationKey: String, pluralForms: Option[Map[String, String]]) = @{
pluralForms.getOrElse(Map.empty).map { case (key, value) =>
s"data-i18n.$translationKey.$key" -> value
}
}
@dataAttributes = @{
val maybeDataAttributes = ListMap(
"data-module" -> "govuk-character-count",
"data-maxlength" -> maxLength.filter(_ > 0).map(_.toString).getOrElse(""),
"data-threshold" -> threshold.filter(_ > 0).map(_.toString).getOrElse(""),
"data-maxwords" -> maxWords.filter(_ > 0).map(_.toString).getOrElse(""),
) ++ govukPluralisedI18nAttributes("characters-under-limit", charactersUnderLimitText) ++ Map(
"data-i18n.characters-at-limit" -> charactersAtLimitText.getOrElse("")
) ++ govukPluralisedI18nAttributes("characters-over-limit", charactersOverLimitText) ++ govukPluralisedI18nAttributes("words-under-limit", wordsUnderLimitText) ++ Map(
"data-i18n.words-at-limit" -> wordsAtLimitText.getOrElse("")
) ++ govukPluralisedI18nAttributes("words-over-limit", wordsOverLimitText) ++ Map(
"data-i18n.textarea-description.other" -> textareaDescriptionText.filter(_ => maxLength.isEmpty && maxWords.isEmpty).getOrElse("")
)
maybeDataAttributes.filter { case (_, value) => value.nonEmpty }
}
@customTextArea = @{
val max = maxWords.filter(_ > 0).orElse(maxLength).getOrElse("undefined")
if(max == "undefined") {
{ Text("") }
} else {
textareaDescriptionText match {
case Some(textAreaText) => { Text(textAreaText.replace("%{count}", "" + max)) }
case None => { Text("You can enter up to " + max + " " + maxWords.filter(_ > 0).fold("characters")(_ => "words")) }
}
}
}
@characterCountMessage = {
@govukHint(Hint(id=Some(id+"-info"),
classes = toClasses("govuk-character-count__message", if (countMessageClasses.isEmpty) "" else s" $countMessageClasses"),
content = customTextArea
))
}
@characterCountMessagePlusOptionalAfterInput = {
@characterCountMessage
@formGroup.afterInput.map(_.asHtml)
}
@govukTextarea(Textarea(
id = id,
name = name,
describedBy = Some(s"${id}-info"),
rows = rows,
value = value,
label = label.copy(forAttr = Some(id)),
hint = hint,
errorMessage = errorMessage,
formGroup = formGroup.copy(
classes = Some(toClasses("govuk-character-count", formGroup.classes.getOrElse(""))),
attributes = dataAttributes ++ formGroup.attributes,
afterInput = Some(HtmlContent(characterCountMessagePlusOptionalAfterInput))
),
classes = toClasses("govuk-js-character-count", if (classes.isEmpty) "" else s" $classes"),
attributes = attributes
))