Skip to content

Commit

Permalink
Merge pull request #86 from indec-it/fix/cleanSubQuestions
Browse files Browse the repository at this point in the history
fix(useSubQuestions): clean sub questions
  • Loading branch information
maximilianoforlenza authored Sep 28, 2023
2 parents 4a5db05 + c98f5b9 commit 3ba4cfd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@indec/form-builder",
"version": "2.4.6",
"version": "2.4.7",
"description": "Form builder",
"main": "index.js",
"private": false,
Expand Down
16 changes: 9 additions & 7 deletions src/components/SubQuestions/SubQuestions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import {Field} from 'formik';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';

import useSubQuestions from '@/hooks/useSubQuestions';
import subQuestionPropTypes from '@/utils/propTypes/subQuestion';
Expand All @@ -10,24 +11,25 @@ import getQuestionProps from '@/utils/getQuestionProps';

function SubQuestions({values, subQuestions, name, ...props}) {
const parentName = name.split('.')[2];
const {selectedQuestions} = useSubQuestions({subQuestions, value: {[parentName]: values}, name: parentName});
return (
<>
const {selectedQuestions} = useSubQuestions({
subQuestions, value: {[parentName]: values}, name: parentName, specificationsPathName: name
});
return selectedQuestions.length > 0 ? (
<Stack spacing={2} sx={{width: '100%'}}>
{selectedQuestions.map(subQuestion => (
<Box key={subQuestion.id} mb={2}>
<Box key={subQuestion.id}>
<Field
{...getQuestionProps({question: subQuestion, name, values}).props}
{...props}
component={getQuestionComponent(subQuestion.type)}
name={`${name}.${subQuestion.name}.answer.value`}
label={{text: subQuestion.label}}
placeholder={subQuestion.placeholder}
sx={{minWidth: '300px'}}
/>
</Box>
))}
</>
);
</Stack>
) : null;
}

SubQuestions.propTypes = {
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/__tests__/useSubQuestions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import {renderHook} from '@testing-library/react';

import useSubQuestions from '../useSubQuestions';

jest.mock('formik', () => ({
useFormikContext: jest.fn().mockReturnValue({
setFieldValue: jest.fn()
})
}));

const subQuestions = [
{
id: 1,
Expand Down
26 changes: 22 additions & 4 deletions src/hooks/useSubQuestions.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import {useEffect, useState} from 'react';
import {useFormikContext} from 'formik';

import {getSubQuestions} from '@/utils/buildQuestions';
import getNavigation from '@/utils/getNavigation';

const useSubQuestions = ({subQuestions, value, name}) => {
const useSubQuestions = ({subQuestions, value, name, specificationsPathName}) => {
const [selectedQuestions, setSelectedSubQuestions] = useState([]);
const {setFieldValue} = useFormikContext();

useEffect(() => {
const subQuestionsFiltered = subQuestions.filter(
const allSubQuestions = subQuestions.map(
subQuestion => {
const condition = getNavigation({
navigation: subQuestion.navigation, answers: value, questionType: subQuestion.type
});
return !condition;
return {...subQuestion, show: !condition};
}
);
setSelectedSubQuestions(subQuestionsFiltered);
const hiddenSubQuestions = allSubQuestions.filter(subQuestion => !subQuestion.show);
const showSubQuestions = allSubQuestions.filter(subQuestion => subQuestion.show);
if (hiddenSubQuestions.length > 0) {
setFieldValue(
specificationsPathName,
{...value?.[name]?.answer?.specifications, ...getSubQuestions(hiddenSubQuestions)}
);
}
if (showSubQuestions.length > 0) {
showSubQuestions.forEach(subQuestion => {
// eslint-disable-next-line no-param-reassign
delete subQuestion.show;
});
setSelectedSubQuestions(showSubQuestions);
}
}, [value?.[name]?.answer?.value]);

return {selectedQuestions};
Expand Down

0 comments on commit 3ba4cfd

Please sign in to comment.