Skip to content

Commit

Permalink
fix: improve slots option
Browse files Browse the repository at this point in the history
  • Loading branch information
38elements committed Jul 8, 2018
1 parent d0a9c16 commit f9eb000
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/create-instance/create-functional-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { throwError } from 'shared/util'
import { validateSlots } from './validate-slots'
import { createSlotVNodes } from './add-slots'
import { createSlotVNodes } from './create-slot-vnodes'

export default function createFunctionalComponent (
component: Component,
Expand Down
2 changes: 1 addition & 1 deletion packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import { createSlotVNodes } from './add-slots'
import { createSlotVNodes } from './create-slot-vnodes'
import addMocks from './add-mocks'
import { addEventLogger } from './log-events'
import { createComponentStubs } from 'shared/stub-components'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow

import Vue from 'vue'
import { compileToFunctions } from 'vue-template-compiler'

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

const vnode = h(el)
let vnode = h(el)
if (typeof slotValue === 'string') {
const vue = new Vue()
try {
// $FlowIgnore
vnode = el.render.call(vue._renderProxy, h)
vnode = h(vnode.tag, vnode.data || {}, vnode.children)
} catch (e) {
}
}

vnode.data.slot = name
return vnode
}
Expand Down
17 changes: 17 additions & 0 deletions test/resources/components/component-with-parent-name.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<div foo="bar"><span baz="qux">{{ quux }}</span></div>
</template>

<script>
export default{
name: 'component-with-parent-name',
data () {
return {
quux: 'quux'
}
},
mounted () {
this.$parent.childName = this.$options.name
}
}
</script>
26 changes: 26 additions & 0 deletions test/specs/mounting-options/slots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { compileToFunctions } from 'vue-template-compiler'
import Component from '~resources/components/component.vue'
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import ComponentWithParentName from '~resources/components/component-with-parent-name.vue'
import { describeWithMountingMethods, vueVersion } from '~resources/utils'
import { itSkipIf, itDoNotRunIf } from 'conditional-specs'
import { mount } from '~vue/test-utils'

describeWithMountingMethods('options.slots', mountingMethod => {
it('mounts component with default slot if passed component in slot object', () => {
Expand Down Expand Up @@ -546,4 +548,28 @@ describeWithMountingMethods('options.slots', mountingMethod => {
wrapper.find('div').trigger('click')
}
)

it('sets a component which can access the parent component', () => {
const wrapperComponent = mount(
{
name: 'parentComponent',
template: '<div><slot /></div>',
data () {
return {
childName: ''
}
}
},
{
components: {
ComponentWithParentName
},
slots: {
default: '<component-with-parent-name />'
}
}
)
expect(wrapperComponent.vm.childName).to.equal('component-with-parent-name')
expect(wrapperComponent.html()).to.equal('<div><div foo="bar"><span baz="qux">quux</span></div></div>')
})
})

0 comments on commit f9eb000

Please sign in to comment.