From 94bc24f93f757a22fcc514d749f7c3c3569d18aa Mon Sep 17 00:00:00 2001 From: Maximiliano Forlenza Date: Sat, 6 Jan 2024 19:14:44 -0300 Subject: [PATCH] feat: transform section to create subQuestions --- package.json | 2 +- src/components/FormBuilder/FormBuilder.js | 9 +- .../SectionHeader/SectionHeader.js | 1 - src/components/FormBuilder/useFormBuilder.js | 84 ++++++++++++++++++- 4 files changed, 86 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index b378efb..170e1ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@indec/form-builder", - "version": "2.4.7", + "version": "2.5.0", "description": "Form builder", "main": "index.js", "private": false, diff --git a/src/components/FormBuilder/FormBuilder.js b/src/components/FormBuilder/FormBuilder.js index 88367f5..268c070 100644 --- a/src/components/FormBuilder/FormBuilder.js +++ b/src/components/FormBuilder/FormBuilder.js @@ -25,7 +25,8 @@ function FormBuilder({section, isLastSection, page, onSubmit, onPrevious, compon addNewSection, handleOpenModal, handleShowSurvey, - setOpenModal + setOpenModal, + transformedSection } = useFormBuilder({isReadOnly, section, initialValues}); return ( handleShowSurvey(currentSection.id, false)} onDelete={() => handleOpenModal(modals.CONFIRM_DELETE_SECTION_MODAL, currentSection.id)} sectionsLength={values[section.name].length} - section={section} + section={transformedSection} values={currentSection} isReadOnly={isReadOnly} isValid={validateSchema.isValidSync({[section.name]: values?.[section.name]})} /> )} {showSurvey === currentSection.id && ( - + diff --git a/src/components/FormBuilder/SectionHeader/SectionHeader.js b/src/components/FormBuilder/SectionHeader/SectionHeader.js index c877069..018d002 100644 --- a/src/components/FormBuilder/SectionHeader/SectionHeader.js +++ b/src/components/FormBuilder/SectionHeader/SectionHeader.js @@ -38,7 +38,6 @@ function SectionHeader({section, sectionsLength, onView, onEdit, onDelete, value { const [showSurvey, setShowSurvey] = useState(); const [selectedSectionId, setSelectedSelectionId] = useState(); const [openModal, setOpenModal] = useState(); - const {initialValues: formInitialValues} = useSectionInitialValues(initialValues, section); - const {errorSchema: validateSchema, warningSchema} = getSchemas({section}); const handleShowSurvey = (sectionId, readOnly) => { setShowSurvey(sectionId); @@ -40,6 +40,81 @@ const useFormBuilder = ({isReadOnly, section, initialValues}) => { setValues(newValues); }; + const transformedSection = useMemo(() => { + const questions = section.questions.map(question => { + if ( + [questionTypes.DROPDOWN, questionTypes.RADIO, questionTypes.CHECKBOX].includes(question.type) && + question.allOptionsNeedSpecification + ) { + return { + ...question, + subQuestions: question.options.map((option, index) => ({ + id: index + 1, + type: question.metadata.specification.type, + optionId: option.id, + label: question.metadata.specification.label, + name: `S${section.id}P${question.id}SQ${index + 1}`, + userVarName: `${question.userVarName}_${option.value}`, + validations: [ + { + id: 1, + rules: [ + { + id: 1, + conditions: [ + { + id: 1, + section: 'S2', + question: question.name, + value: '', + type: + question.type === questionTypes.CHECKBOX ? validationTypes.NOT_INCLUDES : validationTypes.NOT_EQUAL + }, + { + id: 2, + section: 'S2', + question: `S${section.id}P${question.id}SQ${index + 1}`, + value: '', + type: validationTypes.EQUAL + } + ] + } + ], + message: {text: 'Debe completar el campo', type: 'error'} + } + ], + navigation: [ + { + id: 1, + rules: [ + { + id: 1, + conditions: [ + { + id: 1, + section: 'S2', + question: question.name, + value: option.value, + type: + question.type === questionTypes.CHECKBOX ? validationTypes.NOT_INCLUDES : validationTypes.NOT_EQUAL + } + ] + } + ], + action: 'hide' + } + ] + })) + }; + } + return question; + }); + return {...section, questions}; + }, [section]); + + const {initialValues: formInitialValues} = useSectionInitialValues(initialValues, transformedSection); + const {errorSchema: validateSchema, warningSchema} = getSchemas({section: transformedSection}); + return { readOnlyMode, showSurvey, @@ -52,7 +127,8 @@ const useFormBuilder = ({isReadOnly, section, initialValues}) => { addNewSection, handleOpenModal, handleShowSurvey, - setOpenModal + setOpenModal, + transformedSection }; };