-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/WP-72: Highlight matching search terms (#873)
* task/WP-72 highlighted search queries/term in job history infinitescrolltable * fixed formatting * removed highlight and added bold * remove css properties and replaced <mark> with <b> * used local css modules instead of global * Changed implementation of HighlightSearchTerm component to be used by Jobs.jsx * added useMemo hook and improved HighlightSearchTerm perf * removed useMemo hook * added unit tests for HighlightSearchTerm component * updated comment and fixed linting --------- Co-authored-by: Shayan Khan <shayanaijaz@gmail.com> Co-authored-by: Chandra Y <cyemparala@tacc.utexas.edu>
- Loading branch information
1 parent
31af29e
commit f3e7f18
Showing
7 changed files
with
138 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
client/src/components/_common/HighlightSearchTerm/HighlightSearchTerm.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import styles from './HighlightSearchTerm.module.scss'; | ||
|
||
const HighlightSearchTerm = ({ searchTerm, content }) => { | ||
if (!searchTerm) { | ||
return <>{content}</>; | ||
} | ||
|
||
const searchTermRegex = new RegExp(`(${searchTerm})`, 'gi'); | ||
|
||
const highlightParts = () => { | ||
const parts = content.split(searchTermRegex); | ||
return parts.map((part, i) => { | ||
const isSearchTerm = part.match(searchTermRegex); | ||
return isSearchTerm ? ( | ||
<b className={styles['highlight']} key={i}> | ||
{part} | ||
</b> | ||
) : ( | ||
part | ||
); | ||
}); | ||
}; | ||
|
||
return <>{highlightParts()}</>; | ||
}; | ||
|
||
HighlightSearchTerm.propTypes = { | ||
searchTerm: PropTypes.string, | ||
content: PropTypes.string, | ||
}; | ||
|
||
HighlightSearchTerm.defaultProps = { | ||
searchTerm: '', | ||
content: '', | ||
}; | ||
|
||
export default HighlightSearchTerm; |
3 changes: 3 additions & 0 deletions
3
client/src/components/_common/HighlightSearchTerm/HighlightSearchTerm.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.highlight { | ||
font-weight: bold; | ||
} |
57 changes: 57 additions & 0 deletions
57
client/src/components/_common/HighlightSearchTerm/HighlightSearchTerm.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { | ||
toBeInTheDocument, | ||
toHaveClass, | ||
toBeNull, | ||
} from '@testing-library/jest-dom'; | ||
|
||
import HighlightSearchTerm from './HighlightSearchTerm'; | ||
|
||
describe('HighlightSearchTerm Component', () => { | ||
it('renders content when searchTerm is not provided', () => { | ||
const { getByText } = render(<HighlightSearchTerm content="Lorem ipsum" />); | ||
expect(getByText('Lorem ipsum')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders without highlighting when searchTerm in content do not match', () => { | ||
const { getByText } = render( | ||
<HighlightSearchTerm | ||
searchTerm="minim" | ||
content="Lorem ipsum dolor sit amet" | ||
/> | ||
); | ||
expect(getByText('Lorem ipsum dolor sit amet')).toBeInTheDocument(); | ||
expect(document.querySelector('.highlight')).toBeNull(); | ||
}); | ||
|
||
it('renders content when searchTerm is not provided', () => { | ||
const { getByText } = render(<HighlightSearchTerm content="Lorem ipsum" />); | ||
expect(getByText('Lorem ipsum')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders content with searchTerm highlighted', () => { | ||
const { getByText } = render( | ||
<HighlightSearchTerm | ||
searchTerm="ipsum" | ||
content="Lorem ipsum dolor sit amet" | ||
/> | ||
); | ||
const highlightedText = getByText('ipsum'); | ||
expect(highlightedText).toHaveClass('highlight'); | ||
}); | ||
|
||
it('renders content with multiple searchTerm occurrences highlighted', () => { | ||
const { getAllByText } = render( | ||
<HighlightSearchTerm | ||
searchTerm="ipsum" | ||
content="Lorem ipsum ipsum Loremipsum ipsumipsum dolor sit amet" | ||
/> | ||
); | ||
const highlightedText = getAllByText('ipsum'); | ||
expect(highlightedText.length).toBe(5); | ||
highlightedText.forEach((element) => { | ||
expect(element).toHaveClass('highlight'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import HighlightSearchTerm from './HighlightSearchTerm'; | ||
|
||
export default HighlightSearchTerm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters