option-html v0.3.0
option-html
Generate the <option>s html string.
Installation
npm install option-htmlUsage
It is easy to use the option-html.
import optionHtml from 'option-html';
const html = optionHtml(settings); // The `settings` is a configurable objectExamples
An basic example:
const html = optionHtml({
options: [
'Java',
'JavaScript',
'PHP'
]
});
console.log(html);
// "<option value="Java">Java</option><option value="JavaScript">JavaScript</option><option value="PHP">PHP</option>" The settings.options can be a array of objects.
optionHtml({
options: [
{
value: 'java',
text: 'Java'
},
{
value: 'javascript',
text: 'JavaScript'
},
{
value: 'php',
text: 'PHP'
}
]
});
// "<option value="java">Java</option><option value="javascript">JavaScript</option><option value="php">PHP</option>"Also, the settings.options can be an array of [value, text] pairs.
optionHtml({
options: [
['java', 'Java'],
['javascript', 'JavaScript'],
['php', 'PHP']
]
});
// <option value="java">Java</option><option value="javascript">JavaScript</option><option value="php">PHP</option>The shortcut parameter:
optionHtml(options);
// Same as `optionHtml({ options: options })`
// For example
optionHtml(['Java', 'JavaScript', 'PHP']);Specify the selected option(s):
optionHtml({
selectedValue: ['php'],
// Or
// selectedText: ['PHP']
options: [
{
value: 'java',
text: 'Java'
},
{
value: 'javascript',
text: 'JavaScript'
},
{
value: 'php',
text: 'PHP'
}
]
});Specify the disabled option(s):
optionHtml({
disabledValue: ['php'],
// Or
// disabledText: ['PHP']
options: [
{
value: 'java',
text: 'Java'
},
{
value: 'javascript',
text: 'JavaScript'
},
{
value: 'php',
text: 'PHP'
}
]
});You can customize each <option> html string with a function passed as the second parameter.
optionHtml({
selectedValue: 'PHP',
options: ['Java', 'JavaScript', 'PHP']
}, (option, index) => {
let html = `<option value=${option.value.toLowerCase()}`;
html += option.selected ? ' selected' : '';
html += option.disabled ? ' disabled' : '';
html += `>${option.text}</option>`;
return html;
});You can control the indention of each generated <option> string with an integer or a string passed as the third parameter.
optionHtml(['Java', 'JavaScript', 'PHP'], null, 2);
optionHtml(['Java', 'JavaScript', 'PHP'], null, '\t');API
import optionHtml from 'option-html';
optionHtml(settings);
// See the below Settings section for the description about the "settings" parameter
optionHtml(options);
// Same as optionHtml({ options: options })
optionHtml(settings, replacer, space);
// With the second and third parametersreplacer
Type:
FunctionAn "option data object" and "option index" will be passed as arguments. An string must be returned. The "option data object" is an plain object with four properties which is used to generate the option string:
- value: An string;
- text: An string;
- selected:
trueor `false; - disabled:
trueor `false;
The "option index" is a number.
space
Type:
Number|StringSame as the third parameter of the JSON.stringify(), it is used to control spacing in the final string.
- If this is a Number, it indicates the number of space characters to be used as the indention of the each
<option>. - If this is a String, the string will be used as the indention of the each
<option>.
Notes, there isn't a limitation for the length of the indention.
- If this is a Number, it indicates the number of space characters to be used as the indention of the each
Settings
options
Type:
ArrayThe value of the
optionshas three types:- An array contains the primitive values, such as
[0, 1, 2]or['foo', 'bar', 'baz']; - An array of objects which must have the
valueandtextproperties; - An array of
[value, text]pairs.
- An array contains the primitive values, such as
selectedValue
Type:
String|Array|FunctionSpecify the selected option(s). If it is an
StringorArrayorFunction, it will be converted to[String(selectedValue)].selectedText
Type:
String|Array|FunctionAs
selectedValue, it specifies the selected option(s). If it is anStringorArrayorFunction, it will be converted to[String(selectedText)].disabledValue
Type:
String|Array|FunctionSpecify the disabled option(s). If it is an
StringorArrayorFunction, it will be converted to[String(disabledValue)].disabledText
Type:
String|Array|FunctionAs
disabledValue, it specifies the disabled option(s). If it is anStringorArrayorFunction, it will be converted to[String(disabledText)].
License
MIT.