Skip to content

Commit

Permalink
fix: update form context field visibility cascading (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmale authored Nov 28, 2024
1 parent dbdc0f6 commit 19af5e5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
73 changes: 40 additions & 33 deletions src/components/renderer/field/fieldLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
});
}
Expand Down
11 changes: 10 additions & 1 deletion src/hooks/useEvaluateFormFieldExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -122,6 +130,7 @@ export const useEvaluateFormFieldExpressions = (
sessionMode,
patient,
evaluateExpression,
null,
);
} else {
section.isHidden = false;
Expand Down
13 changes: 10 additions & 3 deletions src/utils/form-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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<FormField>) {
function cascadeVisibilityToChildFields(
visibility: boolean,
section: FormSection,
allFields: Array<FormField>,
updateFormFieldFn: (field: FormField) => void,
) {
const candidateIds = section.questions.map((q) => q.id);
allFields
.filter((field) => candidateIds.includes(field.id))
Expand All @@ -130,6 +136,7 @@ function cascadeVisibilityToChildFields(visibility: boolean, section: FormSectio
member.isParentHidden = visibility;
});
}
updateFormFieldFn?.(field);
});
}

Expand Down

0 comments on commit 19af5e5

Please sign in to comment.