Skip to content

Commit

Permalink
feat: pass listeners to functional components (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh authored Nov 24, 2018
1 parent 0d3e46d commit 7a1a49e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
19 changes: 15 additions & 4 deletions packages/create-instance/create-functional-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@ export default function createFunctionalComponent (
validateSlots(mountingOptions.slots)
}

const data = mountingOptions.context ||
component.FunctionalRenderContext || {}
data.scopedSlots = createScopedSlots(mountingOptions.scopedSlots)
const context =
mountingOptions.context ||
component.FunctionalRenderContext ||
{}

const listeners = mountingOptions.listeners

if (listeners) {
Object.keys(listeners).forEach(key => {
context.on[key] = listeners[key]
})
}

context.scopedSlots = createScopedSlots(mountingOptions.scopedSlots)

return {
render (h: Function) {
return h(
component,
data,
context,
(mountingOptions.context &&
mountingOptions.context.children &&
mountingOptions.context.children.map(
Expand Down
51 changes: 39 additions & 12 deletions test/specs/mounting-options/listeners.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,48 @@ import {
import { itDoNotRunIf } from 'conditional-specs'

describeWithShallowAndMount('options.listeners', mountingMethod => {
itDoNotRunIf(isRunningPhantomJS, 'handles inherit listeners', () => {
if (!listenersSupported) return
const aListener = () => {}
const wrapper = mountingMethod(
compileToFunctions('<p :id="aListener" />'),
{
listeners: {
aListener
itDoNotRunIf(
isRunningPhantomJS || !listenersSupported,
'handles inherit listeners', () => {
const aListener = () => {}
const wrapper = mountingMethod(
compileToFunctions('<p :id="aListener" />'),
{
listeners: {
aListener
}
}
)

expect(wrapper.vm.$listeners.aListener.fns).to.equal(aListener)
})

itDoNotRunIf(
isRunningPhantomJS || !listenersSupported,
'passes listeners to functional components', () => {
const TestComponent = {
render (h, ctx) {
ctx.listeners.aListener()
ctx.listeners.bListener()
return h('div')
},
functional: true
}
)

expect(wrapper.vm.$listeners.aListener.fns).to.equal(aListener)
expect(wrapper.vm.$listeners.aListener.fns).to.equal(aListener)
})
mountingMethod(
TestComponent,
{
context: {
on: {
bListener () {}
}
},
listeners: {
aListener () {}
}
}
)
})

itDoNotRunIf(
vueVersion < 2.5,
Expand Down

0 comments on commit 7a1a49e

Please sign in to comment.