updated form utils
This commit is contained in:
parent
7ce0f09382
commit
da160bf227
4 changed files with 55 additions and 42 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@
|
||||||
/src/DistributionTool
|
/src/DistributionTool
|
||||||
com.elgato.template.streamDeckPlugin
|
com.elgato.template.streamDeckPlugin
|
||||||
.vscode/**
|
.vscode/**
|
||||||
|
package.json
|
||||||
|
|
|
||||||
|
|
@ -13,22 +13,14 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="sdpi-wrapper">
|
<div class="sdpi-wrapper">
|
||||||
<!--TODOZ: add one of each control from pi-samples to test FormUtils & CSS -->
|
|
||||||
<form id="property-inspector">
|
<form id="property-inspector">
|
||||||
<div class="sdpi-item">
|
<div class="sdpi-item">
|
||||||
<div data-localize class="sdpi-item-label">Name</div>
|
<div data-localize class="sdpi-item-label">Name</div>
|
||||||
<input
|
<input class="sdpi-item-value" name="name" type="text" value="" />
|
||||||
class="sdpi-item-value"
|
|
||||||
name="name"
|
|
||||||
type="text"
|
|
||||||
value=""
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div type="textarea" class="sdpi-item">
|
<div type="textarea" class="sdpi-item">
|
||||||
<div class="sdpi-item-label up20" data-localize>
|
<div class="sdpi-item-label up20" data-localize>Message</div>
|
||||||
Message
|
|
||||||
</div>
|
|
||||||
<div class="sdpi-item-value textarea">
|
<div class="sdpi-item-value textarea">
|
||||||
<textarea
|
<textarea
|
||||||
name="message"
|
name="message"
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ StreamDeck.onConnected((jsn) => {
|
||||||
const value = Utils.getFormValue(form);
|
const value = Utils.getFormValue(form);
|
||||||
StreamDeck.sendToPlugin(value, context);
|
StreamDeck.sendToPlugin(value, context);
|
||||||
StreamDeck.setSettings(value);
|
StreamDeck.setSettings(value);
|
||||||
|
console.log(value);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
// TODOZ: test other form controls like radio and checkboxes (See previous template code);
|
|
||||||
class Utils {
|
class Utils {
|
||||||
/**
|
/**
|
||||||
* Returns the value from a form using the form controls name property
|
* Returns the value from a form using the form controls name property
|
||||||
|
|
@ -6,22 +5,31 @@ class Utils {
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
static getFormValue(form) {
|
static getFormValue(form) {
|
||||||
if (typeof form === "string") {
|
if (typeof form === 'string') {
|
||||||
form = document.querySelector(form);
|
form = document.querySelector(form);
|
||||||
}
|
}
|
||||||
|
|
||||||
const elements = form?.elements;
|
const elements = form?.elements;
|
||||||
|
|
||||||
if (!elements) {
|
if (!elements) {
|
||||||
throw "Could not find form!";
|
throw 'Could not find form!';
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array.from(elements)
|
const formData = new FormData(form);
|
||||||
.filter((element) => element?.name)
|
let formValue = {};
|
||||||
.reduce((value, element) => {
|
|
||||||
value[element.name] = element.value;
|
formData.forEach((value, key) => {
|
||||||
return value;
|
if (!Reflect.has(formValue, key)) {
|
||||||
}, {});
|
formValue[key] = value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!Array.isArray(formValue[key])) {
|
||||||
|
formValue[key] = [formValue[key]];
|
||||||
|
}
|
||||||
|
formValue[key].push(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
return formValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -30,20 +38,31 @@ class Utils {
|
||||||
* @param {*} form
|
* @param {*} form
|
||||||
*/
|
*/
|
||||||
static setFormValue(jsn, form) {
|
static setFormValue(jsn, form) {
|
||||||
if (typeof form === "string") {
|
if (typeof form === 'string') {
|
||||||
form = document.querySelector(form);
|
form = document.querySelector(form);
|
||||||
}
|
}
|
||||||
|
|
||||||
const elements = form?.elements;
|
const elements = form?.elements;
|
||||||
|
|
||||||
if (!elements) {
|
if (!elements) {
|
||||||
throw "Could not find form!";
|
throw 'Could not find form!';
|
||||||
}
|
}
|
||||||
|
|
||||||
Array.from(elements)
|
Array.from(elements)
|
||||||
.filter((element) => element?.name)
|
.filter((element) => element?.name)
|
||||||
.forEach((element) => {
|
.forEach((element) => {
|
||||||
element.value = jsn[element.name] ?? "";
|
const { name, type } = element;
|
||||||
|
const value = jsn[name];
|
||||||
|
const isCheckOrRadio = type === 'checkbox' || type === 'radio';
|
||||||
|
console.log(element.type)
|
||||||
|
if (isCheckOrRadio) {
|
||||||
|
const isSingle = value === element.value;
|
||||||
|
if (isSingle || (Array.isArray(value) && value.includes(element.value))) {
|
||||||
|
element.checked = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
element.value = value ?? '';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue