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

Optgroup feature #168

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
57 changes: 44 additions & 13 deletions lib/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,29 @@ exports.select = function (options) {
classes: opt.classes,
type: 'select'
};
var choicesHTML;
var groupHTML = function (group, label, value) {
return tag('optgroup', {
label: label
}, choicesHTML(group, value));
};
choicesHTML = function (choices, value) {
return Object.keys(choices).reduce(function (html, k) {
if (is.object(choices[k])) {
return html + groupHTML(choices[k], String(k), value);
} else {
return html + tag('option', {
value: k,
selected: !!(value && String(value) === String(k))
}, choices[k]);
}
}, '');
};

var userAttrs = getUserAttrs(opt);
w.toHTML = function (name, field) {
var f = field || {};
var optionsHTML = Object.keys(f.choices).reduce(function (html, k) {
return html + tag('option', {
value: k,
selected: !!(f.value && String(f.value) === String(k))
}, f.choices[k]);
}, '');
var optionsHTML = choicesHTML(f.choices, f.value, '');
var attrs = {
name: name,
id: f.id === false ? false : (f.id || true),
Expand Down Expand Up @@ -233,16 +247,33 @@ exports.multipleSelect = function (options) {
classes: opt.classes,
type: 'multipleSelect'
};
var choicesHTML;
var groupHTML = function (group, label, value) {
return tag('optgroup', {
label: label
}, choicesHTML(group, value));
};

choicesHTML = function (choices, value) {
return Object.keys(choices).reduce(function (html, k) {
if (is.object(choices[k])) {
return html + groupHTML(choices[k], String(k), value);
} else {
var selected = value && (Array.isArray(value) ? value.some(function (v) {
return String(v) === String(k);
}) : String(value) === String(k));
return html + tag('option', {
value: k,
selected: !!selected
}, choices[k]);
}
}, '');
};

var userAttrs = getUserAttrs(opt);
w.toHTML = function (name, field) {
var f = field || {};
var optionsHTML = Object.keys(f.choices).reduce(function (html, k) {
var selected = f.value && (Array.isArray(f.value) ? f.value.some(function (v) { return String(v) === String(k); }) : String(f.value) === String(k));
return html + tag('option', {
value: k,
selected: !!selected
}, f.choices[k]);
}, '');
var optionsHTML = choicesHTML(f.choices, f.value);
var attrs = {
multiple: true,
name: name,
Expand Down
64 changes: 62 additions & 2 deletions test/test-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,37 @@ test('select', function (t) {
var expectedHTML = '<select name="name" id="someid" class="one two">' +
'<option value="1">one</option>' +
'<option value="2" selected="selected">two</option>' +
'</select>';
'</select>';
st.equal(html, expectedHTML);
st.end();
});
t.test('provides option groups', function (st) {
var html = widget.toHTML('name', {
choices: {
regular: 'value',
group1: {
1: 'one',
2: 'two'
},
'Group Two': {
3: 'three',
4: 'four'
}
},
id: 'someid',
value: '3'
});
var expectedHTML = '<select name="name" id="someid" class="one two">' +
'<option value="regular">value</option>' +
'<optgroup label="group1">' +
'<option value="1">one</option>' +
'<option value="2">two</option>' +
'</optgroup>' +
'<optgroup label="Group Two">' +
'<option value="3" selected="selected">three</option>' +
'<option value="4">four</option>' +
'</optgroup>' +
'</select>';
st.equal(html, expectedHTML);
st.end();
});
Expand Down Expand Up @@ -405,7 +435,37 @@ test('multipleSelect', function (t) {

st.end();
});

t.test('provides option groups', function (st) {
var widget = forms.widgets.multipleSelect({ classes: ['one', 'two'] });
var html = widget.toHTML('name', {
choices: {
'Long groupname': {
1: 'one',
2: 'two'
},
regular: 'value',
Hamster: {
3: 'dance',
4: 'dance!!!'
}
},
id: 'someid',
value: ['2', 'regular']
});
var expectedHTML = '<select multiple="multiple" name="name" id="someid" class="one two">' +
'<optgroup label="Long groupname">' +
'<option value="1">one</option>' +
'<option value="2" selected="selected">two</option>' +
'</optgroup>' +
'<option value="regular" selected="selected">value</option>' +
'<optgroup label="Hamster">' +
'<option value="3">dance</option>' +
'<option value="4">dance!!!</option>' +
'</optgroup>' +
'</select>';
st.equal(html, expectedHTML);
st.end();
});
t.end();
});

Expand Down