Skip to content

Commit f9eb000

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

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

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

+1-1
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,

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

+1-1
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'

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

+12-1
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
}
+17
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>

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

+26
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,28 @@ describeWithMountingMethods('options.slots', mountingMethod => {
546548
wrapper.find('div').trigger('click')
547549
}
548550
)
551+
552+
it('sets a component which can access the parent component', () => {
553+
const wrapperComponent = mount(
554+
{
555+
name: 'parentComponent',
556+
template: '<div><slot /></div>',
557+
data () {
558+
return {
559+
childName: ''
560+
}
561+
}
562+
},
563+
{
564+
components: {
565+
ComponentWithParentName
566+
},
567+
slots: {
568+
default: '<component-with-parent-name />'
569+
}
570+
}
571+
)
572+
expect(wrapperComponent.vm.childName).to.equal('component-with-parent-name')
573+
expect(wrapperComponent.html()).to.equal('<div><div foo="bar"><span baz="qux">quux</span></div></div>')
574+
})
549575
})

0 commit comments

Comments
 (0)