Skip to content

Commit 7a1a49e

Browse files
authored
feat: pass listeners to functional components (#1036)
1 parent 0d3e46d commit 7a1a49e

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

Diff for: packages/create-instance/create-functional-component.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,26 @@ export default function createFunctionalComponent (
1616
validateSlots(mountingOptions.slots)
1717
}
1818

19-
const data = mountingOptions.context ||
20-
component.FunctionalRenderContext || {}
21-
data.scopedSlots = createScopedSlots(mountingOptions.scopedSlots)
19+
const context =
20+
mountingOptions.context ||
21+
component.FunctionalRenderContext ||
22+
{}
23+
24+
const listeners = mountingOptions.listeners
25+
26+
if (listeners) {
27+
Object.keys(listeners).forEach(key => {
28+
context.on[key] = listeners[key]
29+
})
30+
}
31+
32+
context.scopedSlots = createScopedSlots(mountingOptions.scopedSlots)
2233

2334
return {
2435
render (h: Function) {
2536
return h(
2637
component,
27-
data,
38+
context,
2839
(mountingOptions.context &&
2940
mountingOptions.context.children &&
3041
mountingOptions.context.children.map(

Diff for: test/specs/mounting-options/listeners.spec.js

+39-12
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,48 @@ import {
88
import { itDoNotRunIf } from 'conditional-specs'
99

1010
describeWithShallowAndMount('options.listeners', mountingMethod => {
11-
itDoNotRunIf(isRunningPhantomJS, 'handles inherit listeners', () => {
12-
if (!listenersSupported) return
13-
const aListener = () => {}
14-
const wrapper = mountingMethod(
15-
compileToFunctions('<p :id="aListener" />'),
16-
{
17-
listeners: {
18-
aListener
11+
itDoNotRunIf(
12+
isRunningPhantomJS || !listenersSupported,
13+
'handles inherit listeners', () => {
14+
const aListener = () => {}
15+
const wrapper = mountingMethod(
16+
compileToFunctions('<p :id="aListener" />'),
17+
{
18+
listeners: {
19+
aListener
20+
}
1921
}
22+
)
23+
24+
expect(wrapper.vm.$listeners.aListener.fns).to.equal(aListener)
25+
})
26+
27+
itDoNotRunIf(
28+
isRunningPhantomJS || !listenersSupported,
29+
'passes listeners to functional components', () => {
30+
const TestComponent = {
31+
render (h, ctx) {
32+
ctx.listeners.aListener()
33+
ctx.listeners.bListener()
34+
return h('div')
35+
},
36+
functional: true
2037
}
21-
)
2238

23-
expect(wrapper.vm.$listeners.aListener.fns).to.equal(aListener)
24-
expect(wrapper.vm.$listeners.aListener.fns).to.equal(aListener)
25-
})
39+
mountingMethod(
40+
TestComponent,
41+
{
42+
context: {
43+
on: {
44+
bListener () {}
45+
}
46+
},
47+
listeners: {
48+
aListener () {}
49+
}
50+
}
51+
)
52+
})
2653

2754
itDoNotRunIf(
2855
vueVersion < 2.5,

0 commit comments

Comments
 (0)