Skip to content

Commit 9726a64

Browse files
committed
chore(compat): update listeners mixin for vue3
1 parent 92db60b commit 9726a64

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/mixins/listeners.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
import { makePropCacheMixin } from '../utils/cache'
2+
import { Vue, isVue3 } from '../vue'
23

3-
export const listenersMixin = makePropCacheMixin('$listeners', 'bvListeners')
4+
const listenersMixinVue2 = makePropCacheMixin('$listeners', 'bvListeners')
5+
6+
const listenersMixinVue3 = Vue.extend({
7+
data() {
8+
return {
9+
bvListeners: {}
10+
}
11+
},
12+
created() {
13+
this.bvListeners = {
14+
...this.$listeners
15+
}
16+
},
17+
beforeUpdate() {
18+
this.bvListeners = {
19+
...this.$listeners
20+
}
21+
}
22+
})
23+
24+
export const listenersMixin = isVue3 ? listenersMixinVue3 : listenersMixinVue2

src/mixins/listeners.spec.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isVue3 } from '../vue'
12
import { mount } from '@vue/test-utils'
23
import { listenersMixin } from './listeners'
34

@@ -133,17 +134,41 @@ describe('mixins > listeners', () => {
133134
const App1 = {
134135
components: { Input1 },
135136
props: ['listenFocus1', 'listenFocus2'],
137+
methods: {
138+
emit1($event) {
139+
if (this.listenFocus1) {
140+
this.$emit('focus1', $event)
141+
}
142+
},
143+
emit2($event) {
144+
if (this.listenFocus2) {
145+
this.$emit('focus2', $event)
146+
}
147+
}
148+
},
136149
template: `<div>
137-
<Input1 @focus="listenFocus1 ? $emit('focus1', $event) : () => {}" />
138-
<Input1 @focus="listenFocus2 ? $emit('focus2', $event) : () => {}" />
150+
<Input1 @focus="emit1" />
151+
<Input1 @focus="emit2" />
139152
</div>`
140153
}
141154
const App2 = {
142155
components: { Input2 },
143156
props: ['listenFocus1', 'listenFocus2'],
157+
methods: {
158+
emit1($event) {
159+
if (this.listenFocus1) {
160+
this.$emit('focus1', $event)
161+
}
162+
},
163+
emit2($event) {
164+
if (this.listenFocus2) {
165+
this.$emit('focus2', $event)
166+
}
167+
}
168+
},
144169
template: `<div>
145-
<Input2 @focus="listenFocus1 ? $emit('focus1', $event) : () => {}" />
146-
<Input2 @focus="listenFocus2 ? $emit('focus2', $event) : () => {}" />
170+
<Input2 @focus="emit1" />
171+
<Input2 @focus="emit2" />
147172
</div>`
148173
}
149174

@@ -172,15 +197,15 @@ describe('mixins > listeners', () => {
172197
expect(wrapper1.emitted().focus1).toBeTruthy()
173198
expect(wrapper1.emitted().focus2).not.toBeTruthy()
174199
// Both `Input1`'s are re-rendered (See: https://github.com/vuejs/vue/issues/7257)
175-
expect(input1RenderCount).toBe(4)
200+
expect(input1RenderCount).toBe(isVue3 ? 2 : 4)
176201

177202
// Enable focus events for the second input and trigger it
178203
await wrapper1.setProps({ listenFocus2: true })
179204
await $inputs1.at(1).trigger('focus')
180205
expect(wrapper1.emitted().focus1).toBeTruthy()
181206
expect(wrapper1.emitted().focus2).toBeTruthy()
182207
// Both `Input1`'s are re-rendered (See: https://github.com/vuejs/vue/issues/7257)
183-
expect(input1RenderCount).toBe(6)
208+
expect(input1RenderCount).toBe(isVue3 ? 2 : 6)
184209

185210
// --- `Input2` tests ---
186211

0 commit comments

Comments
 (0)