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

Spike conditional reveals alternative visually hidden text #1560

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/govuk/components/checkboxes/checkboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ Checkboxes.prototype.init = function () {
return
}

// If we have content that is controlled, set attributes.
$input.setAttribute('aria-controls', controls)
$input.removeAttribute('data-aria-controls')
this.setAttributes($input)
}.bind(this))

Expand All @@ -38,9 +35,15 @@ Checkboxes.prototype.init = function () {

Checkboxes.prototype.setAttributes = function ($input) {
var inputIsChecked = $input.checked
$input.setAttribute('aria-expanded', inputIsChecked)
var $label = document.querySelector('.govuk-checkboxes__label[for=' + $input.id + ']')
// If the label does not have hidden text for state, inject it.
if (!$label.querySelector('.js-govuk-checkboxes__label__state')) {
$label.innerHTML = $label.innerHTML + '<span class="govuk-visually-hidden js-govuk-checkboxes__label__state"></span>'
}
// Set the hidden text for state depending on if it's expanded or not.
$label.querySelector('.js-govuk-checkboxes__label__state').innerText = inputIsChecked ? ', Expanded' : ', Collapsed'

var $content = this.$module.querySelector('#' + $input.getAttribute('aria-controls'))
var $content = this.$module.querySelector('#' + $input.getAttribute('data-aria-controls'))
if ($content) {
$content.classList.toggle('govuk-checkboxes__conditional--hidden', !inputIsChecked)
}
Expand All @@ -51,7 +54,7 @@ Checkboxes.prototype.handleClick = function (event) {

// If a checkbox with aria-controls, handle click
var isCheckbox = $target.getAttribute('type') === 'checkbox'
var hasAriaControls = $target.getAttribute('aria-controls')
var hasAriaControls = $target.getAttribute('data-aria-controls')
if (isCheckbox && hasAriaControls) {
this.setAttributes($target)
}
Expand Down
18 changes: 10 additions & 8 deletions src/govuk/components/radios/radios.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ Radios.prototype.init = function () {
return
}

// If we have content that is controlled, set attributes.
$input.setAttribute('aria-controls', controls)
$input.removeAttribute('data-aria-controls')
this.setAttributes($input)
}.bind(this))

Expand All @@ -36,12 +33,17 @@ Radios.prototype.init = function () {
}

Radios.prototype.setAttributes = function ($input) {
var $content = document.querySelector('#' + $input.getAttribute('aria-controls'))
var $content = document.querySelector('#' + $input.getAttribute('data-aria-controls'))

if ($content && $content.classList.contains('govuk-radios__conditional')) {
var inputIsChecked = $input.checked

$input.setAttribute('aria-expanded', inputIsChecked)
var $label = document.querySelector('.govuk-radios__label[for=' + $input.id + ']')
// If the label does not have hidden text for state, inject it.
if (!$label.querySelector('.js-govuk-radios__label__state')) {
$label.innerHTML = $label.innerHTML + '<span class="govuk-visually-hidden js-govuk-radios__label__state"></span>'
}
// Set the hidden text for state depending on if it's expanded or not.
$label.querySelector('.js-govuk-radios__label__state').innerText = inputIsChecked ? ', Expanded' : ', Collapsed'

$content.classList.toggle('govuk-radios__conditional--hidden', !inputIsChecked)
}
Expand All @@ -56,8 +58,8 @@ Radios.prototype.handleClick = function (event) {
// Because checking one radio can uncheck a radio in another $module,
// we need to call set attributes on all radios in the same form, or document if they're not in a form.
//
// We also only want radios which have aria-controls, as they support conditional reveals.
var $allInputs = document.querySelectorAll('input[type="radio"][aria-controls]')
// We also only want radios which have data-aria-controls, as they support conditional reveals.
var $allInputs = document.querySelectorAll('input[type="radio"][data-aria-controls]')
nodeListForEach($allInputs, function ($input) {
// Only inputs with the same form owner should change.
var hasSameFormOwner = ($input.form === $clickedInput.form)
Expand Down