diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/common/typedInput.js b/packages/node_modules/@node-red/editor-client/src/js/ui/common/typedInput.js index 1386a8ea5..ba7e48900 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/common/typedInput.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/common/typedInput.js @@ -345,6 +345,47 @@ } } }; + + // For a type with options, check value is a valid selection + // If !opt.multiple, returns the valid option object + // if opt.multiple, returns an array of valid option objects + // If not valid, returns null; + + function isOptionValueValid(opt, currentVal) { + if (!opt.multiple) { + for (var i=0;i').appendTo(this.uiSelect); this.optionExpandButtonIcon = $('').appendTo(this.optionExpandButton); - // Used to remember selections per-type to restore them when switching between types - this.oldValues = {}; - this.type(this.options.default||this.typeList[0].value); }catch(err) { console.log(err.stack); @@ -859,22 +899,44 @@ return this.propertyType; } else { var that = this; + var previousValue = null; var opt = this.typeMap[type]; if (opt && this.propertyType !== type) { // If previousType is !null, then this is a change of the type, rather than the initialisation var previousType = this.typeMap[this.propertyType]; var typeChanged = !!previousType; + previousValue = this.input.val(); if (typeChanged) { if (previousType.options && opt.hasValue !== true) { - this.oldValues[previousType.value] = this.input.val(); + this.oldValues[previousType.value] = previousValue; } else if (previousType.hasValue === false) { - this.oldValues[previousType.value] = this.input.val(); + this.oldValues[previousType.value] = previousValue; } else { - this.oldValues["_"] = this.input.val(); + this.oldValues["_"] = previousValue; } if ((opt.options && opt.hasValue !== true) || opt.hasValue === false) { - this.input.val(this.oldValues.hasOwnProperty(opt.value)?this.oldValues[opt.value]:(opt.default||[]).join(",")) + if (this.oldValues.hasOwnProperty(opt.value)) { + this.input.val(this.oldValues[opt.value]); + } else { + // No old value for the option type. + // It is possible code has called 'value' then 'type' + // to set the selected option. This is what the Inject/Switch/Change + // nodes did before 2.1. + // So we need to be careful to not reset the value if it is a valid option. + var validOptions = isOptionValueValid(opt,previousValue); + if (previousValue && validOptions) { + this.input.val(previousValue); + } else { + if (typeof opt.default === "string") { + this.input.val(opt.default); + } else if (Array.isArray(opt.default)) { + this.input.val(opt.default.join(",")) + } else { + this.input.val(""); + } + } + } } else { this.input.val(this.oldValues.hasOwnProperty("_")?this.oldValues["_"]:(opt.default||"")) } @@ -951,22 +1013,12 @@ var op; if (!opt.hasValue) { - var validValue = false; - var currentVal = this.input.val(); + // Check the value is valid for the available options + var validValues = isOptionValueValid(opt,this.input.val()); if (!opt.multiple) { - for (var i=0;i