Skip to content

Commit

Permalink
added support for selectTag to accept an array of selectedOption
Browse files Browse the repository at this point in the history
if select is in multiple mode, every selectedOption is selected
if select is single mode, only the first selectedOption is selected

added related tests
  • Loading branch information
Massimo Ronca committed Jan 22, 2014
1 parent e4b7739 commit e95eb44
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/template/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ exports.tags = {
, value
, textField = options.textField || 'text'
, valueField = options.valueField || 'value'
, attrs = {};
, attrs = {}
, selectedOptions = [].concat(selectedOption ? selectedOption : []) // turn selectOption into array if not already
, isMultiple = opts ? (opts.multiple ? true : false) : false;

delete options.textField;
delete options.valueField;
delete options.textField;
delete options.valueField;

for (var i = 0; i < optionsArray.length; i++) {
if (typeof optionsArray[i] == 'object') {
Expand All @@ -109,7 +111,12 @@ exports.tags = {
value = optionsArray[i];
}
attrs.value = value || attrs.value;
attrs.selected = selectedOption ? (attrs.value == selectedOption) : attrs.selected;

if(isMultiple) // if multiple select, allow selection of mutiple elements
attrs.selected = selectedOptions.length > 0 ? (selectedOptions.indexOf(attrs.value) > -1) : attrs.selected;
else // otherwise only the first get selected
attrs.selected = selectedOptions.length > 0 ? (selectedOptions[0] === attrs.value) : attrs.selected;

optionTags += this.contentTagString('option', text, attrs);
}

Expand Down
18 changes: 18 additions & 0 deletions test/templates/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ tests = {
assert.equal(string, "<select><option value=\"1\">Text 1</option><option selected=\"selected\" value=\"2\">Text 2</option></select>");
}

, 'test select tag with selected options in single mode': function() {
var choices = [{value: 1, text: "Text 1"}, {value: 2, text: "Text 2"}]
, string = helpers.selectTag(choices, [2,1]);
assert.equal(string, "<select><option value=\"1\">Text 1</option><option selected=\"selected\" value=\"2\">Text 2</option></select>");
}

, 'test select tag with selected options in multiple mode': function() {
var choices = [{value: 1, text: "Text 1"}, {value: 2, text: "Text 2"}]
, string = helpers.selectTag(choices, [2,1], { multiple: true });
assert.equal(string, "<select multiple=\"multiple\"><option selected=\"selected\" value=\"1\">Text 1</option><option selected=\"selected\" value=\"2\">Text 2</option></select>");
}

, 'test select tag with html options': function() {
var choices = [{value: 1, text: "Text 1"}, {value: 2, text: "Text 2"}]
, string = helpers.selectTag(choices, 2, {class: 'myclass'});
Expand Down Expand Up @@ -148,6 +160,12 @@ tests = {
assert.equal(string, "<select><option data-thing=\"avalue\" value=\"1\">Text 1</option><option data-thing=\"avalue\" selected=\"selected\" value=\"2\">Text 2</option></select>");
}

, 'test select tag in mutiple mode with text/attrs and selected option using a selected in attrs and an outer one too to make sure the outer takes precedence': function() {
var choices = [{value: 1, text: "Text 1", attrs: {data: {thing: "avalue"}, selected: true}}, {value: 2, text: "Text 2", attrs: {data: {thing: "avalue"}}}]
, string = helpers.selectTag(choices, [1,2], {multiple: true});
assert.equal(string, "<select multiple=\"multiple\"><option data-thing=\"avalue\" selected=\"selected\" value=\"1\">Text 1</option><option data-thing=\"avalue\" selected=\"selected\" value=\"2\">Text 2</option></select>");
}

, 'test single tags in truncateHTML': function () {
var string = helpers.truncateHTML('<p>Once upon a time in a world</p>', { length: 10 });
assert.equal(string, '<p>Once up...</p>');
Expand Down

0 comments on commit e95eb44

Please sign in to comment.