Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

O3-3616: Filter lab results in result tree viewer #1937

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works because Boolean is function that coerces the value to a boolean and null gets coerced to `false.

Suggested change
.filter((child) => child !== null);
.filter(Boolean);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ibacher are you suggesting a different approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep... I was suggesting you can replace the .filter(child => child !== null) with the slightly shorter .filter(Boolean)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in the other similar place.

}

if (filteredSubSets.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not nest this in the previous if?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved

return {
...treeNode,
subSets: filteredSubSets,
};
}

return null;
}

const FilterSet: React.FC<FilterSetProps> = ({ hideFilterSetHeader = false }) => {
Expand All @@ -44,7 +59,7 @@ const FilterSet: React.FC<FilterSetProps> = ({ 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],
Expand Down
Loading