Skip to content

Commit e1d576b

Browse files
fix(slots): functional component text slots
1 parent bc736fb commit e1d576b

File tree

4 files changed

+33
-40
lines changed

4 files changed

+33
-40
lines changed

Diff for: flow/options.flow.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare type Options = {
44
propsData?: Object,
55
mocks?: Object,
66
methods?: Object,
7-
slots?: Object,
7+
slots?: SlotsObject,
88
scopedSlots?: Object,
99
localVue?: Component,
1010
provide?: Object,

Diff for: packages/create-instance/add-slots.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function startsWithTag (str) {
66
return str && str.trim()[0] === '<'
77
}
88

9-
function createVNodesForSlot (
9+
export function createVNodesForSlot (
1010
h: Function,
1111
slotValue: SlotValue,
1212
name: string
@@ -30,12 +30,17 @@ export function createSlotVNodes (
3030
return Object.keys(slots).reduce((acc, key) => {
3131
const content = slots[key]
3232
if (Array.isArray(content)) {
33-
const nodes = content.reduce((accInner, slotDef) => {
34-
return accInner.concat(createVNodesForSlot(h, slotDef, key))
35-
}, [])
33+
const nodes = content.map(slotDef => createVNodesForSlot(h, slotDef, key))
3634
return acc.concat(nodes)
37-
} else {
38-
return acc.concat(createVNodesForSlot(h, content, key))
3935
}
36+
37+
return acc.concat(createVNodesForSlot(h, content, key))
4038
}, [])
4139
}
40+
41+
export function createFunctionalSlotVNodes (
42+
h: Function,
43+
slots: SlotsObject
44+
) {
45+
return createSlotVNodes(h, slots)
46+
}

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

+2-33
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,8 @@
11
// @flow
22

3-
import { compileToFunctions } from 'vue-template-compiler'
43
import { throwError } from 'shared/util'
54
import { validateSlots } from './validate-slots'
6-
7-
function createFunctionalSlots (slots = {}, h) {
8-
if (Array.isArray(slots.default)) {
9-
return slots.default.map(h)
10-
}
11-
12-
if (typeof slots.default === 'string') {
13-
return [h(compileToFunctions(slots.default))]
14-
}
15-
const children = []
16-
Object.keys(slots).forEach(slotType => {
17-
if (Array.isArray(slots[slotType])) {
18-
slots[slotType].forEach(slot => {
19-
const component =
20-
typeof slot === 'string' ? compileToFunctions(slot) : slot
21-
const newSlot = h(component)
22-
newSlot.data.slot = slotType
23-
children.push(newSlot)
24-
})
25-
} else {
26-
const component =
27-
typeof slots[slotType] === 'string'
28-
? compileToFunctions(slots[slotType])
29-
: slots[slotType]
30-
const slot = h(component)
31-
slot.data.slot = slotType
32-
children.push(slot)
33-
}
34-
})
35-
return children
36-
}
5+
import { createFunctionalSlotVNodes } from './add-slots'
376

387
export default function createFunctionalComponent (
398
component: Component,
@@ -56,7 +25,7 @@ export default function createFunctionalComponent (
5625
mountingOptions.context.children.map(
5726
x => (typeof x === 'function' ? x(h) : x)
5827
)) ||
59-
createFunctionalSlots(mountingOptions.slots, h)
28+
createFunctionalSlotVNodes(h, mountingOptions.slots)
6029
)
6130
},
6231
name: component.name,

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

+19
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,25 @@ describeWithMountingMethods('options.slots', mountingMethod => {
235235
}
236236
})
237237

238+
it('mounts functional component with text slot', () => {
239+
const TestComponent = {
240+
name: 'component-with-slots',
241+
functional: true,
242+
render: (h, ctx) => h('div', ctx.data, [ctx.slots().default, ctx.slots().header])
243+
}
244+
const wrapper = mountingMethod(TestComponent, {
245+
slots: {
246+
default: 'hello,',
247+
header: 'world'
248+
}
249+
})
250+
if (mountingMethod.name === 'renderToString') {
251+
expect(wrapper).contains('hello,world')
252+
} else {
253+
expect(wrapper.text()).to.contain('hello,world')
254+
}
255+
})
256+
238257
it('mounts component with named slot if passed component in slot object', () => {
239258
const wrapper = mountingMethod(ComponentWithSlots, {
240259
slots: {

0 commit comments

Comments
 (0)