Skip to content

Commit e81efab

Browse files
committed
Widget improvements
1 parent 90ecbe9 commit e81efab

4 files changed

Lines changed: 23 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## JavaScriptSnippet
22

3+
:sparkles: **The widget has been rebuild from the ground up as pluggable widget!** :sparkles:
4+
35
Insert custom JavaScript to your Mendix pages, taking object attributes as input parameters.
46

57
## Features

images/screen4.png

7.66 KB
Loading

src/JavaScriptSnippet.jsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import "./ui/JavaScriptSnippet.css";
22
import { createElement, useEffect, useState } from "react";
33

44
export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
5-
const self = this;
65
const [canRender, setCanRender] = useState(false);
76
const [javaScriptString, setJavaScriptString] = useState([]);
87
const widgetName = rest.name || "";
@@ -26,24 +25,34 @@ export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
2625
// }
2726

2827
useEffect(() => {
29-
let JavaScriptStringArray = jsCode;
28+
let JSArray = jsCode;
29+
3030
if (attributeList.length) {
3131
attributeList.map(attr => {
3232
if (attr.jsAttribute.status === "available") {
33-
if (typeof attr.jsAttribute.value === "boolean") {
34-
JavaScriptStringArray = JavaScriptStringArray.split("${" + attr.jsVarName + "}").join(
35-
attr.jsAttribute.value
33+
const useValue =
34+
attr.jsAttribute.value && attr.jsAttribute.value !== undefined
35+
? attr.jsAttribute.value
36+
: attr.jsEmptyValue;
37+
38+
if (useValue === "" && typeof attr.jsAttribute.value !== "boolean") {
39+
console.warn(
40+
`${widgetName}: variable name "${attr.jsVarName}" is empty and also it's empty value replacement`
3641
);
42+
}
43+
44+
if (typeof attr.jsAttribute.value === "boolean") {
45+
JSArray = JSArray.split("${" + attr.jsVarName + "}").join(attr.jsAttribute.value);
46+
} else if (Object.prototype.toString.call(attr.jsAttribute.value) === "[object Date]") {
47+
JSArray = JSArray.split("${" + attr.jsVarName + "}").join(useValue);
3748
} else {
38-
JavaScriptStringArray = JavaScriptStringArray.split("${" + attr.jsVarName + "}").join(
39-
escape(attr.jsAttribute.value)
40-
);
49+
JSArray = JSArray.split("${" + attr.jsVarName + "}").join(escape(useValue));
4150
}
4251
}
4352
return null;
4453
});
4554
}
46-
setJavaScriptString(JavaScriptStringArray);
55+
setJavaScriptString(JSArray);
4756

4857
if (!attributeList.length) {
4958
setCanRender(true);
@@ -55,7 +64,7 @@ export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
5564
setCanRender(true);
5665
}
5766
};
58-
}, [attributeList, jsCode]);
67+
}, [attributeList, jsCode, widgetName]);
5968

6069
if (canRender) {
6170
// JavaScript evaluation will be done in the context of the widget instance.
@@ -65,7 +74,7 @@ export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
6574
(function () {
6675
// eslint-disable-next-line no-new-func
6776
Function(javaScriptString)();
68-
}.call(self));
77+
}.call());
6978
return null;
7079
} catch (error) {
7180
console.warn("Error while evaluating javascript input.");

src/JavaScriptSnippet.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</property>
2727
<property key="jsAttribute" type="attribute">
2828
<caption>Attribute</caption>
29-
<description>Value of this attribute will be used to replace ${camelCaseName}.</description>
29+
<description>Value of this attribute will be used to replace ${camelCaseName} and passed as a string (also Boolean attributes).</description>
3030
<attributeTypes>
3131
<attributeType name="Boolean"/>
3232
<attributeType name="DateTime"/>

0 commit comments

Comments
 (0)