Skip to content

Commit 995a57d

Browse files
committed
fix: improve slots option
1 parent d0a9c16 commit 995a57d

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

packages/create-instance/create-functional-component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { throwError } from 'shared/util'
44
import { validateSlots } from './validate-slots'
5-
import { createSlotVNodes } from './add-slots'
5+
import { createSlotVNodes } from './create-slot-vnodes'
66

77
export default function createFunctionalComponent (
88
component: Component,

packages/create-instance/create-instance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22

3-
import { createSlotVNodes } from './add-slots'
3+
import { createSlotVNodes } from './create-slot-vnodes'
44
import addMocks from './add-mocks'
55
import { addEventLogger } from './log-events'
66
import { createComponentStubs } from 'shared/stub-components'

packages/create-instance/add-slots.js renamed to packages/create-instance/create-slot-vnodes.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22

3+
import Vue from 'vue'
34
import { compileToFunctions } from 'vue-template-compiler'
45

56
function startsWithTag (str: SlotValue): boolean {
@@ -18,7 +19,17 @@ function createVNodesForSlot (
1819
const el =
1920
typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue
2021

21-
const vnode = h(el)
22+
let vnode = h(el)
23+
if (typeof slotValue === 'string') {
24+
const vue = new Vue()
25+
try {
26+
// $FlowIgnore
27+
vnode = el.render.call(vue._renderProxy, h)
28+
vnode = h(vnode.tag, vnode.data || {}, vnode.children)
29+
} catch (e) {
30+
}
31+
}
32+
2233
vnode.data.slot = name
2334
return vnode
2435
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div foo="bar"><span baz="qux">{{ quux }}</span></div>
3+
</template>
4+
5+
<script>
6+
export default{
7+
name: 'component-with-parent-name',
8+
data () {
9+
return {
10+
quux: 'quux'
11+
}
12+
},
13+
mounted () {
14+
this.$parent.childName = this.$options.name
15+
}
16+
}
17+
</script>

test/specs/mounting-options/slots.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { compileToFunctions } from 'vue-template-compiler'
22
import Component from '~resources/components/component.vue'
33
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
44
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
5+
import ComponentWithParentName from '~resources/components/component-with-parent-name.vue'
56
import { describeWithMountingMethods, vueVersion } from '~resources/utils'
67
import { itSkipIf, itDoNotRunIf } from 'conditional-specs'
8+
import { mount } from '~vue/test-utils'
79

810
describeWithMountingMethods('options.slots', mountingMethod => {
911
it('mounts component with default slot if passed component in slot object', () => {
@@ -546,4 +548,32 @@ describeWithMountingMethods('options.slots', mountingMethod => {
546548
wrapper.find('div').trigger('click')
547549
}
548550
)
551+
552+
itDoNotRunIf(
553+
mountingMethod.name === 'renderToString',
554+
'sets a component which can access the parent component',
555+
() => {
556+
const wrapperComponent = mount(
557+
{
558+
name: 'parentComponent',
559+
template: '<div><slot /></div>',
560+
data () {
561+
return {
562+
childName: ''
563+
}
564+
}
565+
},
566+
{
567+
components: {
568+
ComponentWithParentName
569+
},
570+
slots: {
571+
default: '<component-with-parent-name />'
572+
}
573+
}
574+
)
575+
expect(wrapperComponent.vm.childName).to.equal('component-with-parent-name')
576+
expect(wrapperComponent.html()).to.equal('<div><div foo="bar"><span baz="qux">quux</span></div></div>')
577+
}
578+
)
549579
})

0 commit comments

Comments
 (0)