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
Changes from 1 commit
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
38 changes: 26 additions & 12 deletions lib/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ var isSelected = function (value, choice) {
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] };
if (choice.length == 3) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

i'm not sure we need this branching - we can just set disabled to false unless a third truthy value is provided.

var renderData = isNested ?
{ isNested: true, label: choice[0], choices: choice[1], disabled: choice[2] } :
{ isNested: false, value: choice[0], label: choice[1], disabled: choice[2] };
}else{
var renderData = isNested ?
{ isNested: true, label: choice[0], choices: choice[1] } :
{ isNested: false, value: choice[0], label: choice[1] };
}
return partialRendered + renderer(renderData);
}, '');
};
Expand All @@ -74,15 +80,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 > 3 || choice.length < 2) {
throw new TypeError('choice must be array with 2-3 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) {
if (isScalar(choice[0]) && isScalar(choice[1]) && isScalar(choice[2])) {
result.push(choice);
} else {
throw new TypeError('expected primitive values');
}
}
return result;
}, []);
Expand Down Expand Up @@ -119,7 +133,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