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 checkbox and radio form toggles #90
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
app/assets/javascripts/govuk-admin-template/modules/checkbox_toggle.js
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,31 @@ | ||
(function(Modules) { | ||
"use strict"; | ||
|
||
Modules.CheckboxToggle = function() { | ||
this.start = function(element) { | ||
var $checkboxes = element.find('input[type="checkbox"][data-target]'); | ||
|
||
$checkboxes.each(function() { | ||
var $checkbox = $(this), | ||
target = $checkbox.data('target'), | ||
$target = $('#' + target); | ||
|
||
$checkbox.attr('aria-controls', target); | ||
toggle(); | ||
$checkbox.on('click', toggle); | ||
|
||
function toggle() { | ||
var state = $checkbox.is(':checked'); | ||
$target.toggle(state); | ||
setAriaAttr(state) | ||
} | ||
|
||
function setAriaAttr(state) { | ||
$checkbox.attr('aria-expanded', state); | ||
$target.attr('aria-hidden', !state); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
})(window.GOVUKAdmin.Modules); |
46 changes: 46 additions & 0 deletions
46
app/assets/javascripts/govuk-admin-template/modules/radio_toggle.js
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,46 @@ | ||
(function(Modules) { | ||
"use strict"; | ||
|
||
Modules.RadioToggle = function() { | ||
this.start = function(element) { | ||
var $radios = element.find('input[type="radio"][data-target]'), | ||
radioGroups = {}; | ||
|
||
$radios.each(function() { | ||
var $radio = $(this), | ||
target = $radio.data('target'), | ||
$target = $('#' + target); | ||
|
||
radioGroups[$radio.attr('name')] = true; | ||
|
||
$radio.attr('aria-controls', target); | ||
$radio.on('radioValueChanged', toggle); | ||
|
||
function toggle() { | ||
var state = $radio.is(':checked'); | ||
$target.toggle(state); | ||
setAriaAttr(state); | ||
} | ||
|
||
function setAriaAttr(state) { | ||
$radio.attr('aria-expanded', state); | ||
$target.attr('aria-hidden', !state); | ||
} | ||
}); | ||
|
||
listenToChangesOnRadioGroups(); | ||
triggerToggles(); | ||
|
||
function listenToChangesOnRadioGroups() { | ||
$.map(radioGroups, function(v, radioGroupName) { | ||
element.on('change', 'input[type="radio"][name="'+radioGroupName+'"]', triggerToggles); | ||
}); | ||
} | ||
|
||
function triggerToggles() { | ||
$radios.trigger('radioValueChanged'); | ||
} | ||
}; | ||
}; | ||
|
||
})(window.GOVUKAdmin.Modules); |
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,110 @@ | ||
describe('A checkbox toggle module', function() { | ||
"use strict"; | ||
|
||
var root = window, | ||
toggle, | ||
toggleElement; | ||
|
||
beforeEach(function() { | ||
toggleElement = $('<div>\ | ||
<label>\ | ||
<input type="checkbox" class="without-target"> Normal checkbox\ | ||
</label>\ | ||
<label>\ | ||
<input type="checkbox" data-target="target-id" class="with-target"> Check me\ | ||
</label>\ | ||
<div id="target-id">Content</div>\ | ||
'); | ||
|
||
toggle = new GOVUKAdmin.Modules.CheckboxToggle(); | ||
$('body').append(toggleElement); | ||
}); | ||
|
||
afterEach(function() { | ||
toggleElement.remove(); | ||
}); | ||
|
||
describe('when starting', function() { | ||
it('hides content if unchecked', function() { | ||
toggle.start(toggleElement); | ||
|
||
expectToggleToBeHidden(); | ||
toggleCheckbox(); | ||
expectToggleToBeVisible(); | ||
}); | ||
|
||
it('shows content if checked', function() { | ||
toggleCheckbox(); | ||
toggle.start(toggleElement); | ||
|
||
expectToggleToBeVisible(); | ||
toggleCheckbox(); | ||
expectToggleToBeHidden(); | ||
}); | ||
|
||
it('adds an `aria-controls` attribute linking checkbox to target', function() { | ||
toggle.start(toggleElement); | ||
var ariaControls = toggleElement.find('input[type="checkbox"][data-target]').attr('aria-controls'); | ||
expect(ariaControls).toBe('target-id'); | ||
}); | ||
}); | ||
|
||
it('toggles content when checkbox with a target changes', function() { | ||
toggle.start(toggleElement); | ||
|
||
expectToggleToBeHidden(); | ||
toggleCheckbox(); | ||
expectToggleToBeVisible(); | ||
toggleCheckbox(); | ||
expectToggleToBeHidden(); | ||
}); | ||
|
||
it('does nothing when a checkbox without a target changes', function() { | ||
toggle.start(toggleElement); | ||
|
||
expectToggleToBeHidden(); | ||
toggleElement.find('.without-target').click(); | ||
expectToggleToBeHidden(); | ||
toggleElement.find('.without-target').click(); | ||
expectToggleToBeHidden(); | ||
}); | ||
|
||
it('can handle multiple checkboxes with toggles', function() { | ||
toggleElement.find('div').append('\ | ||
<label>\ | ||
<input type="checkbox" data-target="second-target-id" class="second-with-target"> Check me\ | ||
</label>\ | ||
<div id="second-target-id">Content</div>\ | ||
'); | ||
toggle.start(toggleElement); | ||
|
||
expectToggleToBeHidden(); | ||
expectToggleToBeHidden('#second-target-id'); | ||
toggleCheckbox(); | ||
expectToggleToBeVisible(); | ||
expectToggleToBeHidden('#second-target-id'); | ||
toggleCheckbox('.second-with-target'); | ||
expectToggleToBeVisible(); | ||
expectToggleToBeVisible('#second-target-id'); | ||
}); | ||
|
||
function toggleCheckbox(selector) { | ||
selector = selector || '.with-target'; | ||
toggleElement.find(selector).click(); | ||
} | ||
|
||
function expectToggleToBeVisible(id) { | ||
id = id || '#target-id'; | ||
|
||
expect(toggleElement.find(id).is(':visible')).toBe(true); | ||
expect(toggleElement.find(id).attr('aria-hidden')).toBe('false'); | ||
} | ||
|
||
function expectToggleToBeHidden(id) { | ||
id = id || '#target-id'; | ||
|
||
expect(toggleElement.find(id).is(':visible')).toBe(false); | ||
expect(toggleElement.find(id).attr('aria-hidden')).toBe('true'); | ||
} | ||
|
||
}); |
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,116 @@ | ||
describe('A radio toggle module', function() { | ||
"use strict"; | ||
|
||
var root = window, | ||
toggle, | ||
element; | ||
|
||
beforeEach(function() { | ||
element = $('<div>\ | ||
<label>\ | ||
<input type="radio" name="group" class="radio-1" data-target="radio-1-target"> Radio 1\ | ||
</label>\ | ||
<div id="radio-1-target"></div>\ | ||
<label>\ | ||
<input type="radio" name="group" class="radio-2" data-target="radio-2-target"> Radio 2\ | ||
</label>\ | ||
<div id="radio-2-target"></div>\ | ||
<label>\ | ||
<input type="radio" name="group" class="radio-3"> Radio 3\ | ||
</label>\ | ||
</div>'); | ||
|
||
toggle = new GOVUKAdmin.Modules.RadioToggle(); | ||
$('body').append(element); | ||
}); | ||
|
||
afterEach(function() { | ||
element.remove(); | ||
}); | ||
|
||
describe('when starting', function() { | ||
it('hides content if not selected', function() { | ||
toggle.start(element); | ||
expectToggleToBeHidden('#radio-1-target'); | ||
}); | ||
|
||
it('shows content if selected', function() { | ||
toggleRadio('.radio-1'); | ||
toggle.start(element); | ||
expectToggleToBeVisible('#radio-1-target'); | ||
}); | ||
|
||
it('adds an `aria-controls` attribute linking radio to each target', function() { | ||
toggle.start(element); | ||
var $ariaControls = element.find('input[type="radio"][data-target]'); | ||
expect($ariaControls.eq(0).attr('aria-controls')).toBe('radio-1-target'); | ||
expect($ariaControls.eq(1).attr('aria-controls')).toBe('radio-2-target'); | ||
}); | ||
}); | ||
|
||
it('toggles content when radio values change', function() { | ||
toggle.start(element); | ||
|
||
expectToggleToBeHidden('#radio-1-target'); | ||
expectToggleToBeHidden('#radio-2-target'); | ||
|
||
toggleRadio('.radio-1'); | ||
expectToggleToBeVisible('#radio-1-target'); | ||
expectToggleToBeHidden('#radio-2-target'); | ||
|
||
toggleRadio('.radio-2'); | ||
expectToggleToBeHidden('#radio-1-target'); | ||
expectToggleToBeVisible('#radio-2-target'); | ||
|
||
toggleRadio('.radio-3'); | ||
expectToggleToBeHidden('#radio-1-target'); | ||
expectToggleToBeHidden('#radio-2-target'); | ||
}); | ||
|
||
it('can handle multiple radio groups with toggles', function() { | ||
element.append('<div>\ | ||
<label>\ | ||
<input type="radio" name="another-group" class="radio-4" data-target="radio-4-target"> Radio 4\ | ||
</label>\ | ||
<div id="radio-4-target"></div>\ | ||
<label>\ | ||
<input type="radio" name="another-group" class="radio-5"> Radio 5\ | ||
</label>\ | ||
</div>'); | ||
|
||
toggle.start(element); | ||
|
||
expectToggleToBeHidden('#radio-1-target'); | ||
expectToggleToBeHidden('#radio-2-target'); | ||
expectToggleToBeHidden('#radio-4-target'); | ||
|
||
toggleRadio('.radio-1'); | ||
toggleRadio('.radio-4'); | ||
expectToggleToBeVisible('#radio-1-target'); | ||
expectToggleToBeVisible('#radio-4-target'); | ||
|
||
toggleRadio('.radio-2'); | ||
expectToggleToBeHidden('#radio-1-target'); | ||
expectToggleToBeVisible('#radio-2-target'); | ||
expectToggleToBeVisible('#radio-4-target'); | ||
|
||
toggleRadio('.radio-3'); | ||
expectToggleToBeHidden('#radio-1-target'); | ||
expectToggleToBeHidden('#radio-2-target'); | ||
expectToggleToBeVisible('#radio-4-target'); | ||
}); | ||
|
||
function toggleRadio(selector) { | ||
element.find(selector).click(); | ||
} | ||
|
||
function expectToggleToBeVisible(id) { | ||
expect(element.find(id).is(':visible')).toBe(true); | ||
expect(element.find(id).attr('aria-hidden')).toBe('false'); | ||
} | ||
|
||
function expectToggleToBeHidden(id) { | ||
expect(element.find(id).is(':visible')).toBe(false); | ||
expect(element.find(id).attr('aria-hidden')).toBe('true'); | ||
} | ||
}); |
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.
clever! 👍