Skip to content

Commit 46493f6

Browse files
committed
chore: update highlightSearchText in Helpers.tsx
1 parent a69ab93 commit 46493f6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/components/common/helpers/Helpers.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,9 @@ export const handleOnFocus = (e): void => {
10091009
}
10101010
}
10111011

1012+
/**
1013+
* @deprecated
1014+
*/
10121015
export const highlightSearchedText = (searchText: string, matchString: string): string => {
10131016
if (!searchText) {
10141017
return matchString
@@ -1019,6 +1022,34 @@ export const highlightSearchedText = (searchText: string, matchString: string):
10191022
return matchString.replace(regex, highlightText)
10201023
}
10211024

1025+
interface HighlightSearchTextProps {
1026+
/**
1027+
* The text to be highlighted
1028+
*/
1029+
searchText: string
1030+
/**
1031+
* The whole text string
1032+
*/
1033+
text: string
1034+
/**
1035+
* The classes to be applied to the highlighted text
1036+
*/
1037+
highlightClasses?: string
1038+
}
1039+
1040+
export const highlightSearchText = ({ searchText, text, highlightClasses }: HighlightSearchTextProps): string => {
1041+
if (!searchText) {
1042+
return text
1043+
}
1044+
1045+
try {
1046+
const regex = new RegExp(searchText, 'gi')
1047+
return text.replace(regex, (match) => `<span class="${highlightClasses}">${match}</span>`)
1048+
} catch (error) {
1049+
return text
1050+
}
1051+
}
1052+
10221053
export const trackByGAEvent = (category: string, action: string): void => {
10231054
ReactGA.event({
10241055
category: category,

0 commit comments

Comments
 (0)