From 9f7b5061c855da5d2ada42ada57b751004c77ca9 Mon Sep 17 00:00:00 2001 From: reagan-meant Date: Tue, 23 Jul 2024 21:22:20 +0300 Subject: [PATCH 1/2] O3-3616: Filter lab results in result tree viewer --- .../src/test-results/filter/filter-set.tsx | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/esm-patient-labs-app/src/test-results/filter/filter-set.tsx b/packages/esm-patient-labs-app/src/test-results/filter/filter-set.tsx index 54ce60a95e..740f70b997 100644 --- a/packages/esm-patient-labs-app/src/test-results/filter/filter-set.tsx +++ b/packages/esm-patient-labs-app/src/test-results/filter/filter-set.tsx @@ -18,17 +18,32 @@ interface FilterSetProps { } 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()) || - (treeNode.subSets && treeNode.subSets.some((child) => filterTreeNode(inputValue, child)))) - ) { - return true; + if (!treeNode) { + return null; } - // Otherwise, return false - return false; + const matchesInput = treeNode.display.toLowerCase().includes(inputValue.toLowerCase()); + + if (matchesInput) { + return treeNode; + } + + let filteredSubSets = []; + + if (treeNode.subSets) { + filteredSubSets = treeNode.subSets + .map((child) => filterTreeNode(inputValue, child)) + .filter((child) => child !== null); + } + + if (filteredSubSets.length > 0) { + return { + ...treeNode, + subSets: filteredSubSets, + }; + } + + return null; } const FilterSet: React.FC = ({ hideFilterSetHeader = false }) => { @@ -44,7 +59,7 @@ const FilterSet: React.FC = ({ hideFilterSetHeader = false }) => const handleInputChange = useCallback( (e) => { setSearchTerm(searchTerm); - const filteredData = roots.filter((node) => filterTreeNode(searchTerm, node)); + const filteredData = roots.map((node) => filterTreeNode(searchTerm, node)).filter((node) => node !== null); setTreeDataFiltered(filteredData); }, [roots, searchTerm], From 348172d2120fbcfa626da46fe3f515bb0e273e4e Mon Sep 17 00:00:00 2001 From: reagan-meant Date: Mon, 29 Jul 2024 12:52:40 +0300 Subject: [PATCH 2/2] Improve code --- .../src/test-results/filter/filter-set.tsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/esm-patient-labs-app/src/test-results/filter/filter-set.tsx b/packages/esm-patient-labs-app/src/test-results/filter/filter-set.tsx index 740f70b997..e398e17d25 100644 --- a/packages/esm-patient-labs-app/src/test-results/filter/filter-set.tsx +++ b/packages/esm-patient-labs-app/src/test-results/filter/filter-set.tsx @@ -34,13 +34,13 @@ function filterTreeNode(inputValue, treeNode) { filteredSubSets = treeNode.subSets .map((child) => filterTreeNode(inputValue, child)) .filter((child) => child !== null); - } - if (filteredSubSets.length > 0) { - return { - ...treeNode, - subSets: filteredSubSets, - }; + if (filteredSubSets.length > 0) { + return { + ...treeNode, + subSets: filteredSubSets, + }; + } } return null;