Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: svelte event handlers #1413

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/weak-fishes-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@builder.io/mitosis': patch
'@builder.io/mitosis-cli': patch
---

Fix: Qwik methods using computed variables
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,9 @@ exports[`Svelte > jsx > Javascript Test > InputParent 1`] = `
<FormInputComponent
name=\\"kingzez\\"
type=\\"text\\"
onChange={(value) => handleChange(value)}
onChange={(value) => {
handleChange(value);
}}
/>"
`;

Expand Down Expand Up @@ -3978,7 +3980,9 @@ exports[`Svelte > jsx > Typescript Test > InputParent 1`] = `
<FormInputComponent
name=\\"kingzez\\"
type=\\"text\\"
onChange={(value) => handleChange(value)}
onChange={(value) => {
handleChange(value);
}}
/>"
`;

Expand Down
21 changes: 7 additions & 14 deletions packages/core/src/generators/svelte/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,38 +210,31 @@ type BlockToSvelte<T extends BaseNode = MitosisNode> = (props: {
}) => string;

const stringifyBinding =
(node: MitosisNode, options: ToSvelteOptions) =>
(node: MitosisNode, options: ToSvelteOptions, tagName: string) =>
([key, binding]: [string, Binding | undefined]) => {
if (key === 'innerHTML' || !binding) {
return '';
}

const { code, arguments: cusArgs = ['event'], type } = binding;
const isValidHtmlTag = VALID_HTML_TAGS.includes(node.name) || node.name === 'svelte:element';
const isValidHtmlTag = VALID_HTML_TAGS.includes(tagName) || tagName === 'svelte:element';

if (type === 'spread') {
const spreadValue = key === 'props' ? '$$props' : code;
return ` {...${spreadValue}} `;
} else if (key.startsWith('on') && isValidHtmlTag) {
} else if (key.startsWith('on')) {
// handle html native on[event] props
const event = key.replace('on', '').toLowerCase();
// TODO: handle quotes in event handler values

const valueWithoutBlock = removeSurroundingBlock(code);

if (valueWithoutBlock === key) {
return ` on:${event}={${valueWithoutBlock}} `;
} else {
return ` on:${event}="{${cusArgs.join(',')} => {${valueWithoutBlock}}}" `;
}
} else if (key.startsWith('on')) {
// handle on[custom event] props
const valueWithoutBlock = removeSurroundingBlock(code);
const keyToUse = isValidHtmlTag ? `on:${event}` : key;

if (valueWithoutBlock === key) {
return ` ${key}={${valueWithoutBlock}} `;
return ` ${keyToUse}={${valueWithoutBlock}} `;
} else {
return ` ${key}={(${cusArgs.join(',')}) => ${valueWithoutBlock}}`;
return ` ${keyToUse}="{${cusArgs.join(',')} => {${valueWithoutBlock}}}" `;
}
} else if (key === 'ref') {
return ` bind:this={${code}} `;
Expand Down Expand Up @@ -297,7 +290,7 @@ export const blockToSvelte: BlockToSvelte = ({ json, options, parentComponent })
}

const stringifiedBindings = Object.entries(json.bindings)
.map(stringifyBinding(json, options))
.map(stringifyBinding(json, options, tagName))
.join('');

str += stringifiedBindings;
Expand Down
Loading