Skip to content

Commit b3ea528

Browse files
authored
fix: force update on forwarded slots (#33)
1 parent a34ff99 commit b3ea528

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

packages/babel-plugin-jsx/src/transform-vue-jsx.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,17 @@ const getChildren = (
354354
return transformedText;
355355
}
356356
if (path.isJSXExpressionContainer()) {
357-
return transformJSXExpressionContainer(path);
357+
const expression = transformJSXExpressionContainer(path);
358+
359+
if (t.isIdentifier(expression)) {
360+
const { name } = expression as t.Identifier;
361+
const { referencePaths } = path.scope.getBinding(name) || {};
362+
referencePaths?.forEach(referencePath => {
363+
walksScope(referencePath, name);
364+
})
365+
}
366+
367+
return expression;
358368
}
359369
if (t.isJSXSpreadChild(path)) {
360370
return transformJSXSpreadChild(path as NodePath<t.JSXSpreadChild>);

packages/babel-plugin-jsx/src/utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ const transformJSXSpreadChild = (
171171

172172
const walksScope = (path: NodePath, name: string) => {
173173
if (path.scope.hasBinding(name) && path.parentPath) {
174-
path.parentPath.setData('optimize', false);
174+
if (t.isJSXElement(path.parentPath.node)) {
175+
path.parentPath.setData('optimize', false);
176+
}
175177
walksScope(path.parentPath, name);
176178
}
177179
}

packages/babel-plugin-jsx/test/index.test.js

+41-7
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,18 @@ describe('PatchFlags', () => {
353353

354354
expect(wrapper.classes().sort()).toEqual(['b', 'static'].sort());
355355
});
356+
});
356357

357-
test('variables outside slot', async () => {
358-
const A = {
359-
render() {
360-
return this.$slots.default();
361-
},
362-
};
358+
describe('variables outside slots', async () => {
359+
const A = {
360+
render() {
361+
return this.$slots.default();
362+
},
363+
};
363364

364-
A.inheritAttrs = false;
365+
A.inheritAttrs = false;
365366

367+
test('internal', async () => {
366368
const wrapper = mount({
367369
data() {
368370
return {
@@ -390,7 +392,39 @@ describe('PatchFlags', () => {
390392
});
391393

392394
expect(wrapper.get('#textarea').element.innerHTML).toBe('0');
395+
await wrapper.get('#button').trigger('click');
396+
expect(wrapper.get('#textarea').element.innerHTML).toBe('1');
397+
});
398+
399+
test('forwarded', async () => {
400+
const wrapper = mount({
401+
data() {
402+
return {
403+
val: 0,
404+
};
405+
},
406+
methods: {
407+
inc() {
408+
this.val += 1;
409+
},
410+
},
411+
render() {
412+
const attrs = {
413+
innerHTML: this.val,
414+
};
415+
const textarea = <textarea id="textarea" {...attrs} />;
416+
return (
417+
<A inc={this.inc}>
418+
<div>
419+
{textarea}
420+
</div>
421+
<button id="button" onClick={this.inc}>+1</button>
422+
</A>
423+
);
424+
},
425+
});
393426

427+
expect(wrapper.get('#textarea').element.innerHTML).toBe('0');
394428
await wrapper.get('#button').trigger('click');
395429
expect(wrapper.get('#textarea').element.innerHTML).toBe('1');
396430
});

0 commit comments

Comments
 (0)