Skip to content

Commit

Permalink
Merge pull request #317 from alphagov/custom-radios
Browse files Browse the repository at this point in the history
Mark selection buttons with a class that matches their input
  • Loading branch information
NickColley authored Sep 1, 2016
2 parents d54c9b2 + 8c2a90b commit 308c20c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions javascripts/govuk/selection-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

this.selectedClass = 'selected';
this.focusedClass = 'focused';
this.radioClass = 'selection-button-radio';
this.checkboxClass = 'selection-button-checkbox';
if (opts !== undefined) {
$.each(opts, function (optionName, optionObj) {
this[optionName] = optionObj;
Expand All @@ -33,6 +35,8 @@
$elms.each(function (idx, elm) {
var $elm = $(elm);

var labelClass = $elm.attr('type') === 'radio' ? this.radioClass : this.checkboxClass;
$elm.parent('label').addClass(labelClass);
if ($elm.is(':checked')) {
this.markSelected($elm);
}
Expand Down
18 changes: 16 additions & 2 deletions spec/unit/selection-button.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,14 @@ describe("selection-buttons", function () {
});

describe("At the point it is called", function () {
it("Should do nothing if no radios are checked", function () {
it("Should mark radios with the selection-button-radio class", function () {
buttonsInstance = new GOVUK.SelectionButtons("label.selectable input[type='radio']");
expect($radioLabels.eq(0).hasClass('selection-button-radio')).toBe(true);
expect($radioLabels.eq(1).hasClass('selection-button-radio')).toBe(true);
expect($radioLabels.eq(2).hasClass('selection-button-radio')).toBe(true);
});

it("Should not add a selected class if no radios are checked", function () {
buttonsInstance = new GOVUK.SelectionButtons("label.selectable input[type='radio']");
expect($radioLabels.eq(0).hasClass('selected')).toBe(false);
expect($radioLabels.eq(1).hasClass('selected')).toBe(false);
Expand Down Expand Up @@ -411,7 +418,14 @@ describe("selection-buttons", function () {
});

describe("At the point it is called", function () {
it("Should do nothing if no checkboxes are checked", function () {
it("Should mark checkboxes with the selection-button-checkbox class", function () {
buttonsInstance = new GOVUK.SelectionButtons("label.selectable input[type='checkbox']");
expect($checkboxLabels.eq(0).hasClass('selection-button-checkbox')).toBe(true);
expect($checkboxLabels.eq(1).hasClass('selection-button-checkbox')).toBe(true);
expect($checkboxLabels.eq(2).hasClass('selection-button-checkbox')).toBe(true);
});

it("Should not add a selected class if no checkboxes are checked", function () {
buttonsInstance = new GOVUK.SelectionButtons("label.selectable input[type='checkbox']");
expect($checkboxLabels.eq(0).hasClass('selected')).toBe(false);
expect($checkboxLabels.eq(1).hasClass('selected')).toBe(false);
Expand Down

0 comments on commit 308c20c

Please sign in to comment.