Skip to content

Commit

Permalink
Textarea fields in usermods
Browse files Browse the repository at this point in the history
A textarea with a label ending in `>` will be presented as a textarea,
allowing for multi-line text.
  • Loading branch information
obar committed Oct 24, 2024
1 parent 197f47b commit d88f910
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
13 changes: 13 additions & 0 deletions usermods/EXAMPLE_v2/usermod_v2_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MyExampleUsermod : public Usermod {
unsigned long testULong = 42424242;
float testFloat = 42.42;
String testString = "Forty-Two";
String testMultilineString = "Forty...\nTwo"; // Key has > as final character, makes it multiline

// These config variables have defaults set inside readFromConfig()
int testInt;
Expand Down Expand Up @@ -232,6 +233,7 @@ class MyExampleUsermod : public Usermod {
top["testULong"] = testULong;
top["testFloat"] = testFloat;
top["testString"] = testString;
top["testMultilineString>"] = testMultilineString; // Key has > as final character, makes it multiline
JsonArray pinArray = top.createNestedArray("pin");
pinArray.add(testPins[0]);
pinArray.add(testPins[1]);
Expand Down Expand Up @@ -267,6 +269,17 @@ class MyExampleUsermod : public Usermod {
configComplete &= getJsonValue(top["testULong"], testULong);
configComplete &= getJsonValue(top["testFloat"], testFloat);
configComplete &= getJsonValue(top["testString"], testString);
configComplete &= getJsonValue(top["testMultilineString"], testMultilineString);

// Remove any carriage returns (\r) from testMultilineString, leave only newlines (\n)
int i=0;
int lastI;
while(true) {
lastI = i;
i = testMultilineString.indexOf('\r',lastI);
if (i < 0) break;
testMultilineString.remove(i,1);
}

// A 3-argument getJsonValue() assigns the 3rd argument as a default value if the Json value is missing
configComplete &= getJsonValue(top["testInt"], testInt, 42);
Expand Down
9 changes: 7 additions & 2 deletions wled00/data/settings_um.htm
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,19 @@
}
break;
default:
t = "text"; c = `value="${o}" style="width:250px;"`;
if (f.substr(-1)===">") {
t = "textarea"; c = `style="width:300px;height:150px;"`;
} else {
t = "text"; c = `value="${o}" style="width:250px;"`;
}
break;
}
urows += ` ${initCap(f)} `; //only show field (key is shown in grouping)
// https://stackoverflow.com/questions/11657123/posting-both-checked-and-unchecked-checkboxes
if (t=="checkbox") urows += `<input type="hidden" name="${k}:${f}${a?"[]":""}" value="false">`;
else if (!a) urows += `<input type="hidden" name="${k}:${f}${a?"[]":""}" value="${t}">`;
urows += `<input type="${t==="int"?"number":t}" name="${k}:${f}${a?"[]":""}" ${c} oninput="check(this,'${k.substr(k.indexOf(":")+1)}')"><br>`;
if (t=="textarea") urows += `<br><textarea name="${k}:${f}${a?"[]":""}" ${c}>${o}</textarea><br>`;
else urows += `<input type="${t==="int"?"number":t}" name="${k}:${f}${a?"[]":""}" ${c} oninput="check(this,'${k.substr(k.indexOf(":")+1)}')"><br>`;
}
}
function pinDropdowns() {
Expand Down
2 changes: 1 addition & 1 deletion wled00/data/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ button.sml {
.warn {
color: #fa0;
}
input {
input, textarea {
background: #333;
color: #fff;
font-family: Verdana, sans-serif;
Expand Down

0 comments on commit d88f910

Please sign in to comment.