Commit e4984da4 authored by Rafael Ostertag's avatar Rafael Ostertag
Browse files

Make Checkbox.js work with multiple checkboxes per

parent f32e0dcd
......@@ -19,20 +19,38 @@ QfqNS.Element = QfqNS.Element || {};
function Checkbox($element) {
n.FormGroup.call(this, $element);
if (!this.isType("checkbox")) {
var type = "checkbox";
if (!this.isType(type)) {
throw new Error("$element is not of type 'checkbox'");
}
// We allow one Form Group to have several checkboxes. Therefore, we have to remember which checkbox was
// selected if possible.
if ($element.length === 1 && $element.attr('type') === type) {
this.$singleElement = $element;
} else {
this.$singleElement = null;
}
}
Checkbox.prototype = Object.create(n.FormGroup.prototype);
Checkbox.prototype.constructor = Checkbox;
Checkbox.prototype.setValue = function (val) {
this.$element.prop('checked', val);
if (this.$singleElement) {
this.$singleElement.prop('checked', val);
} else {
this.$element.prop('checked', val);
}
};
Checkbox.prototype.getValue = function () {
return this.$element.prop('checked');
if (this.$singleElement) {
return this.$singleElement.prop('checked');
} else {
return this.$element.prop('checked');
}
};
n.Checkbox = Checkbox;
......
......@@ -172,6 +172,38 @@
</div>
</div>
<div class="form-group">
<div class="col-md-2">
<b class="control-label">
Checkbox 3 test
</b>
</div>
<div class="col-md-6">
<div class="checkbox">
<label>
<input name='checkbox3_1' type="checkbox" value="reminder_value">
</label>
</div>
<div class="checkbox">
<label>
<input name='checkbox3_2' type="checkbox" value="reminder_value">
</label>
</div>
<div class="checkbox">
<label>
<input name='checkbox3_3' type="checkbox" value="reminder_value">
</label>
</div>
</div>
<div class="col-md-4">
</div>
</div>
</form>
</div>
......
......@@ -316,6 +316,39 @@
</div>
</div>
<div class="form-group">
<div class="col-md-2">
<b class="control-label">
Checkbox 3 test
</b>
</div>
<div class="col-md-6">
<div class="checkbox">
<label>
<input name='checkbox3_1' type="checkbox" value="reminder_value">
</label>
</div>
<div class="checkbox">
<label>
<input name='checkbox3_2' type="checkbox" value="reminder_value">
</label>
</div>
<div class="checkbox">
<label>
<input name='checkbox3_3' type="checkbox" value="reminder_value">
</label>
</div>
</div>
<div class="col-md-4">
</div>
</div>
</form>
</section>
......
......@@ -290,8 +290,40 @@
</div>
</div>
</form>
<div class="form-group">
<div class="col-md-2">
<b class="control-label">
Checkbox 3 test
</b>
</div>
<div class="col-md-6">
<div class="checkbox">
<label>
<input name='checkbox3_1' type="checkbox" value="reminder_value">
</label>
</div>
<div class="checkbox">
<label>
<input name='checkbox3_2' type="checkbox" value="reminder_value">
</label>
</div>
<div class="checkbox">
<label>
<input name='checkbox3_3' type="checkbox" value="reminder_value">
</label>
</div>
</div>
<div class="col-md-4">
</div>
</div>
</form>
</section>
<% with (scripts) { %>
<% [].concat(polyfills, jasmine, boot, vendor, helpers, src, specs,reporters).forEach(function(script){ %>
......
......@@ -47,4 +47,31 @@ describe("Element Checkbox", function () {
expect($reminderCheckbox.prop('checked')).toBe(true);
expect($checkbox2.prop('checked')).toBe(false);
});
it('should properly handle several checkboxes in one Form Group', function () {
var $checkbox3_1 = $("[name='checkbox3_1']");
var $checkbox3_2 = $("[name='checkbox3_2']");
var $checkbox3_3 = $("[name='checkbox3_3']");
var checkbox3_1 = new QfqNS.Element.Checkbox($checkbox3_1);
checkbox3_1.setValue(true);
expect(checkbox3_1.getValue()).toBe(true);
expect($checkbox3_1.prop('checked')).toBe(true);
expect($checkbox3_2.prop('checked')).toBe(false);
expect($checkbox3_3.prop('checked')).toBe(false);
$('#myForm')[0].reset();
var checkbox3_2 = new QfqNS.Element.Checkbox($checkbox3_2);
checkbox3_2.setValue(true);
expect(checkbox3_2.getValue()).toBe(true);
expect($checkbox3_2.prop('checked')).toBe(true);
expect($checkbox3_1.prop('checked')).toBe(false);
expect($checkbox3_3.prop('checked')).toBe(false);
});
});
\ No newline at end of file
......@@ -97,7 +97,7 @@ describe("Element FormGroup", function () {
expect(personGender.hasHelpBlock()).toBe(true);
});
it("should properly disable the item", function () {
xit("should properly disable the item", function () {
personGender.setEnabled(false);
});
});
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment