Skip to content

Commit e414150

Browse files
committed
Better default slot detection
1 parent b39d9c1 commit e414150

File tree

1 file changed

+65
-58
lines changed

1 file changed

+65
-58
lines changed

src/components/FlowForm.vue

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
140140
export default {
141141
name: 'FlowForm',
142+
142143
components: {
143144
FlowFormQuestion
144145
},
@@ -301,75 +302,81 @@
301302
descriptionLink: LinkOption
302303
}
303304
304-
this
305-
.$slots
306-
.default()[0]
307-
.children
308-
.filter(q => q.type && q.type.name.indexOf('Question') !== -1)
309-
.forEach(q => {
310-
const props = q.props
311-
const componentInstance = this.getInstance(props.id)
312-
let model = new QuestionModel()
313-
314-
if (componentInstance.question !== null) {
315-
model = componentInstance.question
316-
}
317-
318-
if (props.modelValue) {
319-
model.answer = props.modelValue
320-
}
305+
const defaultSlot = this.$slots.default()
306+
let children = null
321307
322-
Object.keys(model).forEach(key => {
323-
if (props[key] !== undefined) {
324-
if (typeof model[key] === 'boolean') {
325-
model[key] = props[key] !== false
326-
} else if (key in classMap) {
327-
const
328-
classReference = classMap[key],
329-
options = []
330-
331-
props[key].forEach(option => {
332-
const instance = new classReference()
333-
334-
Object.keys(instance).forEach(instanceKey => {
335-
if (option[instanceKey] !== undefined) {
336-
instance[instanceKey] = option[instanceKey]
337-
}
338-
})
308+
if (defaultSlot && defaultSlot.length) {
309+
children = defaultSlot[0].children
310+
}
339311
340-
options.push(instance)
341-
})
312+
if (children) {
313+
children
314+
.filter(q => q.type && q.type.name.indexOf('Question') !== -1)
315+
.forEach(q => {
316+
const props = q.props
317+
const componentInstance = this.getInstance(props.id)
318+
let model = new QuestionModel()
342319
343-
model[key] = options
344-
} else {
345-
switch(key) {
346-
case 'type':
347-
if (Object.values(QuestionType).indexOf(props[key]) !== -1) {
348-
model[key] = props[key]
349-
} else {
350-
for (const questionTypeKey in QuestionType) {
351-
if (questionTypeKey.toLowerCase() === props[key].toLowerCase()) {
352-
model[key] = QuestionType[questionTypeKey]
353-
break
320+
if (componentInstance.question !== null) {
321+
model = componentInstance.question
322+
}
323+
324+
if (props.modelValue) {
325+
model.answer = props.modelValue
326+
}
327+
328+
Object.keys(model).forEach(key => {
329+
if (props[key] !== undefined) {
330+
if (typeof model[key] === 'boolean') {
331+
model[key] = props[key] !== false
332+
} else if (key in classMap) {
333+
const
334+
classReference = classMap[key],
335+
options = []
336+
337+
props[key].forEach(option => {
338+
const instance = new classReference()
339+
340+
Object.keys(instance).forEach(instanceKey => {
341+
if (option[instanceKey] !== undefined) {
342+
instance[instanceKey] = option[instanceKey]
343+
}
344+
})
345+
346+
options.push(instance)
347+
})
348+
349+
model[key] = options
350+
} else {
351+
switch(key) {
352+
case 'type':
353+
if (Object.values(QuestionType).indexOf(props[key]) !== -1) {
354+
model[key] = props[key]
355+
} else {
356+
for (const questionTypeKey in QuestionType) {
357+
if (questionTypeKey.toLowerCase() === props[key].toLowerCase()) {
358+
model[key] = QuestionType[questionTypeKey]
359+
break
360+
}
354361
}
355362
}
356-
}
357-
break
363+
break
358364
359-
default:
360-
model[key] = props[key]
361-
break
365+
default:
366+
model[key] = props[key]
367+
break
368+
}
362369
}
363370
}
364-
}
365-
})
371+
})
366372
367-
componentInstance.question = model
373+
componentInstance.question = model
368374
369-
model.resetOptions()
375+
model.resetOptions()
370376
371-
questions.push(model)
372-
})
377+
questions.push(model)
378+
})
379+
}
373380
}
374381
375382
return questions

0 commit comments

Comments
 (0)