Skip to content

Commit

Permalink
added multipleRadio widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Caolan McMahon committed Jun 20, 2010
1 parent 64ae39c commit 844b628
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 252 deletions.
18 changes: 9 additions & 9 deletions lib/render.js
Expand Up @@ -23,16 +23,16 @@ var wrapWith = function(tag){
label = name[0].toUpperCase() + name.substr(1).replace('_', ' ');
}
var html = '<' + tag + ' class="' + field.classes().join(' ') + '">';
switch(field.widget.type){
case 'multipleCheckbox': html +=
'<fieldset>' +
'<legend>' + field.labelText(name) + '</legend>' +
field.errorHTML() +
field.widget.toHTML(name, field) +
'</fieldset>';
break;
default: html +=
if(field.widget.type == 'multipleCheckbox' ||
field.widget.type == 'multipleRadio'){
html += '<fieldset>' +
'<legend>' + field.labelText(name) + '</legend>' +
field.errorHTML() +
field.widget.toHTML(name, field) +
'</fieldset>';
}
else {
html += field.errorHTML() +
field.labelHTML(name) +
field.widget.toHTML(name, field);
}
Expand Down
38 changes: 38 additions & 0 deletions lib/widgets.js
Expand Up @@ -125,3 +125,41 @@ exports.multipleCheckbox = function(opt){
};
return w;
};

exports.multipleRadio = function(opt){
var opt = opt || {};
var w = {};
w.classes = opt.classes || [];
w.type = 'multipleRadio';
w.toHTML = function(name, f){
var f = f || {};
return Object.keys(f.choices).reduce(function(html, k){
// input element
html += '<input type="radio"';
html += ' name="' + name + '"';

var id = f.id ? f.id + '_' + k: 'id_' + name + '_' + k;
html += ' id="' + id + '"';

if(w.classes.length)
html += ' class="' + w.classes.join(' ') + '"';

html += ' value="' + k + '"';

if(f.value instanceof Array){
if(f.value.some(function(v){return v == k;})){
html += ' checked="checked"';
}
}
else html += (f.value == k) ? ' checked="checked"': '';

html += '>';

// label element
html += '<label for="' + id + '">' + f.choices[k] + '</label>';

return html;
}, '');
};
return w;
};

0 comments on commit 844b628

Please sign in to comment.