Skip to content

Commit 4916fed

Browse files
nekosaureddyerburgh
authored andcommitted
feat: enabled slots option to take class components (#826)
1 parent 65449b3 commit 4916fed

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

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

+12-20
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import { throwError } from 'shared/util'
44
import { compileToFunctions } from 'vue-template-compiler'
5+
import { isVueComponent } from '../shared/validators'
56

67
function isValidSlot (slot: any): boolean {
78
return (
8-
Array.isArray(slot) ||
9-
(slot !== null && typeof slot === 'object') ||
9+
isVueComponent(slot) ||
1010
typeof slot === 'string'
1111
)
1212
}
@@ -23,24 +23,16 @@ function requiresTemplateCompiler (slot: any): void {
2323

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

32-
requiresTemplateCompiler(slots[key])
33-
34-
if (Array.isArray(slots[key])) {
35-
slots[key].forEach(slotValue => {
36-
if (!isValidSlot(slotValue)) {
37-
throwError(
38-
`slots[key] must be a Component, string or an array ` +
39-
`of Components`
40-
)
41-
}
42-
requiresTemplateCompiler(slotValue)
43-
})
44-
}
28+
slot.forEach(slotValue => {
29+
if (!isValidSlot(slotValue)) {
30+
throwError(
31+
`slots[key] must be a Component, string or an array ` +
32+
`of Components`
33+
)
34+
}
35+
requiresTemplateCompiler(slotValue)
36+
})
4537
})
4638
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,26 @@ describeWithMountingMethods('options.slots', mountingMethod => {
546546
wrapper.find('div').trigger('click')
547547
}
548548
)
549+
550+
it('mounts component with default slot if passed class component in slot object', () => {
551+
const wrapper = mountingMethod(ComponentWithSlots, {
552+
slots: { default: ComponentAsAClass }
553+
})
554+
if (mountingMethod.name === 'renderToString') {
555+
expect(wrapper).contains('<div></div>')
556+
} else {
557+
expect(wrapper.contains(ComponentAsAClass)).to.equal(true)
558+
}
559+
})
560+
561+
it('mounts component with default slot if passed class component in array in slot object', () => {
562+
const wrapper = mountingMethod(ComponentWithSlots, {
563+
slots: { default: [ComponentAsAClass] }
564+
})
565+
if (mountingMethod.name === 'renderToString') {
566+
expect(wrapper).contains('<div></div>')
567+
} else {
568+
expect(wrapper.contains(ComponentAsAClass)).to.equal(true)
569+
}
570+
})
549571
})

0 commit comments

Comments
 (0)