Skip to content

Commit

Permalink
Fix enableWhen logic linked to a repeating item
Browse files Browse the repository at this point in the history
  • Loading branch information
fongsean committed Oct 14, 2024
1 parent c4a2813 commit f606f23
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,98 @@ export const qEnableWhen: Questionnaire = {
]
};

export const qEnableWhenMultiCheckbox: Questionnaire = {
resourceType: 'Questionnaire',
id: 'EnableWhenMultiCheckbox',
name: 'EnableWhenMultiCheckbox',
title: 'EnableWhen Multi-select Checkbox',
version: '0.1.0',
status: 'draft',
date: '2024-05-01',
url: 'https://smartforms.csiro.au/docs/behavior/other/enable-when-multi-checkbox',
item: [
{
extension: [
{
url: 'http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl',
valueCodeableConcept: {
coding: [
{
system: 'http://hl7.org/fhir/questionnaire-item-control',
code: 'check-box'
}
]
}
},
{
url: 'http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-openLabel',
valueString: 'Other, please specify'
}
],
linkId: 'select-conditions-list',
text: 'Select one or more conditions',
type: 'open-choice',
repeats: true,
answerOption: [
{
valueString: 'Condition A (Displays Clinical guidance: Condition A question)'
},
{
valueString: 'Condition B (Displays Clinical guidance: Condition B question)'
},
{
valueString: 'Condition C (Displays Clinical guidance: Condition C question)'
},
{
valueString: 'Condition D'
},
{
valueString: 'Condition E'
},
{
valueString: 'Condition F'
}
]
},
{
linkId: 'clinical-guidance-a',
text: 'Clinical guidance: Condition A',
type: 'display',
enableWhen: [
{
question: 'select-conditions-list',
operator: '=',
answerString: 'Condition A (Displays Clinical guidance: Condition A question)'
}
]
},
{
linkId: 'clinical-guidance-b',
text: 'Clinical guidance: Condition B',
type: 'display',
enableWhen: [
{
question: 'select-conditions-list',
operator: '=',
answerString: 'Condition B (Displays Clinical guidance: Condition B question)'
}
]
},
{
linkId: 'clinical-guidance-c',
text: 'Clinical guidance: Condition C',
type: 'display',
enableWhen: [
{
question: 'select-conditions-list',
operator: '=',
answerString: 'Condition C (Displays Clinical guidance: Condition C question)'
}
]
}
]
};

export const qEnableBehaviorAll: Questionnaire = {
resourceType: 'Questionnaire',
id: 'EnableBehaviorAll',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
qEnableWhen,
qEnableWhenExpressionSimple,
qEnableWhenExpressionTabs,
qEnableWhenMultiCheckbox,
qInitialRepeats,
qInitialSingle,
qText
Expand Down Expand Up @@ -59,6 +60,12 @@ export const EnableWhen: Story = {
}
};

export const EnableWhenMultiCheckbox: Story = {
args: {
questionnaire: qEnableWhenMultiCheckbox
}
};

export const EnableBehaviorAll: Story = {
args: {
questionnaire: qEnableBehaviorAll
Expand Down
20 changes: 15 additions & 5 deletions packages/smart-forms-renderer/src/utils/enableWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,19 +339,29 @@ export function checkItemIsEnabledSingle(
): boolean {
const checkedIsEnabledItems: boolean[] = [];

// Check if linked item satisfies enableWhen condition
for (const linkedItem of enableWhenItemProperties.linked) {
let isEnabledForThisLinkedItem = false;

// Linked item has answers
if (linkedItem.answer && linkedItem.answer.length > 0) {
// Check if linked answer within item satisfies enableWhen condition
// Exit early once a linked answer is found to satisfy the condition
for (const answer of linkedItem.answer) {
const isEnabledForThisLinkedItem = isEnabledAnswerTypeSwitcher(
const isEnabledForThisLinkedAnswer = isEnabledAnswerTypeSwitcher(
linkedItem.enableWhen,
answer
);

// In a repeat item, if at least one answer satisfies the condition, the item is enabled
// FIXME need to look further at this
checkedIsEnabledItems.push(isEnabledForThisLinkedItem);
break;
if (isEnabledForThisLinkedAnswer) {
isEnabledForThisLinkedItem = true;
break;
}
}

// Push result of the linked item to the checkedIsEnabledItems array
checkedIsEnabledItems.push(isEnabledForThisLinkedItem);

continue;
}

Expand Down

0 comments on commit f606f23

Please sign in to comment.