I'm always confused about what typedInput saves into the node and wonder whether I'm doing something wrong or this is as-designed...
If I have a typedInput and select the number type or boolean type, will the node get a javascript Number respectively boolean or will it get a string with the number respectively a string with "true"/"false"?
I built a minimal test-case which suggests that the value saved is always a string and thus the node implementation has to make the conversion to the intended type. Is this correct or am I simply missing a trick?
typedinput-test.js:
module.exports = function(RED) {
function typedinput_test(config) {
RED.nodes.createNode(this, config)
console.log("typedinput_test", JSON.stringify(config))
}
RED.nodes.registerType("typedinput test", typedinput_test)
}
typedinput-test.html:
<script type="text/javascript">
RED.nodes.registerType('typedinput test', {
category: "tests",
color: "#FFFF00",
defaults: {
name: {value:""},
test_string:{value:"default string", required:true},
test_number:{value:125, required:true},
},
inputs: 0,
outputs: 1,
icon: "font-awesome/fa-th",
paletteLabel: "typedinput test",
label() { return this.name || "typedinput test" },
oneditprepare() {
console.log("oneditprepare", this)
$("#node-input-test_string").typedInput({
type:"str",
types:["str","num","bool"],
typeField: "#node-input-test_string-type"
})
$("#node-input-test_number").typedInput({
type:"str",
types:["str","num","bool"],
typeField: "#node-input-test_number-type"
})
},
});
</script>
<script type="text/html" data-template-name="typedinput test">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
<div class="form-row">
<label for="node-input-test_string">Test string</label>
<input type="text" id="node-input-test_string" />
<input type="hidden" id="node-input-test_string-type" />
</div>
<div class="form-row">
<label for="node-input-test_number">Test number</label>
<input type="text" id="node-input-test_number" />
<input type="hidden" id="node-input-test_number-type" />
</div>
</script>
<script type="text/html" data-help-name="typedinput test"></script>
The edit panel comes up:
I change it:
I deploy and the node prints:
typedinput_test {"id":"f553feb13caf2a2d","type":"typedinput test","z":"451d9acba8e52c2f","name":"",
"test_string":"true","test_number":"44",
"x":680,"y":480,"wires":[[]]}
And I see that both test_string and test_number have string values.