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: add and remove var prop attribute #7206

Merged
merged 2 commits into from
Dec 31, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@mui/material": "5.16.4",
"@mui/system": "5.16.4",
"@mui/x-data-grid": "6.20.4",
"@qwik-ui/headless": "0.5.0",
"@qwik-ui/headless": "0.6.3",
"@qwik.dev/core": "workspace:*",
"@qwik.dev/react": "workspace:*",
"@qwik.dev/router": "workspace:*",
Expand Down
20 changes: 12 additions & 8 deletions packages/qwik/src/core/client/vnode-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,23 +848,27 @@ export const vnode_diff = (
if (dstKey && isHtmlAttributeAnEventName(dstKey)) {
patchEventDispatch = true;
dstIdx++;
} else if (dstKey) {
record(dstKey, null);
} else {
record(dstKey!, null);
dstIdx--;
}
dstKey = dstIdx < dstLength ? dstAttrs[dstIdx++] : null;
} else if (dstKey == null) {
// Destination has more keys, so we need to insert them from source.
const isEvent = isJsxPropertyAnEventName(srcKey);
if (srcKey && isEvent) {
if (isEvent) {
// Special handling for events
patchEventDispatch = true;
recordJsxEvent(srcKey, srcAttrs[srcIdx]);
} else if (srcKey) {
record(srcKey, srcAttrs[srcIdx]);
} else {
record(srcKey!, srcAttrs[srcIdx]);
}
srcIdx++;
srcKey = srcIdx < srcLength ? srcAttrs[srcIdx++] : null;
// we need to increment dstIdx too, because we added destination key and value to the VNode
// and dstAttrs is a reference to the VNode
dstIdx++;
dstKey = dstIdx < dstLength ? dstAttrs[dstIdx++] : null;
} else if (srcKey == dstKey) {
const srcValue = srcAttrs[srcIdx++];
const dstValue = dstAttrs[dstIdx++];
Expand Down Expand Up @@ -892,11 +896,11 @@ export const vnode_diff = (
dstKey = dstIdx < dstLength ? dstAttrs[dstIdx++] : null;
} else {
// Source is missing the key, so we need to remove it from destination.
if (dstKey && isHtmlAttributeAnEventName(dstKey)) {
if (isHtmlAttributeAnEventName(dstKey)) {
patchEventDispatch = true;
dstIdx++;
} else if (dstKey) {
record(dstKey, null);
} else {
record(dstKey!, null);
dstIdx--;
}
dstKey = dstIdx < dstLength ? dstAttrs[dstIdx++] : null;
Expand Down
54 changes: 54 additions & 0 deletions packages/qwik/src/core/tests/attributes.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,58 @@ describe.each([
</Component>
);
});

it('should add and remove var props on destination vnode', async () => {
const Tab = component$((props: any) => {
return <button {...props} onClick$={() => props.onClick$()}></button>;
});

const Wrapper = component$(() => {
const selected = useSignal(0);
return (
<>
<Tab
data-selected={selected.value === 0}
id="button-0"
onClick$={() => (selected.value = 0)}
/>
<Tab
data-selected={selected.value === 1}
id="button-1"
onClick$={() => (selected.value = 1)}
/>
</>
);
});

const { vNode, document } = await render(<Wrapper />, { debug });

expect(vNode).toMatchVDOM(
<Component ssr-required>
<Fragment ssr-required>
<Component ssr-required>
<button data-selected id="button-0"></button>
</Component>
<Component ssr-required>
<button id="button-1"></button>
</Component>
</Fragment>
</Component>
);

await trigger(document.body, 'button[id=button-1]', 'click');

expect(vNode).toMatchVDOM(
<Component ssr-required>
<Fragment ssr-required>
<Component ssr-required>
<button id="button-0"></button>
</Component>
<Component ssr-required>
<button data-selected id="button-1"></button>
</Component>
</Fragment>
</Component>
);
});
});
13 changes: 6 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading