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

Allow disabled choices #197

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
30 changes: 19 additions & 11 deletions lib/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ var renderChoices = function (choices, renderer) {
return reduce(choices, function (partialRendered, choice) {
var isNested = is.array(choice[1]);
var renderData = isNested ?
{ isNested: true, label: choice[0], choices: choice[1] } :
{ isNested: false, value: choice[0], label: choice[1] };
{ isNested: true, label: choice[0], choices: choice[1], disabled: choice[2], default: choice[3] } :
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is "default"? let's keep this PR just to adding "disabled".

{ isNested: false, value: choice[0], label: choice[1], disabled: choice[2], default: choice[3] };
return partialRendered + renderer(renderData);
}, '');
};
Expand All @@ -74,15 +74,23 @@ var unifyChoices = function (choices, nestingLevel) {

var unifyChoiceArray = function (arrayChoices, currentLevel) {
return reduce(arrayChoices, function (result, choice) {
if (!is.array(choice) || choice.length !== 2) {
throw new TypeError('choice must be array with two elements');
if (!is.array(choice) || choice.length > 4 || choice.length < 2) {
throw new TypeError('choice must be array with 2-4 elements');
}
if (isScalar(choice[0]) && isScalar(choice[1])) {
result.push(choice);
} else if (isScalar(choice[0]) && (is.array(choice[1]) || is.object(choice[1]))) {
result.push([choice[0], unifyChoices(choice[1], currentLevel - 1)]);
} else {
throw new TypeError('expected primitive value as first and primitive value, object, or array as second element');
if(choice.length == 2) {
if (isScalar(choice[0]) && isScalar(choice[1])) {
result.push(choice);
} else if (isScalar(choice[0]) && (is.array(choice[1]) || is.object(choice[1]))) {
result.push([choice[0], unifyChoices(choice[1], currentLevel - 1)]);
} else {
throw new TypeError('expected primitive value as first and primitive value, object, or array as second element');
}
}else if (choice.length === 3 || choice.length === 4) {
if (isScalar(choice[0]) && isScalar(choice[1]) && isScalar(choice[2]) && isScalar(choice[3])) {
result.push(choice);
} else {
throw new TypeError('expected primitive values');
}
}
return result;
}, []);
Expand Down Expand Up @@ -119,7 +127,7 @@ var select = function (isMultiple) {
if (choice.isNested) {
return tag('optgroup', { label: choice.label }, renderChoices(choice.choices, render), true);
} else {
return tag('option', { value: choice.value, selected: !!isSelected(f.value, choice.value) }, choice.label);
return tag('option', { value: choice.value, selected: !!isSelected(f.value, choice.value), disabled: choice.disabled }, choice.label);
}
});
var attrs = {
Expand Down
2 changes: 1 addition & 1 deletion test/test-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ test('select', function (t) {
st['throws'](function () {
forms.widgets.select().toHTML('name', {
choices: [
['val1', 'text1', 'invalid']
['val1', 'text1', false, false, 'invalid']
]
});
});
Expand Down