Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLATUI-2918 add empty value #304

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
For compatibility information see `govukFrontendVersion` and `hmrcFrontendVersion` in
[LibDependencies](project/LibDependencies.scala)

## [9.11.0] - 2024-05-09

### Changed

- Added an `emptyItem` parameter to AccessibleAutocomplete to provide an item with no value to the top of a list.

### Compatible with

- [hmrc/hmrc-frontend v6.17.0](https://github.com/hmrc/hmrc-frontend/releases/tag/v6.17.0)
- [alphagov/govuk-frontend v5.3.0](https://github.com/alphagov/govuk-frontend/releases/tag/v5.3.0)

## [9.10.0] - 2024-04-24

### Changed
Expand Down
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -915,17 +915,13 @@ If you wish to change these values you can do so by providing an `AccessibleAuto
```

There is a caveat currently with the `defaultValue` property, to use this option you will need to ensure
that you have a placeholder item in your select element that has an empty value. You will also need to make sure none of
your select items have the selected property set on them. See example below.
that you have an empty item in your select element that has an empty value. You can do this with the `emptyItem` property. See example below.

```scala
@govukSelect(Select(
id = "sort",
name = "sort",
items = Seq(
SelectItem(
text = "Placeholder text"
),
SelectItem(
value = Some("published"),
text = "Recently published"
Expand All @@ -940,15 +936,16 @@ your select items have the selected property set on them. See example below.
)
).asAccessibleAutocomplete(Some(
AccessibleAutocomplete(
emptyItem = Some(""),
defaultValue = Some("Recently updated"),
showAllValues = true,
autoSelect = false)
)))
```

More information on `defaultValue` property can be found [here](https://www.npmjs.com/package/accessible-autocomplete) under the `Null options` heading
More information on `defaultValue` property can be found [here](https://www.npmjs.com/package/accessible-autocomplete) under the `Null options` heading.

A preferred way would be to select a default value using the `selected` attribute on an select item instead of using the `defaultValue` property, as seen below.
Another way would be to use the `emptyItem` parameter on `AccessibleAutocomplete` with `Some("String")`. This will act as an item with no value, the text will only show up when JavaScript is disabled. If the first or selected item has text in it, the text would show up in the input, preventing the rest of the list from showing with `AccessibleAutocomplete` without deleting the text first.

```scala
@govukSelect(Select(
Expand All @@ -962,7 +959,6 @@ A preferred way would be to select a default value using the `selected` attribut
SelectItem(
value = Some("updated"),
text = "Recently updated"
selected = true
)
),
label = Label(
Expand All @@ -971,11 +967,12 @@ A preferred way would be to select a default value using the `selected` attribut
).asAccessibleAutocomplete(Some(
AccessibleAutocomplete(
showAllValues = true,
autoSelect = false)
autoSelect = false,
emptyItem = Some("Select an item")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I know people said no to placeholder - but I don't think it will be clear what this param will do - a bit of me is wondering seeing this, if we're not going to ever forward this to the placeholder param of a11y autocomplete, if we should even give people an option for this rather that just having it be guidance that you need to explicitly pass an empty value to create a initial placeholder option

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emptyItem isn't an intuitive name to me, but I'm struggling to come up with a concise alternative 😅

)
)))
```


### Warning users before timing them out

In order to meet the accessibility [WCAG 2.1 Principle 2: Operable](https://www.w3.org/TR/WCAG21/#operable) you must
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ trait RichSelectSupport {
def withHeadingAndSectionCaption(heading: Content, sectionCaption: Content): Select =
withHeadingLabel(select, heading, Some(sectionCaption))((sl, ul) => sl.copy(label = ul))

def withEmptyItem(emptyItemText: String): Select = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a test for this new extension method?

val emptyItem: SelectItem = SelectItem.emptyItemObject(emptyItemText)
select.copy(
items = emptyItem +: select.items
)
}

def asAccessibleAutocomplete(
accessibleAutocomplete: Option[AccessibleAutocomplete] = None
)(implicit messages: Messages): Select = {
Expand All @@ -72,15 +79,26 @@ trait RichSelectSupport {
"data-show-all-values" -> accessibleAutocomplete.showAllValues.toString,
"data-default-value" -> accessibleAutocomplete.defaultValue.getOrElse(""),
"data-min-length" -> accessibleAutocomplete.minLength.map(_.toString).getOrElse(""),
"data-empty-item" -> accessibleAutocomplete.emptyItem.map(_.toString).getOrElse(""),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(where) is this data-empty-item attribute actually used?

"data-module" -> accessibleAutocomplete.dataModule
)

val dataAttributes =
toMapOfDataAttributes(accessibleAutocomplete.getOrElse(AccessibleAutocomplete(None)))

val maybeDataLanguage = Map("data-language" -> messages.lang.code).filterNot(_._2 == En.code)

select.copy(attributes = select.attributes ++ dataAttributes ++ maybeDataLanguage)
val maybeEmptyItem = accessibleAutocomplete.map(_.emptyItem).getOrElse(None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getOrElse(None) is a bit weird to see - you can .flatten nested Options, but then .map(...).flatten simplifies to .flatMap(...)

Suggested change
val maybeEmptyItem = accessibleAutocomplete.map(_.emptyItem).getOrElse(None)
val maybeEmptyItem = accessibleAutocomplete.flatMap(_.emptyItem)


maybeEmptyItem match {
case None =>
select.copy(attributes = select.attributes ++ dataAttributes ++ maybeDataLanguage)
case Some(text) =>
val emptyItem: SelectItem = SelectItem.emptyItemObject(text)
select.copy(
attributes = select.attributes ++ dataAttributes ++ maybeDataLanguage,
items = emptyItem +: select.items
)
}
Comment on lines +92 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would maybe restructure this to isolate the conditional empty item bit from the attributes which are the same in both cases

Suggested change
maybeEmptyItem match {
case None =>
select.copy(attributes = select.attributes ++ dataAttributes ++ maybeDataLanguage)
case Some(text) =>
val emptyItem: SelectItem = SelectItem.emptyItemObject(text)
select.copy(
attributes = select.attributes ++ dataAttributes ++ maybeDataLanguage,
items = emptyItem +: select.items
)
}
(maybeEmptyItem match {
case Some(text) => select.copy(items = SelectItem.emptyItemObject(text) +: select.items)
case None => select
}).copy(attributes = select.attributes ++ dataAttributes ++ maybeDataLanguage)

}

private[views] def withName(field: Field): Select =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ final case class SelectItem(

object SelectItem {

def defaultObject: SelectItem = SelectItem()
def defaultObject: SelectItem = SelectItem()
def emptyItemObject(emptyItemText: String): SelectItem = SelectItem(value = Some(""), text = emptyItemText)

implicit def jsonReads: Reads[SelectItem] = (
(__ \ "value").readsJsValueToString.map(Option[String]).orElse(Reads.pure(None)) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ case class AccessibleAutocomplete(
None, // Please read on the usage of the defaultValue property at https://www.npmjs.com/package/accessible-autocomplete under the `Null options` heading
showAllValues: Boolean = false,
autoSelect: Boolean = false,
minLength: Option[Int] = None
minLength: Option[Int] = None,
emptyItem: Option[String] = None
) {
val dataModule: String = "hmrc-accessible-autocomplete"
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class RichSelectSpec extends AnyWordSpec with Matchers with MessagesHelpers with
"data-show-all-values" -> "true",
"data-auto-select" -> "true",
"data-min-length" -> "",
"data-empty-item" -> "",
"data-module" -> "hmrc-accessible-autocomplete"
)
)
Expand All @@ -200,6 +201,7 @@ class RichSelectSpec extends AnyWordSpec with Matchers with MessagesHelpers with
"data-show-all-values" -> "true",
"data-auto-select" -> "true",
"data-min-length" -> "",
"data-empty-item" -> "",
"data-module" -> "hmrc-accessible-autocomplete",
"data-language" -> "cy"
)
Expand Down Expand Up @@ -227,6 +229,7 @@ class RichSelectSpec extends AnyWordSpec with Matchers with MessagesHelpers with
"data-show-all-values" -> "true",
"data-auto-select" -> "true",
"data-min-length" -> "2",
"data-empty-item" -> "",
"data-module" -> "hmrc-accessible-autocomplete",
"data-language" -> "cy"
)
Expand All @@ -243,6 +246,7 @@ class RichSelectSpec extends AnyWordSpec with Matchers with MessagesHelpers with
"data-show-all-values" -> "false",
"data-auto-select" -> "false",
"data-min-length" -> "",
"data-empty-item" -> "",
"data-module" -> "hmrc-accessible-autocomplete"
)
)
Expand All @@ -258,6 +262,7 @@ class RichSelectSpec extends AnyWordSpec with Matchers with MessagesHelpers with
"data-show-all-values" -> "false",
"data-auto-select" -> "false",
"data-min-length" -> "",
"data-empty-item" -> "",
"data-module" -> "hmrc-accessible-autocomplete",
"some-attr1" -> "1",
"some-attr2" -> "2"
Expand Down