Skip to content

Commit 663a8fd

Browse files
committed
Fix children being other type
1 parent 5757e62 commit 663a8fd

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-tsx",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A simple way to write tsx files. No babel required!",
55
"main": "dist/index.js",
66
"type": "commonjs",

src/element.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default class Element {
66
constructor(
77
name: string,
88
attributes: AttributesType | null,
9-
...children: Element[]
9+
...children: (Element | string | number)[]
1010
) {
1111
this.element = document.createElement(name);
1212
const trueAttributes = attributes || {};
@@ -20,7 +20,18 @@ export default class Element {
2020
this.element.setAttribute(name, value.toString());
2121
}
2222
});
23-
children.forEach((child) => this.element.appendChild(child.element));
23+
children.forEach((child) => {
24+
let node: Node;
25+
if (typeof child === "string") {
26+
node = document.createTextNode(child);
27+
} else if (typeof child === "number") {
28+
node = document.createTextNode(child.toString());
29+
} else {
30+
node = child.element;
31+
}
32+
33+
this.element.appendChild(node);
34+
});
2435
}
2536
}
2637

0 commit comments

Comments
 (0)