I set up a JS Query with the code below and it worked fine.
const messages = {
"000": "message 1",
"100": "message 2",
"110": "message 3",
// Add more cases as needed
};
// Get values from Retool components
const a = Number(aInput.value); // Example: Get value from a text input
const b = Number(bInput.value);
const c = Number(cInput.value);
const key = `${a}${b}${c}`;
return messages[key] || "Default message"; // Return message or fallback
This is giving me some flashbacks to logic gates and truth tables from long long ago. Since you are talking about 0 or 1 values you could probably write some case logic for this:
In your example, message2 would result from the condition a && c.
In your message 4 example, it would be the condition !a && !c.
Mapping your 0s and 1s to true/false comparisons would still require you to write the logic for the proper combos, but you could maybe then start to generalize the right pattern for all of your cases. You also might need to consider some logical precedence for case (like only a which would need to take precedence over a or b if that distinction was required).
I don't know how many options you have or if they are meant to be dynamic, but you could definitely reduce the total number of permutations this way.
Some clunky code:
let groupData = {{ checkboxGroup1.values }}
let selectedData = {{ checkboxGroup1.selectedItems }}
let message = "DEFAULT MESSAGE BEFORE RUN"
let options = groupData.map(option => {
return {
option: option,
optionValue: selectedData.some(selection => selection.value === option)
}
})
switch (options.length > 0){
case options[0].optionValue && options[2].optionValue:{
message = "1 AND 3";
break;
}
case options[0].optionValue && (!options[1].optionValue && !options[2].optionValue) :{
message = "1 ONLY";
break;
}
case options[0].optionValue || options[1].optionValue: {
message = "1 OR 2";
break;
}
default:
message = "Default after run."
break;
}
return message
Just a logical proof of concept using a checkbox group:
I took a crack at this using regex. There are definitely cleaner ways to do it with regex patterns, especially if you needed to expand to more than 3 characters, but this was the most straightforward way I could come up with.
There are also definitely more clever and more performant ways to do this, like with bit masks, which would be worth investigating.