Methods
(inner) selectOptions(value, domopt, selectedopt) → {Array.<Object>|Array.<HTMLElement>}
- Description:
Form controls in this library uses the selectOptions function. This converts different kind of lists into a uniform format that can easily be mapped into a dom. Is the
domparametertrue, the function returns a ready mapped domoptionlist.
Thelistparameter accepts four notations:- a komma separated list: 'TextValue1,TextValue2' - a array of values: ['TextValue1', 'TextValue2'...] - a array of display / value pairs: [['Text1', Value1], ['Text2', Value2]...] - a array of objects: [{value: 1, ['children' || 'text' || 'displayValue']}]
- Source:
Examples
Notations can be mixed inside a array.
For example `[ '', ['text 1', 1], ['text 2', 2] ]` will be converted to:
[
{
value: '',
children: '',
},
{
value: 1,
children: 'text 1',
},
{
value: 2,
children: 'text 2',
}
]
and with `dom=true` and `selected: "1"`:
[
option({value=""}),
option({value="1", selected: true}, "text 1"),
option({value="2"}, "text 2"),
]
an other example:
import {selectOptions} from 'lib';
..
return input({type: 'select', value: fontName}, selectOptions(fontNames, true, fontName))
selectOptions('red,green,blue', false)
// => [{ value: 'red', children: 'red' }, ...]
selectOptions([['Text 1', 1], ['Text 2', 2]], true, 1)
// => [option({ value: '1', selected: true }, 'Text 1'), ...]
Merge and Translate
const translate = txt => (txt==='None,Left,Right') ? 'Nix,Links,Rechts' : txt;
input:
{
value: ',1,2', // required, array acepted
text: translate('None,Left,Right'), // should, array acepted
}
output:
[{
value: '',
children: 'Nix',
},{
value: 1,
children: 'Links',
}]
},{
value: 2,
children: 'Rechts',
}]
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
value |
SelectOption | input list in one of the supported formats |
||
dom |
boolean |
<optional> |
false
|
when true, return a DOM-ready option list instead of plain objects |
selected |
string | number |
<optional> |
selected value used when |
Returns:
either normalized option objects or mapped <option> elements
- Type
- Array.<Object> | Array.<HTMLElement>
Type Definitions
SelectOption
- Description:
Input type for selectOptions
- a komma separated list: 'TextValue1,TextValue2' - a array of values: ['TextValue1', 'TextValue2'...] - a array of display / value pairs: [['Text1', Value1], ['Text2', Value2]...] - a array of objects: [{value: 1, ['children' || 'text' || 'displayValue']}]
- Source:
Input type for selectOptions
- a komma separated list: 'TextValue1,TextValue2'
- a array of values: ['TextValue1', 'TextValue2'...]
- a array of display / value pairs: [['Text1', Value1], ['Text2', Value2]...]
- a array of objects: [{value: 1, ['children' || 'text' || 'displayValue']}]
Type:
- string | Array.<string> | Array.<Array.<(string|number)>> | Array.<Object> | Object