fix: force update on forwarded slots (#33)

This commit is contained in:
Amour1688 2020-07-17 21:24:05 +08:00 committed by GitHub
parent a34ff99277
commit b3ea528a6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 9 deletions

View File

@ -354,7 +354,17 @@ const getChildren = (
return transformedText; return transformedText;
} }
if (path.isJSXExpressionContainer()) { if (path.isJSXExpressionContainer()) {
return transformJSXExpressionContainer(path); const expression = transformJSXExpressionContainer(path);
if (t.isIdentifier(expression)) {
const { name } = expression as t.Identifier;
const { referencePaths } = path.scope.getBinding(name) || {};
referencePaths?.forEach(referencePath => {
walksScope(referencePath, name);
})
}
return expression;
} }
if (t.isJSXSpreadChild(path)) { if (t.isJSXSpreadChild(path)) {
return transformJSXSpreadChild(path as NodePath<t.JSXSpreadChild>); return transformJSXSpreadChild(path as NodePath<t.JSXSpreadChild>);

View File

@ -171,7 +171,9 @@ const transformJSXSpreadChild = (
const walksScope = (path: NodePath, name: string) => { const walksScope = (path: NodePath, name: string) => {
if (path.scope.hasBinding(name) && path.parentPath) { if (path.scope.hasBinding(name) && path.parentPath) {
if (t.isJSXElement(path.parentPath.node)) {
path.parentPath.setData('optimize', false); path.parentPath.setData('optimize', false);
}
walksScope(path.parentPath, name); walksScope(path.parentPath, name);
} }
} }

View File

@ -353,8 +353,9 @@ describe('PatchFlags', () => {
expect(wrapper.classes().sort()).toEqual(['b', 'static'].sort()); expect(wrapper.classes().sort()).toEqual(['b', 'static'].sort());
}); });
});
test('variables outside slot', async () => { describe('variables outside slots', async () => {
const A = { const A = {
render() { render() {
return this.$slots.default(); return this.$slots.default();
@ -363,6 +364,7 @@ describe('PatchFlags', () => {
A.inheritAttrs = false; A.inheritAttrs = false;
test('internal', async () => {
const wrapper = mount({ const wrapper = mount({
data() { data() {
return { return {
@ -390,7 +392,39 @@ describe('PatchFlags', () => {
}); });
expect(wrapper.get('#textarea').element.innerHTML).toBe('0'); expect(wrapper.get('#textarea').element.innerHTML).toBe('0');
await wrapper.get('#button').trigger('click');
expect(wrapper.get('#textarea').element.innerHTML).toBe('1');
});
test('forwarded', async () => {
const wrapper = mount({
data() {
return {
val: 0,
};
},
methods: {
inc() {
this.val += 1;
},
},
render() {
const attrs = {
innerHTML: this.val,
};
const textarea = <textarea id="textarea" {...attrs} />;
return (
<A inc={this.inc}>
<div>
{textarea}
</div>
<button id="button" onClick={this.inc}>+1</button>
</A>
);
},
});
expect(wrapper.get('#textarea').element.innerHTML).toBe('0');
await wrapper.get('#button').trigger('click'); await wrapper.get('#button').trigger('click');
expect(wrapper.get('#textarea').element.innerHTML).toBe('1'); expect(wrapper.get('#textarea').element.innerHTML).toBe('1');
}); });