Skip to content

fix: render all slots inside a <template> vnode #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions packages/create-instance/create-slot-vnodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,32 @@ import { compileToFunctions } from 'vue-template-compiler'

function createVNodes (
vm: Component,
slotValue: string
slotValue: string,
name
): Array<VNode> {
const el = compileToFunctions(`<div>${slotValue}</div>`)
const el = compileToFunctions(
`<div><template slot=${name}>${slotValue}</template></div>`
)
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
const _staticTrees = vm._renderProxy._staticTrees
vm._renderProxy._staticTrees = []
vm._renderProxy.$options.staticRenderFns = el.staticRenderFns
const vnode = el.render.call(vm._renderProxy, vm.$createElement)
vm._renderProxy.$options.staticRenderFns = _staticRenderFns
vm._renderProxy._staticTrees = _staticTrees
return vnode.children
return vnode.children[0]
}

function createVNodesForSlot (
vm: Component,
slotValue: SlotValue,
name: string,
): VNode | Array<VNode> {
let vnode
if (typeof slotValue === 'string') {
const vnodes = createVNodes(vm, slotValue)
if (vnodes.length > 1) {
return vnodes
}
vnode = vnodes[0]
} else {
vnode = vm.$createElement(slotValue)
}
if (vnode.data) {
vnode.data.slot = name
} else {
vnode.data = { slot: name }
return createVNodes(vm, slotValue, name)
}
const vnode = vm.$createElement(slotValue)
;(vnode.data || (vnode.data = {})).slot = name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this line do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sets vnode.data.slot to equal the slot name. It's shorthand for:

if(!vnode.data) {
  vnode.data = {}
}
vnode.data.slot = name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! haven't seen this syntax before. I take it the preceeding ; is required.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's is one of the drawbacks of not including semi-colons, there are a few other cases where no semi-colons will cause errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

Code looks good outside that, good job.

return vnode
}

Expand Down
8 changes: 4 additions & 4 deletions test/specs/mounting-options/slots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,14 @@ describeWithMountingMethods('options.slots', mountingMethod => {
it('mounts component with default and named text slot', () => {
const wrapper = mountingMethod(ComponentWithSlots, {
slots: {
default: 'hello,',
footer: '<template>world</template>'
footer: 'world',
default: 'hello,'
}
})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('hello,world')
expect(wrapper).contains('hello,</main> <footer>world')
} else {
expect(wrapper.text()).to.contain('hello,world')
expect(wrapper.text()).to.contain('hello, world')
}
})

Expand Down