Hi guys, i'm relatively new to blockly community and i'm trying different stuffs with blocks, but now i found a problem i can't get over it.
I've created a block that has a first dropdown element, showing some column names (dropdown elements are generated dynamically here), a second dropdown showing static operators, and a third part that is an input or another dropdown depending on the value selected on the first dropdown.
The block kinda works correctly but the first and the third dropdown render is weird, i'm going to attach a screenshot to show how it renders now.
1 and 2 are correct renders that happen sometimes, and sometimes the 3rd render shows up, breaking the entire component.
Blockly.Blocks["logic_equality"] = {
init: function () {
this.columnDropdown = new Blockly.FieldDropdown(
this.generateColumnOptions.bind(this),
this.onColumnChange.bind(this)
);
this.columnInput = this.appendDummyInput("A").appendField(
this.columnDropdown,
"columnDropdown"
);
this.operatorInput = this.appendDummyInput().appendField(
new Blockly.FieldDropdown([
["=", "EQ"],
["≠", "NQ"],
]),
"OP"
);
this.dynamicInput = null;
this.setOutput(true, "condition");
this.setColour("#5C81A6");
this.setTooltip("Confronta una variabile con un valore.");
this.setHelpUrl("");
this.setInputsInline(true);
},
generateColumnOptions: function () {
if (!allColumns || allColumns.length === 0) {
return [["", '""']];
}
return allColumns.map((element) => {
return [element[0], element[1]];
});
},
onColumnChange: function (selectedColumn) {
const currentColumn = this.getFieldValue("columnDropdown");
if (selectedColumn === currentColumn) {
return;
}
if (this.dynamicInput) {
this.removeInput(this.dynamicInput.name);
this.dynamicInput = null;
}
if (this.getInput("B")) {
this.removeInput("B");
}
const matchingField = valueList.find(
(field) => field.field_name === selectedColumn
);
try {
if (matchingField && matchingField.values.length > 0) {
this.dynamicInput = this.appendDummyInput(
"dynamicValueInput"
).appendField(
new Blockly.FieldDropdown(
matchingField.values.map((value) => [value, value]),
(newValue) => {
this.setFieldValue(newValue, "dynamicValue");
}
),
"dynamicValue"
);
} else {
this.dynamicInput = this.appendValueInput("B").setCheck([
"Number",
"String",
"Date",
"db_null",
]);
}
this.initSvg();
this.render();
} catch (error) {
console.error("Errore durante l'aggiornamento del blocco:", error);
}
},
};
I'm aware that the code has probably more than one problem but i can't figure them out, feeel free to blast my code :)