Skip to content

Commit

Permalink
feat: enabled slots option to take class components (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekosaur authored and eddyerburgh committed Jul 15, 2018
1 parent 65449b3 commit 4916fed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
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)
}
})
})

0 comments on commit 4916fed

Please sign in to comment.