From 673df7a2473812be319231ec21eedc6d3f4246d7 Mon Sep 17 00:00:00 2001 From: Laurent Caouissin Date: Wed, 8 Nov 2023 21:33:32 +0100 Subject: [PATCH] fix: hasResponse for table --- queen-v2/src/utils/components/deduceState.js | 28 ++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/queen-v2/src/utils/components/deduceState.js b/queen-v2/src/utils/components/deduceState.js index c7a48e1f..51a76903 100644 --- a/queen-v2/src/utils/components/deduceState.js +++ b/queen-v2/src/utils/components/deduceState.js @@ -9,6 +9,27 @@ import { COMP_TYPE_TEXTAREA, } from 'utils/constants'; +export const extractComponentFromTableLine = line => { + if (Array.isArray(line)) + return line.reduce((result, cell) => { + if (typeof cell === 'object' && 'componentType' in cell) return [...result, cell]; + return result; + }, []); + return []; +}; + +export const extractComponentFromTable = component => { + const { componentType } = component; + if (COMP_TYPE_TABLE !== componentType) return [component]; + const { body } = component; + if (Array.isArray(body)) { + return body.reduce((result, line) => { + return [...result, ...extractComponentFromTableLine(line)]; + }, []); + } + return []; +}; + export const componentHasResponse = component => { if (component === undefined) return false; @@ -17,9 +38,12 @@ export const componentHasResponse = component => { const { componentType } = component; switch (componentType) { - case COMP_TYPE_CHECK_BOX_GROUP: case COMP_TYPE_TABLE: - return Object.values(component.value).some(val => val !== null); + const tableComponents = extractComponentFromTable(component); + return tableComponents.reduce((result, comp) => { + return componentHasResponse(comp) || result; + }, false); + case COMP_TYPE_CHECK_BOX_GROUP: case COMP_TYPE_INPUT_NUMBER: case COMP_TYPE_CHECK_BOX_BOOLEAN: case COMP_TYPE_TEXTAREA: