Skip to content

Commit 15fb5cc

Browse files
authored
Merge pull request #5251 from alphagov/select-with-search-fix3
Change blank repeated "Select One" option to "Select None"
2 parents 4e19536 + dfadc7f commit 15fb5cc

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
useful summary for people upgrading their application, not a replication
88
of the commit log.
99

10+
## Unreleased
11+
12+
* Change blank repeated "Select One" option to "Select None" ([PR #5251](https://github.com/alphagov/govuk_publishing_components/pull/5251))
13+
1014
## 64.1.1
1115

1216
* Make input search styles less specific ([PR #5282](https://github.com/alphagov/govuk_publishing_components/pull/5282))

app/assets/javascripts/govuk_publishing_components/components/select-with-search.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}
1313
return
1414
}
1515

16+
const blankOptionText = 'Select none'
1617
const placeholderOption = this.module.querySelector(
1718
'option[value=""]:first-child'
1819
)
@@ -42,6 +43,21 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}
4243
const inner = this.containerInner.element
4344
const input = this.input.element
4445
inner.prepend(input)
46+
} else {
47+
// Update text for blank option in the hidden select
48+
const selectElement = this.passedElement.element
49+
if (selectElement.id.search('blank') > 0) {
50+
selectElement.firstChild.innerText = blankOptionText
51+
}
52+
// Update text for blank option in the choices dropdown
53+
// choices.js 'lastChild' in this context is the listbox of choices:
54+
const listbox = this.dropdown.element.lastChild
55+
// choices.js 'firstChild' in this context is the first option.
56+
// This always displays "Select One" for selects with a blank option:
57+
var blankOption = listbox.firstChild
58+
if (blankOption && blankOption.id.search('blank') > 0) {
59+
blankOption.innerText = blankOptionText
60+
}
4561
}
4662
// Add aria-labelledby to the listbox as well as the combobox
4763
const listbox = this.itemList.element
@@ -54,6 +70,23 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}
5470
}
5571
})
5672

73+
// Reset blank 'Select One' to 'Select None' on each change
74+
// This is because choices.js rebuilds the widget on each
75+
// change event and resets the text. We can't use the preferred
76+
// refresh method as this loses the current state of the dropdown.
77+
const selectElement = this.choices.passedElement.element
78+
const listbox = this.choices.dropdown.element.lastChild
79+
selectElement.addEventListener(
80+
'change',
81+
function (event) {
82+
var blankOption = listbox.firstChild
83+
if (blankOption && blankOption.id.search('blank') > 0) {
84+
blankOption.innerText = blankOptionText
85+
}
86+
},
87+
false
88+
)
89+
5790
this.module.choices = this.choices
5891
}
5992

0 commit comments

Comments
 (0)