Skip to content

Commit 82f44a5

Browse files
author
Mark
committed
Updated Checkbox getter and setters to change value instead of check state, onChange now passes checked boolean value
1 parent 15cf2c5 commit 82f44a5

File tree

2 files changed

+71
-31
lines changed

2 files changed

+71
-31
lines changed

src/checkbox.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class Checkbox extends FormElement {
179179
this.options.onChecked(value, input, container);
180180
}
181181
if (this.options.onChange) {
182-
this.options.onChange(value, input, container);
182+
this.options.onChange(true, input, container);
183183
}
184184
}
185185

@@ -193,12 +193,11 @@ class Checkbox extends FormElement {
193193
input.checked = false;
194194
}
195195
container.classList.remove(this.options.checkedClass);
196-
let value = this.getValue();
197196
if (this.options.onUnchecked) {
198-
this.options.onUnchecked(value, input, container);
197+
this.options.onUnchecked('', input, container);
199198
}
200199
if (this.options.onChange) {
201-
this.options.onChange(value, input, container);
200+
this.options.onChange(false, input, container);
202201
}
203202
}
204203

@@ -251,20 +250,23 @@ class Checkbox extends FormElement {
251250

252251
/**
253252
* Returns whether the checkbox is checked or not
254-
* @returns {boolean} Returns a truthy value if checkbox is checked, falsy if not
253+
* @returns {string} Returns the checkbox value attribute
255254
*/
256255
getValue () {
257-
return this.getFormElement().checked;
256+
let formEl = this.getFormElement();
257+
if (formEl.checked) {
258+
return formEl.value;
259+
} else {
260+
return '';
261+
}
258262
}
259263

260264
/**
261-
* Checks the checkbox if a truthy value is passed.
262-
* @param {string|boolean} value
265+
* Sets the checkbox value attribute.
266+
* @param {string} value
263267
*/
264268
setValue (value) {
265-
// check it if the value is truthy
266-
value = value ? true : false;
267-
this.getFormElement().checked = value;
269+
this.getFormElement().value = value;
268270
}
269271

270272
/**

tests/checkbox-tests.js

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,37 @@ module.exports = (function () {
5959
QUnit.ok(input.checked, 'input checked boolean returns true because that\'s how it was initially');
6060
});
6161

62-
QUnit.test('checking/unchecking callbacks', function() {
63-
QUnit.expect(4);
62+
QUnit.test('should trigger onChecked callback option with correct args when checked', function() {
63+
QUnit.expect(2);
6464
var fixture = document.getElementById('qunit-fixture');
6565
var container = TestUtils.createHtmlElement(html);
6666
fixture.appendChild(container);
6767
var input = container.getElementsByClassName('ui-checkbox-input')[0];
6868
var onCheckedSpy = Sinon.spy();
69-
var onUncheckedSpy = Sinon.spy();
70-
var checkbox = new Checkbox({el: input, onChecked: onCheckedSpy, onUnchecked: onUncheckedSpy});
69+
var checkbox = new Checkbox({el: input, onChecked: onCheckedSpy});
7170
var UICheckbox = container.getElementsByClassName('ui-checkbox')[0];
7271
checkbox.check();
7372
QUnit.deepEqual(onCheckedSpy.args[0], ['NY', input, UICheckbox], 'on check(), onChecked callback was fired with correct args');
74-
QUnit.equal(onUncheckedSpy.callCount, 0, 'onUnchecked callback was NOT fired yet');
7573
checkbox.uncheck();
76-
QUnit.deepEqual(onUncheckedSpy.args[0], ['NY', input, UICheckbox], 'on uncheck(), onUnchecked callback was fired with correct args');
7774
QUnit.equal(onCheckedSpy.callCount, 1, 'onChecked callback was NOT fired');
7875
checkbox.destroy();
7976
});
8077

78+
QUnit.test('should trigger onUnchecked callback option with empty string and correct args when unchecked', function() {
79+
QUnit.expect(1);
80+
var fixture = document.getElementById('qunit-fixture');
81+
var container = TestUtils.createHtmlElement(html);
82+
fixture.appendChild(container);
83+
var input = container.getElementsByClassName('ui-checkbox-input')[0];
84+
var onUncheckedSpy = Sinon.spy();
85+
var checkbox = new Checkbox({el: input, onUnchecked: onUncheckedSpy});
86+
var UICheckbox = container.getElementsByClassName('ui-checkbox')[0];
87+
checkbox.check();
88+
checkbox.uncheck();
89+
QUnit.deepEqual(onUncheckedSpy.args[0], ['', input, UICheckbox], 'on uncheck(), onUnchecked callback was fired with correct args');
90+
checkbox.destroy();
91+
});
92+
8193
//QUnit.test('clicking on and off ui element', function () {
8294
// QUnit.expect(12);
8395
// var fixture = document.getElementById('qunit-fixture');
@@ -201,32 +213,39 @@ module.exports = (function () {
201213
setAttrSpy.restore();
202214
});
203215

204-
QUnit.test('passing truthy value to setValue() should check the checkbox, and passing a falsy value should uncheck it', function() {
205-
QUnit.expect(3);
216+
QUnit.test('should set the value passed to setValue() onto the checkbox form element', function() {
217+
QUnit.expect(1);
206218
var fixture = document.getElementById('qunit-fixture');
207219
var container = TestUtils.createHtmlElement(html);
208220
fixture.appendChild(container);
209221
var checkboxEl = container.getElementsByClassName('ui-checkbox-input')[0];
210222
var checkbox = new Checkbox({el: checkboxEl});
211-
QUnit.ok(!checkboxEl.checked, 'not checked by default');
212-
checkbox.setValue('true');
213-
QUnit.equal(checkboxEl.checked, true);
214-
checkbox.setValue(null);
215-
QUnit.equal(checkboxEl.checked, false);
223+
var testVal = 'blah';
224+
checkbox.setValue(testVal);
225+
QUnit.equal(checkboxEl.value, testVal);
216226
checkbox.destroy();
217227
});
218228

219-
QUnit.test('getValue() should return true when checkbox is checked and false when not', function() {
220-
QUnit.expect(2);
229+
QUnit.test('getValue() should return value of the checkbox when checked', function() {
230+
QUnit.expect(1);
231+
var fixture = document.getElementById('qunit-fixture');
232+
var container = TestUtils.createHtmlElement(html);
233+
fixture.appendChild(container);
234+
var checkbox = new Checkbox({el: container.getElementsByClassName('ui-checkbox-input')[0]});
235+
checkbox.check();
236+
QUnit.equal(checkbox.getValue(), 'NY');
237+
checkbox.destroy();
238+
});
239+
240+
QUnit.test('getValue() should return empty string when unchecked', function() {
241+
QUnit.expect(1);
221242
var fixture = document.getElementById('qunit-fixture');
222243
var container = TestUtils.createHtmlElement(html);
223244
fixture.appendChild(container);
224245
var checkbox = new Checkbox({el: container.getElementsByClassName('ui-checkbox-input')[0]});
225-
// check checkbox
226246
checkbox.check();
227-
QUnit.equal(checkbox.getValue(), true);
228247
checkbox.uncheck();
229-
QUnit.equal(checkbox.getValue(), false);
248+
QUnit.equal(checkbox.getValue(), '');
230249
checkbox.destroy();
231250
});
232251

@@ -264,7 +283,7 @@ module.exports = (function () {
264283
});
265284

266285
QUnit.test('clicking on ui element should trigger onChange callback option with correct args', function() {
267-
QUnit.expect(2);
286+
QUnit.expect(3);
268287
var fixture = document.getElementById('qunit-fixture');
269288
var container = TestUtils.createHtmlElement(html);
270289
fixture.appendChild(container);
@@ -274,7 +293,26 @@ module.exports = (function () {
274293
var UICheckbox = container.getElementsByClassName('ui-checkbox')[0];
275294
QUnit.equal(onChangeSpy.callCount, 0);
276295
UICheckbox.click();
277-
QUnit.deepEqual(onChangeSpy.args[0], ['NY', input, UICheckbox]);
296+
QUnit.deepEqual(onChangeSpy.args[0], [true, input, UICheckbox], 'onChange was called with truth value when clicked to check');
297+
UICheckbox.click();
298+
QUnit.deepEqual(onChangeSpy.args[1], [false, input, UICheckbox], 'onChange was called with false value when clicked to uncheck');
299+
checkbox.destroy();
300+
});
301+
302+
QUnit.test('clicking on input checkbox element should trigger onChange callback option with correct args', function() {
303+
QUnit.expect(3);
304+
var fixture = document.getElementById('qunit-fixture');
305+
var container = TestUtils.createHtmlElement(html);
306+
fixture.appendChild(container);
307+
var input = container.getElementsByClassName('ui-checkbox-input')[0];
308+
var onChangeSpy = Sinon.spy();
309+
var checkbox = new Checkbox({el: input, onChange: onChangeSpy});
310+
var UICheckbox = container.getElementsByClassName('ui-checkbox')[0];
311+
QUnit.equal(onChangeSpy.callCount, 0);
312+
input.click();
313+
QUnit.deepEqual(onChangeSpy.args[0], [true, input, UICheckbox], 'onChange was called with truth value when clicked to check');
314+
input.click();
315+
QUnit.deepEqual(onChangeSpy.args[1], [false, input, UICheckbox], 'onChange was called with false value when clicked to uncheck');
278316
checkbox.destroy();
279317
});
280318

0 commit comments

Comments
 (0)