Skip to content

Commit

Permalink
fix: Don't pass children to leaf nodes
Browse files Browse the repository at this point in the history
Fixes #25

Uncaught Error: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.
  • Loading branch information
Bob Fanger committed Apr 27, 2023
1 parent e42f6bd commit 783b108
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "git",
"url": "https://github.com/bfanger/svelte-preprocess-react.git"
},
"version": "0.14.1",
"version": "0.15.0",
"license": "MIT",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -52,13 +52,14 @@
"@sveltejs/package": "1",
"@testing-library/react": "^14.0.0",
"@testing-library/svelte": "^3.2.2",
"@types/react-dom": "^18.2.1",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.1",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"@vitejs/plugin-react": "3",
"autoprefixer": "^10.4.14",
"concurrently": "^8.0.1",
"eslint": "^8.39.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.8.0",
Expand All @@ -68,22 +69,21 @@
"eslint-plugin-only-warn": "^1.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-svelte3": "^4.0.0",
"eslint": "^8.39.0",
"husky": "^8.0.3",
"jsdom": "^21.1.1",
"lint-staged": "^13.2.2",
"postcss": "^8.4.23",
"prettier-plugin-svelte": "^2.10.0",
"prettier": "^2.8.8",
"react-dom": "^18.2.0",
"prettier-plugin-svelte": "^2.10.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sass": "^1.62.1",
"svelte-check": "^3.2.0",
"svelte": "^3.58.0",
"svelte-check": "^3.2.0",
"svelte2tsx": "^0.6.11",
"typescript": "^5.0.4",
"vite-tsconfig-paths": "^4.2.0",
"vite": "^4.3.3",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.30.1"
},
"dependencies": {
Expand Down
55 changes: 34 additions & 21 deletions src/lib/internal/Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,46 @@ const Bridge: React.FC<BridgeProps> = ({ createPortal, node }) => {
if (!target) {
return null;
}
const children: React.ReactElement[] = node.nodes.map((subnode) => {
return React.createElement(Bridge, {
key: `bridge${subnode.key}`,
createPortal,
node: subnode,
let children: React.ReactElement[] | undefined;
if (node.nodes.length === 0 && slot === undefined && hooks.length === 0) {
if (props.children) {
children = props.children;
props = { ...props };
delete props.children;
}
} else {
children = node.nodes.map((subnode) => {
return React.createElement(Bridge, {
key: `bridge${subnode.key}`,
createPortal,
node: subnode,
});
});
});
if (props.children) {
children.push(props.children);
props = { ...props };
delete props.children;
}
if (slot) {
children.push(React.createElement(Child, { key: "svelte-slot", el: slot }));
}
if (hooks.length >= 0) {
children.push(
...hooks.map(({ Hook, key }) =>
React.createElement(Hook, { key: `hook${key}` })
)
);
if (props.children) {
children.push(props.children);
props = { ...props };
delete props.children;
}
if (slot) {
children.push(
React.createElement(Child, { key: "svelte-slot", el: slot })
);
}
if (hooks.length >= 0) {
children.push(
...hooks.map(({ Hook, key }) =>
React.createElement(Hook, { key: `hook${key}` })
)
);
}
}
return createPortal(
React.createElement(
SvelteToReactContext.Provider,
{ value: node.contexts },
React.createElement(node.reactComponent, props, children)
children === undefined
? React.createElement(node.reactComponent, props)
: React.createElement(node.reactComponent, props, children)
),
target
);
Expand Down
10 changes: 10 additions & 0 deletions src/routes/input/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
let value = "";
function onChange(e: { target: { value: string } }) {
value = e.target.value;
}
</script>

<react:input {value} on:change={onChange} />

{value}

0 comments on commit 783b108

Please sign in to comment.