Skip to content
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

fix: enabled slots option to take class components #826

Merged
merged 1 commit into from
Jul 15, 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
32 changes: 12 additions & 20 deletions packages/create-instance/validate-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import { throwError } from 'shared/util'
import { compileToFunctions } from 'vue-template-compiler'
import { isVueComponent } from '../shared/validators'

function isValidSlot (slot: any): boolean {
return (
Array.isArray(slot) ||
(slot !== null && typeof slot === 'object') ||
isVueComponent(slot) ||
typeof slot === 'string'
)
}
Expand All @@ -23,24 +23,16 @@ function requiresTemplateCompiler (slot: any): void {

export function validateSlots (slots: SlotsObject): void {
Object.keys(slots).forEach(key => {
if (!isValidSlot(slots[key])) {
throwError(
`slots[key] must be a Component, string or an array ` + `of Components`
)
}
const slot = Array.isArray(slots[key]) ? slots[key] : [slots[key]]

requiresTemplateCompiler(slots[key])

if (Array.isArray(slots[key])) {
slots[key].forEach(slotValue => {
if (!isValidSlot(slotValue)) {
throwError(
`slots[key] must be a Component, string or an array ` +
`of Components`
)
}
requiresTemplateCompiler(slotValue)
})
}
slot.forEach(slotValue => {
if (!isValidSlot(slotValue)) {
throwError(
`slots[key] must be a Component, string or an array ` +
`of Components`
)
}
requiresTemplateCompiler(slotValue)
})
})
}
22 changes: 22 additions & 0 deletions test/specs/mounting-options/slots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,26 @@ describeWithMountingMethods('options.slots', mountingMethod => {
wrapper.find('div').trigger('click')
}
)

it('mounts component with default slot if passed class component in slot object', () => {
const wrapper = mountingMethod(ComponentWithSlots, {
slots: { default: ComponentAsAClass }
})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<div></div>')
} else {
expect(wrapper.contains(ComponentAsAClass)).to.equal(true)
}
})

it('mounts component with default slot if passed class component in array in slot object', () => {
const wrapper = mountingMethod(ComponentWithSlots, {
slots: { default: [ComponentAsAClass] }
})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<div></div>')
} else {
expect(wrapper.contains(ComponentAsAClass)).to.equal(true)
}
})
})