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

Mark selection buttons with a class that matches their input #317

Merged
merged 1 commit into from
Sep 1, 2016
Merged
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
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