diff --git a/packages/esm-patient-labs-app/src/test-results/filter/filter-reducer.ts b/packages/esm-patient-labs-app/src/test-results/filter/filter-reducer.ts index e629f3fef3..2663050da3 100644 --- a/packages/esm-patient-labs-app/src/test-results/filter/filter-reducer.ts +++ b/packages/esm-patient-labs-app/src/test-results/filter/filter-reducer.ts @@ -54,6 +54,12 @@ const computeParents = ( tests.push(...newTests); lowestParents.push(...newLowestParents); }); + } else if (node.obs?.length > 0) { + // Treat the current node as a leaf and a test + leaves.push(node.flatName); + tests.push([node.flatName, node]); + // Add the current node to the lowest parents list + lowestParents.push({ flatName: node.flatName, display: node.display }); } parents[node.flatName] = leaves; return { parents, leaves, tests, lowestParents }; diff --git a/packages/esm-patient-labs-app/src/test-results/filter/filter-set.component.tsx b/packages/esm-patient-labs-app/src/test-results/filter/filter-set.component.tsx index b3d48d7f34..f17dc6480a 100644 --- a/packages/esm-patient-labs-app/src/test-results/filter/filter-set.component.tsx +++ b/packages/esm-patient-labs-app/src/test-results/filter/filter-set.component.tsx @@ -22,7 +22,6 @@ interface filterNodeParentProps extends Pick { } function filterTreeNode(inputValue, treeNode) { - // If the tree node's display value contains the user input, or any of its children's display contains the user input, return true if ( treeNode && (treeNode.display.toLowerCase().includes(inputValue.toLowerCase()) || @@ -30,8 +29,6 @@ function filterTreeNode(inputValue, treeNode) { ) { return true; } - - // Otherwise, return false return false; } @@ -62,23 +59,39 @@ const FilterSet: React.FC = () => { ); }; +// Recursive function to check if a node or any of its descendants have data +const hasNodeOrChildrenWithData = (node) => { + if (node.hasData) { + return true; + } + if (node.subSets) { + return node.subSets.some((subNode) => hasNodeOrChildrenWithData(subNode)); + } + return false; +}; + const FilterNodeParent = ({ root, itemNumber }: filterNodeParentProps) => { const config = useConfig(); const { t } = useTranslation(); const tablet = useLayoutType() === 'tablet'; const [expandAll, setExpandAll] = useState(undefined); - if (!root.subSets) return; + if (!hasNodeOrChildrenWithData(root)) { + return null; + } - const filterParent = root.subSets.map((node) => { - return ( - - ); - }); + const filterParent = root.subSets + .filter((node) => hasNodeOrChildrenWithData(node)) + .map((node) => { + return ( + + ); + }); return (
@@ -104,6 +117,11 @@ const FilterNode = ({ root, level, open }: FilterNodeProps) => { const indeterminate = isIndeterminate(parents[root.flatName], checkboxes); const allChildrenChecked = parents[root.flatName]?.every((kid) => checkboxes[kid]); + // Filter out nodes that don't have data or descendants with data + if (!hasNodeOrChildrenWithData(root)) { + return null; + } + return ( { open={open ?? false} > {!root?.subSets?.[0]?.obs && - root?.subSets?.map((node, index) => )} - {root?.subSets?.[0]?.obs && root.subSets?.map((obs, index) => )} + root?.subSets + ?.filter((node) => hasNodeOrChildrenWithData(node)) + .map((node, index) => )} + {root?.subSets?.[0]?.obs && + root.subSets?.filter((obs) => obs.hasData).map((obs, index) => )} ); @@ -130,6 +151,12 @@ const FilterNode = ({ root, level, open }: FilterNodeProps) => { const FilterLeaf = ({ leaf }: FilterLeafProps) => { const { checkboxes, toggleVal } = useContext(FilterContext); + + // Filter out leaves without data + if (!leaf.hasData) { + return null; + } + return (
{ labelText={leaf?.display} checked={checkboxes?.[leaf.flatName]} onChange={() => toggleVal(leaf.flatName)} - disabled={!leaf.hasData} />
);