Skip to content

Commit

Permalink
fix(to-vue-3): reactive prop w/ event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 25, 2020
1 parent c388b60 commit 62a01b0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
31 changes: 16 additions & 15 deletions src/to-vue-3.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Vue from 'vue';
import {createApp, shallowReactive, h} from 'vue3';
import {vue3ProxyNode, privateState} from './utils';
import {vue3ProxyNode} from './utils';

const hyphenateRE = /\B([A-Z])/g;
const hyphenate = string => string.replace(hyphenateRE, '-$1').toLowerCase();
Expand All @@ -20,6 +20,7 @@ function getAttrsAndListeners($attrs) {
class: undefined,
on: {},
attrs: {},
props: {},
};
const {on, attrs} = data;

Expand Down Expand Up @@ -107,10 +108,7 @@ const isConfigurableProperty = {configurable: true};

const vue3WrapperBase = {
created() {
this._[privateState] = Vue.observable({
data: null,
slots: null,
});
this.v2 = undefined;
},

mounted() {
Expand All @@ -126,13 +124,15 @@ const vue3WrapperBase = {
},
}),

render: h => h(
this.$options.component,
{
...this._[privateState].data,
scopedSlots: transformSlots(h, this),
},
),
render(h) {
return h(
vm.$options.component,
{
...getAttrsAndListeners(vm.$attrs),
scopedSlots: transformSlots(h, vm),
},
);
},

mounted() {
// Rewrite Vue3 vnodes to reference Vue 2 element
Expand Down Expand Up @@ -167,9 +167,10 @@ const vue3WrapperBase = {
},

render() {
const data = getAttrsAndListeners(this.$attrs);
this._[privateState].data = data;
this._[privateState].slots = this.$slots;
if (this.v2) {
this.v2.$forceUpdate();
}

return h('div');
},
};
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/to-vue-3.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Vue 2 component in a Vue 3 app event-listeners 1`] = `"<button>I'm Vue 2 123</button>"`;

exports[`Vue 2 component in a Vue 3 app reactivity 1`] = `
"<div> I'm Vue 3 0 <article title=\\"Attribute 0\\">
I'm Vue 2
Expand Down
8 changes: 7 additions & 1 deletion test/to-vue-3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@ describe('Vue 2 component in a Vue 3 app', () => {
const customEventHandler = jest.fn();

const Vue2Component = {
template: '<button v-on="$listeners" @click="$emit(\'custom-event\')">I\'m Vue 2</button>',
props: ['someProp'],
template: '<button v-on="$listeners" @click="$emit(\'custom-event\')">I\'m Vue 2 {{ someProp }}</button>',
};

const app = mount({
template: outdent`
<vue-2-component
some-prop="123"
@click.capture.once="clickHandler"
@custom-event="customEventHandler"
>
Expand All @@ -210,6 +212,10 @@ describe('Vue 2 component in a Vue 3 app', () => {
},
});

await nextTick();

expect(app.html()).toMatchSnapshot();

const button = app.vm.$el;

button.click();
Expand Down

0 comments on commit 62a01b0

Please sign in to comment.