Skip to content

Commit

Permalink
Implementation of checkbox groups.
Browse files Browse the repository at this point in the history
The 'checked'/'attr'-data-query with 'checked' as attribute will accept
arrays/array-observables as the 'value' parameter
and try to find their value in the array to set the value
and add their value to the array when checked.
  • Loading branch information
Joscha Rohmann committed Jan 7, 2016
1 parent acf58c9 commit 547d934
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/query/DomQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,16 @@ define([
var tagName = element.tagName.toLowerCase();
var type = element.getAttribute('type');

if (type == 'checkbox') {
if (blocks.isArray(value()) && (type == 'checkbox' || type == 'radio')) {
var unwrapedValue = value();
if (element.checked) {
if (unwrapedValue.indexOf(element.value) == -1) {
value.add(element.value);
}
} else if (unwrapedValue.indexOf(element.value) !== -1) {
value.splice(unwrapedValue.indexOf(element.value), 1);
}
} else if (type == 'checkbox') {
value(element.checked);
} else if (tagName == 'select' && element.getAttribute('multiple') !== null) {
var values = [];
Expand Down
8 changes: 6 additions & 2 deletions src/query/VirtualElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,15 @@ define([
if (attributeName == 'checked' && attributeValue != null && !this._fake) {
if (this._attributes.type == 'radio' &&
typeof attributeValue == 'string' &&
value != attributeValue && value != null) {
value != attributeValue && value != null && !blocks.isArray(attributeValue)) {

attributeValue = null;
} else {
attributeValue = attributeValue ? 'checked' : null;
if (blocks.isArray(attributeValue)) {
attributeValue = attributeValue.indexOf(value) !== -1 ? 'checked' : null;
} else {
attributeValue = attributeValue ? 'checked' : null;
}
}
} else if (attributeName == 'disabled') {
attributeValue = attributeValue ? 'disabled' : null;
Expand Down
6 changes: 5 additions & 1 deletion src/query/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ define([

attributeValue = false;
} else {
attributeValue = !!attributeValue;
if (blocks.isArray(attributeValue)) {
attributeValue = attributeValue.indexOf(element.value) !== -1;
} else {
attributeValue = !!attributeValue;
}
}
}

Expand Down

0 comments on commit 547d934

Please sign in to comment.