diff --git a/package.json b/package.json index 50a9677..cc0deb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@indec/form-builder", - "version": "3.1.0", + "version": "3.1.1", "description": "Form builder", "main": "index.js", "private": false, diff --git a/src/components/FormBuilder/FormBuilder.stories.js b/src/components/FormBuilder/FormBuilder.stories.js index 0893ead..7af8114 100644 --- a/src/components/FormBuilder/FormBuilder.stories.js +++ b/src/components/FormBuilder/FormBuilder.stories.js @@ -17,7 +17,7 @@ export default { const sections = [ { name: 'S2', - label: 'Seccion 1', + label: 'INGRESOS', questions: [ { id: 1, @@ -219,26 +219,7 @@ const sections = [ ], multiple: true, favorite: false, - validations: [ - { - id: 1, - rules: [ - { - id: 1, - conditions: [ - { - id: 1, - type: 'eq', - question: 'S2P4', - value: '', - section: 'S2' - } - ] - } - ], - message: {type: 'error', text: 'Debe completar el campo'} - } - ], + validations: [], navigation: [], subQuestions: [], metadata: {}, @@ -320,13 +301,12 @@ const sections = [ ], id: 2, userVarName: 'Ingreso', - introduction: 'Seleccione aquellos Items para los cuales tuvo Ingresos', - navigation: [] + introduction: 'Seleccione aquellos Items para los cuales tuvo Ingresos' }, { id: 1, name: 'S1', - label: 'Seccion 2', + label: 'EGRESOS', questions: [ { id: 1, @@ -609,178 +589,7 @@ const sections = [ id: 1 } ], - userVarName: 'Egresos', - navigation: [ - { - id: 1, - rules: [ - { - id: 1, - conditions: [ - { - id: 1, - question: 'S2P1', - value: '1', - type: 'eq', - section: 'S2' - } - ] - } - ], - action: 'hide' - } - ] - }, - { - id: 4, - name: 'S4', - label: 'Seccion 3', - questions: [ - { - id: 1, - label: 'Tipo de Transaccion', - name: 'S4P1', - number: '1', - type: 3, - options: [ - { - id: 1, - name: 'S1P1O1', - subOptions: [ - { - id: 1 - } - ], - label: '2.1 Manufatura sobre insumos fisicos', - value: '1', - needSpecification: false - }, - { - id: 2, - needSpecification: false, - label: '2.1.1 Bienes enviados al exterior', - value: '2' - }, - { - id: 3, - needSpecification: false, - label: '2.2 Mantenimiento y reparaciones', - value: '3' - } - ], - multiple: false, - favorite: false, - validations: [], - navigation: [], - subQuestions: [], - metadata: {}, - userVarName: 'Transaccion' - } - ], - multiple: false, - favorite: false, - interruption: { - name: 'S1I1', - interruptible: false, - reason: '', - options: [ - { - id: 1 - } - ] - }, - headers: [ - { - id: 1 - } - ], - userVarName: 'Egresos', - navigation: [ - { - id: 1, - rules: [ - { - id: 1, - conditions: [ - { - id: 1, - question: 'S2P1', - value: '1', - type: 'eq', - section: 'S2' - } - ] - } - ], - action: 'hide' - } - ] - }, - { - id: 3, - name: 'S3', - label: 'Seccion 4', - questions: [ - { - id: 1, - label: 'Tipo de Transaccion', - name: 'S3P1', - number: '1', - type: 3, - options: [ - { - id: 1, - name: 'S1P1O1', - subOptions: [ - { - id: 1 - } - ], - label: '2.1 Manufatura sobre insumos fisicos', - value: '1', - needSpecification: false - }, - { - id: 2, - needSpecification: false, - label: '2.1.1 Bienes enviados al exterior', - value: '2' - }, - { - id: 3, - needSpecification: false, - label: '2.2 Mantenimiento y reparaciones', - value: '3' - } - ], - multiple: false, - favorite: false, - validations: [], - navigation: [], - subQuestions: [], - metadata: {}, - userVarName: 'Transaccion' - } - ], - multiple: false, - favorite: false, - interruption: { - name: 'S1I1', - interruptible: false, - reason: '', - options: [ - { - id: 1 - } - ] - }, - headers: [ - { - id: 1 - } - ], - userVarName: 'Egresos', - navigation: [] + userVarName: 'Egresos' } ]; diff --git a/src/components/SubQuestions/SubQuestions.js b/src/components/SubQuestions/SubQuestions.js index 26eade3..0099020 100644 --- a/src/components/SubQuestions/SubQuestions.js +++ b/src/components/SubQuestions/SubQuestions.js @@ -17,6 +17,7 @@ function SubQuestions({values, subQuestions, name, ...props}) { name: parentName, specificationsPathName: name }); + return selectedQuestions.length > 0 ? ( {selectedQuestions.map(subQuestion => ( diff --git a/src/hooks/__tests__/useSubQuestions.test.js b/src/hooks/__tests__/useSubQuestions.test.js index 954af89..bcbb330 100644 --- a/src/hooks/__tests__/useSubQuestions.test.js +++ b/src/hooks/__tests__/useSubQuestions.test.js @@ -1090,6 +1090,7 @@ describe('useSubQuestions', () => { { id: 3, optionId: 3, + show: true, type: 1, label: 'Monto pais', name: 'S2P4SQ3', diff --git a/src/hooks/useSubQuestions.js b/src/hooks/useSubQuestions.js index 5851382..45a29a0 100644 --- a/src/hooks/useSubQuestions.js +++ b/src/hooks/useSubQuestions.js @@ -12,8 +12,9 @@ const useSubQuestions = ({subQuestions, value, name, specificationsPathName}) => const {sections, initialValues, section} = useForm(); useEffect(() => { - const allSubQuestions = subQuestions.map(subQuestion => { - const condition = getNavigation({ + const allSubQuestions = subQuestions.map(subQuestion => ({ + ...subQuestion, + show: getNavigation({ navigation: subQuestion.navigation, answers: value, section, @@ -21,9 +22,8 @@ const useSubQuestions = ({subQuestions, value, name, specificationsPathName}) => sections, questionName: name, isSubQuestion: true - }); - return {...subQuestion, show: condition.valid}; - }); + }).valid + })); const hiddenSubQuestions = allSubQuestions.filter(subQuestion => !subQuestion.show); const showSubQuestions = allSubQuestions.filter(subQuestion => subQuestion.show); if (hiddenSubQuestions.length > 0) { @@ -32,13 +32,7 @@ const useSubQuestions = ({subQuestions, value, name, specificationsPathName}) => ...getSubQuestions(hiddenSubQuestions) }); } - if (showSubQuestions.length > 0) { - showSubQuestions.forEach(subQuestion => { - // eslint-disable-next-line no-param-reassign - delete subQuestion.show; - }); - setSelectedSubQuestions(showSubQuestions); - } + setSelectedSubQuestions(showSubQuestions); }, [value?.[name]?.answer?.value]); return {selectedQuestions}; diff --git a/src/utils/__tests__/buildQuestions.test.js b/src/utils/__tests__/buildQuestions.test.js index aab4df5..4702221 100644 --- a/src/utils/__tests__/buildQuestions.test.js +++ b/src/utils/__tests__/buildQuestions.test.js @@ -626,6 +626,53 @@ describe('buildQuestions', () => { }); }); + describe('when the section has a Message question`s type', () => { + beforeEach(() => { + section = { + id: 1, + name: 'S1', + label: 'Sección 1', + questions: [ + { + id: 1, + label: 'Select a date', + name: 'S1P1', + number: '1', + type: 9, + metadata: {}, + options: [], + validations: [], + userVarName: 's1p1' + } + ], + interruption: { + name: 'S1I1', + interruptible: false, + reason: '', + options: [ + { + id: 1 + } + ] + }, + headers: [], + userVarName: 'S1' + }; + }); + + it('should return an array with `id` without `answer` section.question.name`', () => { + expect(buildQuestions(section)).toEqual({ + S1: [ + expect.objectContaining({ + S1P1: { + id: 1 + } + }) + ] + }); + }); + }); + describe('when the section has a question with subQuestions', () => { beforeEach(() => { section = { diff --git a/src/utils/buildQuestions.js b/src/utils/buildQuestions.js index 007c494..2c2c8f2 100644 --- a/src/utils/buildQuestions.js +++ b/src/utils/buildQuestions.js @@ -43,21 +43,25 @@ const buildQuestions = section => { } section.questions.forEach(question => { const {id} = question; - values[section.name][question.name] = {id, answer: {value: getValue(question)}}; - if (question.subQuestions && question.subQuestions.length > 0) { - values[section.name][question.name] = { - ...values[section.name][question.name], - answer: { - ...values[section.name][question.name].answer, - specifications: getSubQuestions(question.subQuestions) - } - }; - } - if (question.multiple) { - values[section.name][question.name] = { - ...values[section.name][question.name], - answer: [{id: 1, ...values[section.name][question.name].answer}] - }; + if (question.type === questionTypes.MESSAGE) { + values[section.name][question.name] = {id}; + } else { + values[section.name][question.name] = {id, answer: {value: getValue(question)}}; + if (question.subQuestions && question.subQuestions.length > 0) { + values[section.name][question.name] = { + ...values[section.name][question.name], + answer: { + ...values[section.name][question.name].answer, + specifications: getSubQuestions(question.subQuestions) + } + }; + } + if (question.multiple) { + values[section.name][question.name] = { + ...values[section.name][question.name], + answer: [{id: 1, ...values[section.name][question.name].answer}] + }; + } } }); values[section.name] = [values[section.name]]; diff --git a/src/utils/hasFormikErrorsAndWarnings.js b/src/utils/hasFormikErrorsAndWarnings.js index 1082e3f..d31dd62 100644 --- a/src/utils/hasFormikErrorsAndWarnings.js +++ b/src/utils/hasFormikErrorsAndWarnings.js @@ -4,9 +4,9 @@ const hasFormikErrorsAndWarnings = ({form, field, warnings = {}}) => { const warning = getIn(warnings, field.name); const error = getIn(form.errors, field.name); const touched = getIn(form.touched, field.name); - const formSubmittedOrTouched = form.submitCount > 0 && touched; + const formSubmittedOrTouched = form.submitCount > 0 || touched; return { - hasError: formSubmittedOrTouched || (touched && error) ? !!error : false, + hasError: formSubmittedOrTouched ? !!error : false, error, hasWarning: !!warning, warning