A replacement for a regular <input>
that allows the type of the value to be
chosen, including options for string, number and boolean types.
Type: String
If defined, sets the default type of the input if typeField
is not set.
$(".input").typedInput({
default: "msg"
});
Type: Array
Sets the list of types the element will offer.
The value of the option is an array of string-identifiers for the predefined types and TypeDefinition objects for any custom types.
The predefined types are:
identifier | description |
---|---|
msg |
a msg. property expression |
flow |
a flow. property expression |
global |
a global. property expression |
str |
a String |
num |
a Number |
bool |
a Boolean |
json |
a valid JSON string |
bin |
a Node.js Buffer |
re |
a Regular Expression |
jsonata |
a Jsonata Expression |
date |
the current timestamp |
env |
an environment variable |
node |
a node. property expression |
cred |
a secure credential |
$(".input").typedInput({
types: ["msg","str"]
});
Type: CSS Selector
In some circumstances it is desirable to already have an <input>
element to store
the type value of the typedInput. This option allows such an existing element to be
provided. As the type of the typedInput is changed, the value of the provided input
will also change.
$(".input").typedInput({
typeField: ".my-type-field"
});
When used in a Node-RED node, this value can be stored as a node property by adding
an entry for it in the node’s defaults
object. This ensures the type is saved
along with the value in the node configuration.
<div class="form-row">
<label>Example:</label>
<input type="text" id="node-input-myField">
<input type="hidden" id="node-input-myFieldType">
</div>
RED.nodes.registerType('example', {
defaults: {
myField: { value: "" },
myFieldType: { value: "str" }
},
...
oneditprepare: function () {
$("#node-input-myField").typedInput({
typeField: "#node-input-myFieldType"
});
}
})
Since Node-RED 1.2.7
Disable the typedInput when it is currently enabled.
The optional state
parameter can be used to toggle the disabled/enabled state of the typedInput. If state
is true, the element will be disabled, otherwise it will be enabled.
$(".input").typedInput('disable');
Since Node-RED 1.2.7
Returns: Boolean
Gets whether the typedInput is currently disabled or not.
$(".input").typedInput('disabled');
Since Node-RED 1.3.3
Enable the typedInput when it is currently disabled.
$(".input").typedInput('enable');
Hide the typedInput when it is currently visible.
$(".input").typedInput('hide');
Show the typedInput when it is currently hidden.
$(".input").typedInput('show');
Returns: String
Gets the selected type of the typedInput.
var type = $(".input").typedInput('type');
Sets the selected type of the typedInput.
$(".input").typedInput('type','msg');
Sets the list of types offered by the typedInput. See the description of the types
option.
$(".input").typedInput('types',['str','num']);
Returns: Boolean
Triggers a revalidation of the typedInput’s type/value. This occurs automatically whenever the type or value change, but this method allows it to be run manually.
var isValid = $(".input").typedInput('validate');
Returns: String
Gets the value of the typedInput.
var value = $(".input").typedInput('value');
Sets the value of the typedInput.
$(".input").typedInput('value','payload');
Sets the width of the typedInput.
$(".input").typedInput('width', '200px');
Triggered when either the type or value of the input is changed.
$(".input").on('change', function(event, type, value) {} );
Note: The value
property was added in Node-RED 1.3
A TypeDefinition
object describes a type that can be offered by a typedInput
element.
It is an object with the following properties:
Property | Type | Required | Description |
---|---|---|---|
value |
string | yes | The identifier for the type |
label |
string | A label to display in the type menu | |
icon |
string | An icon to display in the type menu. This can be either an image url, or a FontAwesome 4 icon, for example "fa fa-list" . |
|
options |
array | If the type has a fixed set of values, this is an array of string options for the value. For example, ["true","false"] for the boolean type. |
|
multiple |
boolean | If options is set, this can enable multiple selection of them. |
|
hasValue |
boolean | Set to false if there is no value associated with the type. |
|
validate |
function | A function to validate the value for the type. | |
valueLabel |
function | A function that generates the label for a given value. The function takes two arguments: container - the DOM element the label should be constructed in, and value . |
|
autoComplete |
function | Since 2.1.0. If set, enable autoComplete on the input, using this function to get completion suggestions. See autoComplete for details. This option cannot be used with options , hasValue=false or valueLabel |
<input type="text" id="node-input-example1">
$("#node-input-example1").typedInput({
type:'str',
types:['str','num','bool']
})
<input type="text" id="node-input-example2">
$("#node-input-example2").typedInput({
type:'msg',
types:['msg']
})
<input type="text" id="node-input-example3">
$("#node-input-example3").typedInput({
type:'flow',
types:['flow','global']
})
<input type="text" id="node-input-example4">
$("#node-input-example4").typedInput({type:"fruit", types:[{
value: "fruit",
options: [
{ value: "apple", label: "Apple"},
{ value: "banana", label: "Banana"},
{ value: "cherry", label: "Cherry"},
]
}]})
<input type="text" id="node-input-example5">
$("#node-input-example5").typedInput({type:"fruit", types:[{
value: "fruit",
multiple: true,
options: [
{ value: "apple", label: "Apple"},
{ value: "banana", label: "Banana"},
{ value: "cherry", label: "Cherry"},
]
}]})
Due to the way the typedInput
enhances a regular HTML <input>
, its value is
stored as a string. For example, booleans are stored as "true"
and "false"
.
When stored as node properties, it is necessary for the runtime part of the node to parse the string to get the typed value.
A utility function is provided to handle the built-in types provided by the TypedInput.
RED.util.evaluateNodeProperty(value, type, node, msg, callback)
Property | Type | Required | Description |
---|---|---|---|
value |
string | yes | The property to evaluate |
type |
string | yes | The type of the property |
node |
Node | yes, for certain types | The node evaluating the property |
msg |
Message Object | yes, for certain types | A message object to evaluate against |
callback |
Callback | yes, for flow /global types |
A callback to receive the result |
For most types, the function can be used synchronously without providing a callback.
const result = RED.util.evaluateNodeProperty(value, type, node)
For msg
type, the message object should also be provided:
const result = RED.util.evaluateNodeProperty(value, type, node, msg)
To handle flow
and global
context types, the node needs to be provided as well as
a callback function due to the asynchronous nature of context access:
RED.util.evaluateNodeProperty(value, type, node, msg, (err, result) => {
if (err) {
// Something went wrong accessing context
} else {
// Do something with 'result'
}
})
Node-RED: Low-code programming for event-driven applications.
Copyright OpenJS Foundation and Node-RED contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
The OpenJS Foundation | Terms of Use | Privacy Policy | OpenJS Foundation Bylaws | Trademark Policy | Trademark List | Cookie Policy