Skip to content

Commit

Permalink
feat(builder): add dependent question to multicheckboxes (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
masopego authored Sep 2, 2022
1 parent dd16c69 commit 1d4e366
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,21 @@ const FormBuilder = ({
/>
),
multiple_checkboxes: (
<QuestionMultipleCheckboxes
useForm={useFormObj}
question={question}
form={form}
/>
<>
<QuestionMultipleCheckboxes
useForm={useFormObj}
question={question}
form={form}
/>
{question.dependentQuestions &&
question.dependentQuestions.map(
ConditionalQuestion(
question.dependentQuestions,
question.name,
question.type
)
)}
</>
),
markdown: (
<QuestionMarkdown
Expand All @@ -153,7 +163,7 @@ const FormBuilder = ({
}
}

function ConditionalQuestion(question, name) {
function ConditionalQuestion(question, name, preQuestionType) {
return (dependentQuestion, i) => {
const nestedQuestion = dependentQuestion && dependentQuestion.question

Expand All @@ -166,7 +176,7 @@ const FormBuilder = ({
? conditionValue.value
: conditionValue

return getConditions().includes(getFormattedValue()) ? (
const renderComponent = () => (
<React.Fragment key={i}>
{
QuestionsMap(dependentQuestion.question)[
Expand All @@ -183,7 +193,23 @@ const FormBuilder = ({
)
: null}
</React.Fragment>
) : null
)

if (preQuestionType === 'multiple_checkboxes') {
const getMultiFormattedValue = () =>
conditionValue && conditionValue.value
? conditionValue.value
: conditionValue || []

return getMultiFormattedValue() &&
getMultiFormattedValue().some((e) => e === getConditions())
? renderComponent()
: null
}

return getConditions().includes(getFormattedValue())
? renderComponent()
: null
}
}

Expand Down

0 comments on commit 1d4e366

Please sign in to comment.