-
Notifications
You must be signed in to change notification settings - Fork 104
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
Add GOVUK.ShowHideContent JavaScript #315
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1c0db7f
Add GOVUK.ShowHideContent
colinrotherham 51de396
Add clarity to GOVUK.ShowHideContent docs
colinrotherham ba6936e
Slight performance tweak to GOVUK.ShowHideContent
colinrotherham 77268de
Use Standard to format JS
gemmaleigh cbbcb38
Use Standard to lint the spec
gemmaleigh 63362d8
Use closest() rather than parent()
gemmaleigh 6caa441
Remove unused class js-toggle-showhide from spec
gemmaleigh 3999832
Run handleRadioContent only on controlling radios
tombye 9d9b3c6
Add fixes for standard JS errors
tombye 91a8ed7
Re-point paths to jasmine runner files
tombye 420b23d
Only bind events to controlling selection buttons
tombye a8ae9ee
Remove parenthesis from return expression
tombye File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
;(function (global) { | ||
'use strict' | ||
|
||
var $ = global.jQuery | ||
var GOVUK = global.GOVUK || {} | ||
|
||
function ShowHideContent () { | ||
var self = this | ||
|
||
// Radio and Checkbox selectors | ||
var selectors = { | ||
namespace: 'ShowHideContent', | ||
radio: '.block-label[data-target] input[type="radio"]', | ||
checkbox: '.block-label[data-target] input[type="checkbox"]' | ||
} | ||
|
||
// Escape name attribute for use in DOM selector | ||
function escapeElementName (str) { | ||
var result = str.replace('[', '\\[').replace(']', '\\]') | ||
return result | ||
} | ||
|
||
// Adds ARIA attributes to control + associated content | ||
function initToggledContent () { | ||
var $control = $(this) | ||
var $content = getToggledContent($control) | ||
|
||
// Set aria-controls and defaults | ||
if ($content.length) { | ||
$control.attr('aria-controls', $content.attr('id')) | ||
$control.attr('aria-expanded', 'false') | ||
$content.attr('aria-hidden', 'true') | ||
} | ||
} | ||
|
||
// Return toggled content for control | ||
function getToggledContent ($control) { | ||
var id = $control.attr('aria-controls') | ||
|
||
// ARIA attributes aren't set before init | ||
if (!id) { | ||
id = $control.closest('label').data('target') | ||
} | ||
|
||
// Find show/hide content by id | ||
return $('#' + id) | ||
} | ||
|
||
// Show toggled content for control | ||
function showToggledContent ($control, $content) { | ||
// Show content | ||
if ($content.hasClass('js-hidden')) { | ||
$content.removeClass('js-hidden') | ||
$content.attr('aria-hidden', 'false') | ||
|
||
// If the controlling input, update aria-expanded | ||
if ($control.attr('aria-controls')) { | ||
$control.attr('aria-expanded', 'true') | ||
} | ||
} | ||
} | ||
|
||
// Hide toggled content for control | ||
function hideToggledContent ($control, $content) { | ||
$content = $content || getToggledContent($control) | ||
|
||
// Hide content | ||
if (!$content.hasClass('js-hidden')) { | ||
$content.addClass('js-hidden') | ||
$content.attr('aria-hidden', 'true') | ||
|
||
// If the controlling input, update aria-expanded | ||
if ($control.attr('aria-controls')) { | ||
$control.attr('aria-expanded', 'false') | ||
} | ||
} | ||
} | ||
|
||
// Handle radio show/hide | ||
function handleRadioContent ($control, $content) { | ||
// All radios in this group which control content | ||
var selector = selectors.radio + '[name=' + escapeElementName($control.attr('name')) + '][aria-controls]' | ||
var $radios = $control.closest('form').find(selector) | ||
|
||
// Hide content for radios in group | ||
$radios.each(function () { | ||
hideToggledContent($(this)) | ||
}) | ||
|
||
// Select content for this control | ||
if ($control.is('[aria-controls]')) { | ||
showToggledContent($control, $content) | ||
} | ||
} | ||
|
||
// Handle checkbox show/hide | ||
function handleCheckboxContent ($control, $content) { | ||
// Show checkbox content | ||
if ($control.is(':checked')) { | ||
showToggledContent($control, $content) | ||
} else { // Hide checkbox content | ||
hideToggledContent($control, $content) | ||
} | ||
} | ||
|
||
// Set up event handlers etc | ||
function init ($container, elementSelector, eventSelectors, handler) { | ||
$container = $container || $(document.body) | ||
|
||
// Handle control clicks | ||
function deferred () { | ||
var $control = $(this) | ||
handler($control, getToggledContent($control)) | ||
} | ||
|
||
// Prepare ARIA attributes | ||
var $controls = $(elementSelector) | ||
$controls.each(initToggledContent) | ||
|
||
// Handle events | ||
$.each(eventSelectors, function (idx, eventSelector) { | ||
$container.on('click.' + selectors.namespace, eventSelector, deferred) | ||
}) | ||
|
||
// Any already :checked on init? | ||
if ($controls.is(':checked')) { | ||
$controls.filter(':checked').each(deferred) | ||
} | ||
} | ||
|
||
// Get event selectors for all radio groups | ||
function getEventSelectorsForRadioGroups () { | ||
var radioGroups = [] | ||
|
||
// Build an array of radio group selectors | ||
return $(selectors.radio).map(function () { | ||
var groupName = $(this).attr('name') | ||
|
||
if ($.inArray(groupName, radioGroups) === -1) { | ||
radioGroups.push(groupName) | ||
return 'input[type="radio"][name="' + $(this).attr('name') + '"]' | ||
} | ||
return null | ||
}) | ||
} | ||
|
||
// Set up radio show/hide content for container | ||
self.showHideRadioToggledContent = function ($container) { | ||
init($container, selectors.radio, getEventSelectorsForRadioGroups(), handleRadioContent) | ||
} | ||
|
||
// Set up checkbox show/hide content for container | ||
self.showHideCheckboxToggledContent = function ($container) { | ||
init($container, selectors.checkbox, [selectors.checkbox], handleCheckboxContent) | ||
} | ||
|
||
// Remove event handlers | ||
self.destroy = function ($container) { | ||
$container = $container || $(document.body) | ||
$container.off('.' + selectors.namespace) | ||
} | ||
} | ||
|
||
ShowHideContent.prototype.init = function ($container) { | ||
this.showHideRadioToggledContent($container) | ||
this.showHideCheckboxToggledContent($container) | ||
} | ||
|
||
GOVUK.ShowHideContent = ShowHideContent | ||
global.GOVUK = GOVUK | ||
})(window) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tombye, @gemmaleigh.
This has a side effect unfortunately… Now only controlling radios can be used to show/hide content.
Imagine a yes or no question and answer form, for example "Do you have a partner"?
Previously the "Yes" radio (controlling) could toggle the content open with further content, and the "No" radio in the same
name
group (not controlling) could toggle the content closed again.With this latest change, how do you close it once it's open?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure the method you show/hide content for radio groups works with the change in that scenario:
If you click 'yes', all content controlled by the group would be hidden by the loop on line 87. The condition on line 92 will return true and so show the content controlled by the event target.
If you click 'no', all content controlled by the group would still be hidden by the loop on line 87. The condition on line 92 will return false so no content is shown.
I ran the tests after making this change and they were green so figured it was ok. I made a jsbin to play around with it which seems to work as expected: https://jsbin.com/nademaxaxo/edit?html,js,output
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tombye, that's great.
Forgot I wrote a test to cover that very scenario!
https://github.com/alphagov/govuk_frontend_toolkit/pull/315/files#diff-1cb888219674ec4080d3a7d2bdca5117R183
Still works as intended then 👍