Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic widget attributes #80

Merged
merged 1 commit into from
Sep 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"/;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi "no" is an invalid value for this attribute, the only valid options are "on" or "off" - the test is fine tho :-)

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();
};