-
Notifications
You must be signed in to change notification settings - Fork 13
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
Follow up questions #304
Merged
Merged
Follow up questions #304
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e4c1d49
Remove 'is defined' check from textbox form template
allait 55c3ca8
Add followup support for radio and checkbox questions
allait a7ba947
Add support for `hidden` flag to list-entry inputs
allait 0404610
Vendor show-hide-content.js file from https://github.com/alphagov/gov…
allait ea3152e
Add combination examples for checkboxes and list-entry
allait a93f7e9
Fix False values being replace with labels in boolean input
allait 74eada6
Bump version to 21.0.0
allait 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
20.0.1 | ||
21.0.0 |
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,194 @@ | ||
;(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) | ||
var contentIds = '' | ||
|
||
// Set aria-controls and defaults | ||
if ($content.length) { | ||
$content.each(function () { | ||
$(this).attr('aria-hidden', 'true') | ||
contentIds += ' ' + $(this).attr('id') | ||
}) | ||
|
||
$control.attr('aria-controls', contentIds.trim()) | ||
$control.attr('aria-expanded', 'false') | ||
} | ||
} | ||
|
||
// Return toggled content for control | ||
function getToggledContent ($control) { | ||
var targetIds = $control.attr('aria-controls') | ||
|
||
// ARIA attributes aren't set before init | ||
if (!targetIds) { | ||
targetIds = $control.closest('label').data('target') | ||
} | ||
|
||
// turn a space-separated list of ids into a comma-separated css id selector | ||
// ie, 'id-1 id-2 id-3' becomes '#id-1, #id-2, #id-3' | ||
if (targetIds) { | ||
targetIds = targetIds | ||
.split(' ') | ||
.map(function (targetId) { return '#' + targetId }) | ||
.join(', ') | ||
} | ||
|
||
// Find show/hide content by id | ||
return $(targetIds) | ||
} | ||
|
||
// Show toggled content for control | ||
function showToggledContent ($control, $content) { | ||
if (!$content.length) { | ||
return | ||
} | ||
|
||
// 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) | ||
|
||
if (!$content.length) { | ||
return | ||
} | ||
|
||
// 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 $form = $control.closest('form') | ||
var $radios = $form.length ? $form.find(selector) : $(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
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.
This should be the other way around, shouldn't it?
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.
This is not YAML format used by the content loader, it's the data we'd normally pass into toolkit form macros, so this is what
.values_followup
would return, which is a reversed mapping.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.
ah ok