-
Notifications
You must be signed in to change notification settings - Fork 0
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
Task/WP-72: Highlight matching search terms #873
Task/WP-72: Highlight matching search terms #873
Conversation
Codecov Report
@@ Coverage Diff @@
## main #873 +/- ##
==========================================
+ Coverage 63.34% 63.36% +0.02%
==========================================
Files 428 429 +1
Lines 12250 12271 +21
Branches 2521 2532 +11
==========================================
+ Hits 7760 7776 +16
- Misses 4284 4288 +4
- Partials 206 207 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New stylesheets for React components should avoid SASS, because it is global and offers limited benefits1.
Prefer a CSS Module to global CSS.
I.E. Rename to HighlightSearchTerm.scss.module.css
and update how class is set.
Footnotes
-
The benefits of SASS, for now, are only
@mixin
's. Native CSS supports variables and nesting. ↩
client/src/components/_common/HighlightSearchTerm/HighlightSearchTerm.scss
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approve the rendered UI.
Regarding arguably-redundant font-weight: bold
CSS, read my comment about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This solution works pretty well. I did have some concerns over code structuring and the new component that was created.
-
Whenever we create a new component in
_common
the goal is to have the component be as generic as possible so it can easily plug in anywhere we want to use it with little change/configuration. With the wayHighlightSearchTerm
is coded it is very specific to the job search functionality. If we wanted to add search term highlighting into another component in the future (e.g data files) then it would require a bit of rework to make it happen. -
In a table each column type can have a different cell rendering structure which is taken care of in
HighlightSearchTerm
using if statements. The issue I see is that cell rendering structures for these columns are already defined inJobs.jsx
when we setup the columns so redefining those with slightly different html (adding bold tags and class) seems a little redundant to me. I think I would better prefer a solution that maybe uses that column definition inJobs.jsx
and for each column that needs search term highlighting we can add theHighlightSearchTerm
component.
@chandra-tacc @rstijerina I'd be curious to know what y'all think
@shayanaijaz I agree. I changed the implementation of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work here. Looks good.
Please see if the additional feedback can be addressed to make this even better.
client/src/components/_common/HighlightSearchTerm/HighlightSearchTerm.jsx
Show resolved
Hide resolved
const highlightParts = () => { | ||
const parts = content.split(new RegExp(`(${searchTerm})`, 'gi')); | ||
return parts.map((part, i) => | ||
part.toLowerCase() === searchTerm.toLowerCase() ? ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This look great.
Minor suggestions to help improve perf.
- Instantiate a regex and use it for split and comparison in line 9. Doing toLowerCase on searchTerm for each part is unnecessary. You have the regex defined for it already. Regex works better on shorter strings which is the split part.
- If searchTerm is not available, return immediately.
I tried to see if just string replace with regex would work instead of split and replace, I did not get one working. But, may be you can try to see if that feasible?
suggestion code:
const highlightParts = () => {
if (!searchTerm) {
return content;
}
const searchTermRegex = new RegExp(`(${searchTerm})`, 'gi');
const parts = content.split(searchTermRegex);
return parts.map((part, i) =>
part.match(searchTermRegex) ? (
<b className={styles['highlight']} key={i}>
{part}
</b>
) : (
part
)
);
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed logic to use match
instead of toLowerCase
. Tried the other suggestion to use .replace()
function instead but couldn't get it to work properly. I am getting [object] [object' inside the string that needs to be highlighted. Is it because .replace() isn't good for returning the <b>...</b>
and only good for string? Let me know.
import PropTypes from 'prop-types'; | ||
import styles from './HighlightSearchTerm.module.scss'; | ||
|
||
const HighlightSearchTerm = ({ searchTerm, content }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For common components, can you please add unit tests. This helps to learn the behavior for usages and not break existing use cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by adding test cases for HighlightSearchTerm
. Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for adding tests and addressing comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small comment to fix
d30770a
to
6b24fb0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Good work
Overview
PR FP-319 introduced the job history search feature. This PR achieves highlighting/bolding of the matching search terms/ query in the Job History Search.
Related
Changes
HighlightSearchTerm
that bolds the matching queriesInfiniteScrollTable.jsx
for rendering the HighlightSearchTerm componentTesting
UI
Notes