-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHighlightedTrimmedText.tsx
More file actions
38 lines (32 loc) · 1.05 KB
/
Copy pathHighlightedTrimmedText.tsx
File metadata and controls
38 lines (32 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { FC } from 'react'
import { HighlightedText, HighlightOptions, HighlightPattern, NoHighlights } from './index'
import { trimAroundMatch } from './text-trimming'
type HighlightedTrimmedTextProps = {
/**
* The text to display
*/
text: string | undefined
/**
* The pattern to search for (and highlight)
*/
pattern?: HighlightPattern
/**
* Options for highlighting (case sensitivity, styling, etc.)
*
* (This is optional, sensible defaults are provided.)
*/
options?: HighlightOptions
/**
* What should be the length of the fragment delivered, which
* has the pattern inside it?
*/
fragmentLength: number
}
/**
* Display a text with a part highlighted, trimmed to a specific length around the highlight
*/
export const HighlightedTrimmedText: FC<HighlightedTrimmedTextProps> = props => {
const { text, pattern = NoHighlights, fragmentLength, options } = props
const { part } = trimAroundMatch(text, pattern, { fragmentLength })
return <HighlightedText text={part} pattern={pattern} options={options} />
}