Skip to content

Commit

Permalink
Merge pull request #80 from path/dynamic-widget-attributes
Browse files Browse the repository at this point in the history
Add support for dynamic widget attributes
  • Loading branch information
ljharb committed Sep 9, 2013
2 parents e6951d4 + 6567b5d commit 5212044
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
31 changes: 15 additions & 16 deletions lib/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var input = function (type) {
id: f.id || true,
classes: w.classes,
value: w.formatValue(f.value)
}, userAttrs]);
}, userAttrs, w.attrs || {}]);
};
w.getDataRegExp = function () {
return dataRegExp;
Expand Down Expand Up @@ -129,14 +129,14 @@ exports.checkbox = function (opt) {
};
w.toHTML = function (name, f) {
if (!f) { f = {}; }
return singleTag('input', {
return singleTag('input', [{
type: 'checkbox',
name: name,
id: f.id || true,
classes: w.classes,
checked: !!f.value,
value: 'on'
});
}, w.attrs || {}]);
};
return w;
};
Expand All @@ -155,11 +155,11 @@ exports.select = function (opt) {
selected: !!(f.value && f.value === k)
}, f.choices[k]);
}, '');
return tag('select', {
return tag('select', [{
name: name,
id: f.id || true,
classes: w.classes
}, optionsHTML);
}, w.attrs || {}], optionsHTML);
};
return w;
};
Expand All @@ -172,13 +172,13 @@ exports.textarea = function (opt) {
};
w.toHTML = function (name, f) {
if (!f) { f = {}; }
return tag('textarea', {
return tag('textarea', [{
name: name,
id: f.id || true,
classes: w.classes,
rows: opt.rows || null,
cols: opt.cols || null
}, f.value || '');
}, w.attrs || {}], f.value || '');
};
return w;
};
Expand All @@ -196,14 +196,14 @@ exports.multipleCheckbox = function (opt) {
var id = f.id ? f.id + '_' + k : 'id_' + name + '_' + k,
checked = Array.isArray(f.value) ? f.value.some(function (v) { return v === k; }) : f.value === k;

html += singleTag('input', {
html += singleTag('input', [{
type: 'checkbox',
name: name,
id: id,
classes: w.classes,
value: k,
checked: !!checked
});
}, w.attrs || {}]);

// label element
html += tag('label', {'for': id}, f.choices[k]);
Expand All @@ -220,11 +220,10 @@ exports.label = function (opt) {
classes: opt.classes || []
};
w.toHTML = function (forID, f) {
var labelAttrs = {
return tag('label', [{
for: forID,
classes: w.classes
};
return tag('label', labelAttrs, opt.content);
}, w.attrs || {}], opt.content);
};
return w;
};
Expand All @@ -242,14 +241,14 @@ exports.multipleRadio = function (opt) {
var id = f.id ? f.id + '_' + k : 'id_' + name + '_' + k,
checked = Array.isArray(f.value) ? f.value.some(function (v) { return v === k; }) : f.value === k;

html += singleTag('input', {
html += singleTag('input', [{
type: 'radio',
name: name,
id: id,
classes: w.classes,
value: k,
checked: !!checked
});
}, w.attrs || {}]);
// label element
html += tag('label', {for: id}, f.choices[k]);

Expand All @@ -274,12 +273,12 @@ exports.multipleSelect = function (opt) {
selected: !!selected
}, f.choices[k]);
}, '');
return tag('select', {
return tag('select', [{
multiple: true,
name: name,
id: f.id || true,
classes: w.classes
}, optionsHTML);
}, w.attrs || {}], optionsHTML);
};
return w;
};
Expand Down
10 changes: 10 additions & 0 deletions test/test-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,13 @@ exports.label = function (test) {
test.done();
};

exports['dynamic widget attributes'] = function(test) {
var re = /autocomplete="no"/;
Object.keys(forms.widgets).forEach(function(name) {
var w = forms.widgets[name]();
w.attrs = {autocomplete: 'no'};
var html = w.toHTML('test', {choices: {foo: 'bar'}});
test.equals(re.test(html), true);
});
test.done();
};

0 comments on commit 5212044

Please sign in to comment.