Skip to content

Commit 7a04ff4

Browse files
38elementseddyerburgh
authored andcommitted
feat: support scopedSlots mounting option for functional component (#893)
1 parent 73980c4 commit 7a04ff4

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { throwError } from 'shared/util'
44
import { validateSlots } from './validate-slots'
55
import { createSlotVNodes } from './create-slot-vnodes'
6+
import createScopedSlots from './create-scoped-slots'
67

78
export default function createFunctionalComponent (
89
component: Component,
@@ -15,11 +16,15 @@ export default function createFunctionalComponent (
1516
validateSlots(mountingOptions.slots)
1617
}
1718

19+
const data = mountingOptions.context ||
20+
component.FunctionalRenderContext || {}
21+
data.scopedSlots = createScopedSlots(mountingOptions.scopedSlots)
22+
1823
return {
1924
render (h: Function) {
2025
return h(
2126
component,
22-
mountingOptions.context || component.FunctionalRenderContext,
27+
data,
2328
(mountingOptions.context &&
2429
mountingOptions.context.children &&
2530
mountingOptions.context.children.map(

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

+44
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,50 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
8888
}
8989
)
9090

91+
itDoNotRunIf(
92+
vueVersion < 2.5,
93+
'mounts component scoped slots in render function which exists in functional component',
94+
() => {
95+
const destructuringWrapper = mountingMethod(
96+
{
97+
functional: true,
98+
render: function (createElement, context) {
99+
return context.data.scopedSlots.default({
100+
index: 1,
101+
item: 'foo'
102+
})
103+
}
104+
},
105+
{
106+
scopedSlots: {
107+
default:
108+
'<template slot-scope="{ index, item }"><p>{{index}},{{item}}</p></template>'
109+
}
110+
}
111+
)
112+
expect(destructuringWrapper.html()).to.equal('<p>1,foo</p>')
113+
114+
const notDestructuringWrapper = mountingMethod(
115+
{
116+
functional: true,
117+
render: function (createElement, context) {
118+
return context.data.scopedSlots.named({
119+
index: 1,
120+
item: 'foo'
121+
})
122+
}
123+
},
124+
{
125+
scopedSlots: {
126+
named:
127+
'<p slot-scope="foo">{{foo.index}},{{foo.item}}</p>'
128+
}
129+
}
130+
)
131+
expect(notDestructuringWrapper.html()).to.equal('<p>1,foo</p>')
132+
}
133+
)
134+
91135
itDoNotRunIf(
92136
vueVersion < 2.5,
93137
'mounts component scoped slots',

0 commit comments

Comments
 (0)