diff --git a/lib/widgets.js b/lib/widgets.js index 2e79fe9..ed59a2d 100644 --- a/lib/widgets.js +++ b/lib/widgets.js @@ -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] } : + { isNested: false, value: choice[0], label: choice[1], disabled: choice[2], default: choice[3] }; return partialRendered + renderer(renderData); }, ''); }; @@ -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; }, []); @@ -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 = { diff --git a/test/test-widgets.js b/test/test-widgets.js index 0f9021f..060f8c1 100644 --- a/test/test-widgets.js +++ b/test/test-widgets.js @@ -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'] ] }); });