Skip to content

Commit 4c00f3e

Browse files
committed
Added Highlihter to easy highlight words
1 parent f652e2f commit 4c00f3e

File tree

4 files changed

+7922
-7755
lines changed

4 files changed

+7922
-7755
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@types/react-dom": "^18.0.10",
1919
"react": "^18.2.0",
2020
"react-dom": "^18.2.0",
21+
"react-highlight-words": "^0.18.0",
2122
"react-redux": "^8.0.5",
2223
"react-router-dom": "^6.6.2",
2324
"react-scripts": "5.0.1",
@@ -48,5 +49,8 @@
4849
"last 1 firefox version",
4950
"last 1 safari version"
5051
]
52+
},
53+
"devDependencies": {
54+
"@types/react-highlight-words": "^0.16.4"
5155
}
5256
}

src/app/components/Card/CardDescription.tsx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
import { Box } from '@mui/system'
22
import React from 'react'
3+
import Highlighter from 'react-highlight-words'
34
import { News } from '../../../types'
5+
import { useAppSelector } from '../../hooks/storeHooks'
6+
import { selectSearchRequest } from '../../store/storeSlices/searchSlice'
47
import { StyledTypography } from '../../styles/styledComponents'
58

69
export const CardDescription: React.FC<Props> = ({ newsData }) => {
10+
const searchRequest = atob(useAppSelector(selectSearchRequest)).split(' ')
11+
const description = getDescription(newsData)
12+
713
return (
814
<Box>
9-
<StyledTypography
10-
sx={{ fontSize: '24px', fontWeight: '400', lineHeight: '29px', marginY: '20px' }}
11-
>
12-
{getTitle(newsData)}
13-
</StyledTypography>
14-
1515
<StyledTypography sx={{ marginBottom: '20px', fontWeight: 400, lineHeight: '24px' }}>
16-
{getDescription(newsData)}
16+
<Highlighter
17+
searchWords={searchRequest}
18+
autoEscape={true}
19+
textToHighlight={description}
20+
highlightStyle={{ backgroundColor: '#fff619a1' }}
21+
/>
1722
</StyledTypography>
1823
</Box>
1924
)
2025
}
2126

22-
const getTitle = (newsData: News) => {
23-
if (newsData.title.length > 37) {
24-
return `${newsData.title.split('-')[0].slice(0, 37)}...`
25-
}
26-
return newsData.title
27-
}
28-
2927
const getDescription = (newsData: News) => {
30-
if (newsData.description && newsData.description.length > 139) {
31-
return `${newsData.description.slice(0, 139)}...`
28+
if (newsData.summary && newsData.summary.length > 100) {
29+
return `${newsData.summary.slice(0, 100)}...`
3230
}
33-
return newsData.description
31+
return newsData.summary
3432
}
3533

3634
type Props = {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react'
2+
import { News } from '../../../types'
3+
import { useAppSelector } from '../../hooks/storeHooks'
4+
import { selectSearchRequest } from '../../store/storeSlices/searchSlice'
5+
import { StyledTypography } from '../../styles/styledComponents'
6+
import Highlighter from 'react-highlight-words'
7+
8+
export const CardTitle: React.FC<Props> = ({ newsData }) => {
9+
const searchRequest = atob(useAppSelector(selectSearchRequest)).split(' ')
10+
11+
return (
12+
<StyledTypography
13+
sx={{
14+
fontSize: '24px',
15+
fontWeight: '400',
16+
lineHeight: '29px',
17+
marginY: '20px',
18+
height: '60px',
19+
}}
20+
>
21+
<Highlighter
22+
searchWords={searchRequest}
23+
autoEscape={true}
24+
textToHighlight={newsData.title}
25+
highlightStyle={{ backgroundColor: '#fff619a1' }}
26+
/>
27+
</StyledTypography>
28+
)
29+
}
30+
type Props = {
31+
newsData: News
32+
}

0 commit comments

Comments
 (0)