diff --git a/src/components/renderer/field/fieldLogic.ts b/src/components/renderer/field/fieldLogic.ts index adc18c27..9060f9a2 100644 --- a/src/components/renderer/field/fieldLogic.ts +++ b/src/components/renderer/field/fieldLogic.ts @@ -64,30 +64,40 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon mode: sessionMode, patient, }, - ).then((result) => { - setValue(dependent.id, result); - // validate calculated value - const { errors, warnings } = validateFieldValue(dependent, result, context.formFieldValidators, { - formFields, - values, - expressionContext: { patient, mode: sessionMode }, + ) + .then((result) => { + setValue(dependent.id, result); + // validate calculated value + const { errors, warnings } = validateFieldValue(dependent, result, context.formFieldValidators, { + formFields, + values, + expressionContext: { patient, mode: sessionMode }, + }); + if (!dependent.meta.submission) { + dependent.meta.submission = {}; + } + dependent.meta.submission.errors = errors; + dependent.meta.submission.warnings = warnings; + if (!errors.length) { + context.formFieldAdapters[dependent.type].transformFieldValue(dependent, result, context); + } + updateFormField(dependent); + }) + .catch((error) => { + reportError(error, 'Error evaluating calculate expression'); }); - if (!dependent.meta.submission) { - dependent.meta.submission = {}; - } - dependent.meta.submission.errors = errors; - dependent.meta.submission.warnings = warnings; - if (!errors.length) { - context.formFieldAdapters[dependent.type].transformFieldValue(dependent, result, context); - } - updateFormField(dependent); - }).catch((error) => { - reportError(error, 'Error evaluating calculate expression'); - }); } // evaluate hide if (dependent.hide) { - evaluateHide({ value: dependent, type: 'field' }, formFields, values, sessionMode, patient, evaluateExpression); + evaluateHide( + { value: dependent, type: 'field' }, + formFields, + values, + sessionMode, + patient, + evaluateExpression, + updateFormField, + ); } // evaluate disabled if (typeof dependent.disabled === 'object' && dependent.disabled.disableWhenExpression) { @@ -197,12 +207,8 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon sessionMode, patient, evaluateExpression, + updateFormField, ); - if (isTrue(section.isHidden)) { - section.questions.forEach((field) => { - field.isParentHidden = true; - }); - } shouldUpdateForm = true; break; } @@ -214,14 +220,15 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon if (field.pageDependents) { field.pageDependents?.forEach((dep) => { const dependent = formJson.pages.find((f) => f.label == dep); - evaluateHide({ value: dependent, type: 'page' }, formFields, values, sessionMode, patient, evaluateExpression); - if (isTrue(dependent.isHidden)) { - dependent.sections.forEach((section) => { - section.questions.forEach((field) => { - field.isParentHidden = true; - }); - }); - } + evaluateHide( + { value: dependent, type: 'page' }, + formFields, + values, + sessionMode, + patient, + evaluateExpression, + updateFormField, + ); shouldUpdateForm = true; }); } diff --git a/src/hooks/useEvaluateFormFieldExpressions.ts b/src/hooks/useEvaluateFormFieldExpressions.ts index d260d619..c52c1ade 100644 --- a/src/hooks/useEvaluateFormFieldExpressions.ts +++ b/src/hooks/useEvaluateFormFieldExpressions.ts @@ -109,7 +109,15 @@ export const useEvaluateFormFieldExpressions = ( useEffect(() => { factoryContext.formJson?.pages?.forEach((page) => { if (page.hide) { - evaluateHide({ value: page, type: 'page' }, formFields, formValues, sessionMode, patient, evaluateExpression); + evaluateHide( + { value: page, type: 'page' }, + formFields, + formValues, + sessionMode, + patient, + evaluateExpression, + null, + ); } else { page.isHidden = false; } @@ -122,6 +130,7 @@ export const useEvaluateFormFieldExpressions = ( sessionMode, patient, evaluateExpression, + null, ); } else { section.isHidden = false; diff --git a/src/utils/form-helper.ts b/src/utils/form-helper.ts index 5e6002f2..95eef61d 100644 --- a/src/utils/form-helper.ts +++ b/src/utils/form-helper.ts @@ -95,6 +95,7 @@ export function evaluateHide( sessionMode: SessionMode, patient: fhir.Patient, expressionRunnerFn, + updateFormFieldFn: (field: FormField) => void | null, ) { const { value, type } = node; const isHidden = expressionRunnerFn(value['hide']?.hideWhenExpression, node, allFields, allValues, { @@ -111,15 +112,20 @@ export function evaluateHide( if (type == 'page') { value['sections'].forEach((section) => { section.isParentHidden = isHidden; - cascadeVisibilityToChildFields(isHidden, section, allFields); + cascadeVisibilityToChildFields(isHidden, section, allFields, updateFormFieldFn); }); } if (type == 'section') { - cascadeVisibilityToChildFields(isHidden, value, allFields); + cascadeVisibilityToChildFields(isHidden, value, allFields, updateFormFieldFn); } } -function cascadeVisibilityToChildFields(visibility: boolean, section: FormSection, allFields: Array) { +function cascadeVisibilityToChildFields( + visibility: boolean, + section: FormSection, + allFields: Array, + updateFormFieldFn: (field: FormField) => void, +) { const candidateIds = section.questions.map((q) => q.id); allFields .filter((field) => candidateIds.includes(field.id)) @@ -130,6 +136,7 @@ function cascadeVisibilityToChildFields(visibility: boolean, section: FormSectio member.isParentHidden = visibility; }); } + updateFormFieldFn?.(field); }); }