Skip to content

Commit 783b108

Browse files
author
Bob Fanger
committed
fix: Don't pass children to leaf nodes
Fixes #25 Uncaught Error: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.
1 parent e42f6bd commit 783b108

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "https://github.com/bfanger/svelte-preprocess-react.git"
1212
},
13-
"version": "0.14.1",
13+
"version": "0.15.0",
1414
"license": "MIT",
1515
"type": "module",
1616
"scripts": {
@@ -52,13 +52,14 @@
5252
"@sveltejs/package": "1",
5353
"@testing-library/react": "^14.0.0",
5454
"@testing-library/svelte": "^3.2.2",
55-
"@types/react-dom": "^18.2.1",
5655
"@types/react": "^18.2.0",
56+
"@types/react-dom": "^18.2.1",
5757
"@typescript-eslint/eslint-plugin": "^5.59.1",
5858
"@typescript-eslint/parser": "^5.59.1",
5959
"@vitejs/plugin-react": "3",
6060
"autoprefixer": "^10.4.14",
6161
"concurrently": "^8.0.1",
62+
"eslint": "^8.39.0",
6263
"eslint-config-airbnb-base": "^15.0.0",
6364
"eslint-config-airbnb-typescript": "^17.0.0",
6465
"eslint-config-prettier": "^8.8.0",
@@ -68,22 +69,21 @@
6869
"eslint-plugin-only-warn": "^1.1.0",
6970
"eslint-plugin-prettier": "^4.2.1",
7071
"eslint-plugin-svelte3": "^4.0.0",
71-
"eslint": "^8.39.0",
7272
"husky": "^8.0.3",
7373
"jsdom": "^21.1.1",
7474
"lint-staged": "^13.2.2",
7575
"postcss": "^8.4.23",
76-
"prettier-plugin-svelte": "^2.10.0",
7776
"prettier": "^2.8.8",
78-
"react-dom": "^18.2.0",
77+
"prettier-plugin-svelte": "^2.10.0",
7978
"react": "^18.2.0",
79+
"react-dom": "^18.2.0",
8080
"sass": "^1.62.1",
81-
"svelte-check": "^3.2.0",
8281
"svelte": "^3.58.0",
82+
"svelte-check": "^3.2.0",
8383
"svelte2tsx": "^0.6.11",
8484
"typescript": "^5.0.4",
85-
"vite-tsconfig-paths": "^4.2.0",
8685
"vite": "^4.3.3",
86+
"vite-tsconfig-paths": "^4.2.0",
8787
"vitest": "^0.30.1"
8888
},
8989
"dependencies": {

src/lib/internal/Bridge.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,46 @@ const Bridge: React.FC<BridgeProps> = ({ createPortal, node }) => {
2121
if (!target) {
2222
return null;
2323
}
24-
const children: React.ReactElement[] = node.nodes.map((subnode) => {
25-
return React.createElement(Bridge, {
26-
key: `bridge${subnode.key}`,
27-
createPortal,
28-
node: subnode,
24+
let children: React.ReactElement[] | undefined;
25+
if (node.nodes.length === 0 && slot === undefined && hooks.length === 0) {
26+
if (props.children) {
27+
children = props.children;
28+
props = { ...props };
29+
delete props.children;
30+
}
31+
} else {
32+
children = node.nodes.map((subnode) => {
33+
return React.createElement(Bridge, {
34+
key: `bridge${subnode.key}`,
35+
createPortal,
36+
node: subnode,
37+
});
2938
});
30-
});
31-
if (props.children) {
32-
children.push(props.children);
33-
props = { ...props };
34-
delete props.children;
35-
}
36-
if (slot) {
37-
children.push(React.createElement(Child, { key: "svelte-slot", el: slot }));
38-
}
39-
if (hooks.length >= 0) {
40-
children.push(
41-
...hooks.map(({ Hook, key }) =>
42-
React.createElement(Hook, { key: `hook${key}` })
43-
)
44-
);
39+
if (props.children) {
40+
children.push(props.children);
41+
props = { ...props };
42+
delete props.children;
43+
}
44+
if (slot) {
45+
children.push(
46+
React.createElement(Child, { key: "svelte-slot", el: slot })
47+
);
48+
}
49+
if (hooks.length >= 0) {
50+
children.push(
51+
...hooks.map(({ Hook, key }) =>
52+
React.createElement(Hook, { key: `hook${key}` })
53+
)
54+
);
55+
}
4556
}
4657
return createPortal(
4758
React.createElement(
4859
SvelteToReactContext.Provider,
4960
{ value: node.contexts },
50-
React.createElement(node.reactComponent, props, children)
61+
children === undefined
62+
? React.createElement(node.reactComponent, props)
63+
: React.createElement(node.reactComponent, props, children)
5164
),
5265
target
5366
);

src/routes/input/+page.svelte

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
let value = "";
3+
function onChange(e: { target: { value: string } }) {
4+
value = e.target.value;
5+
}
6+
</script>
7+
8+
<react:input {value} on:change={onChange} />
9+
10+
{value}

0 commit comments

Comments
 (0)