-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAdaptiveHighlightedText.tsx
More file actions
90 lines (81 loc) · 2.07 KB
/
Copy pathAdaptiveHighlightedText.tsx
File metadata and controls
90 lines (81 loc) · 2.07 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { FC, ReactNode } from 'react'
import InfoIcon from '@mui/icons-material/Info'
import { HighlightedText, HighlightOptions, HighlightPattern } from './index'
import { AdaptiveDynamicTrimmer } from '../AdaptiveTrimmer/AdaptiveDynamicTrimmer'
import { HighlightedTrimmedText } from './HighlightedTrimmedText'
type AdaptiveHighlightedTextProps = {
idPrefix?: string
/**
* 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
/**
* Extra content to put into the tooltip
*/
extraTooltip?: ReactNode
/**
* The minimum length we never want to go under
*/
minLength?: number
/**
* Do we want to see debug output about the adaptive trimming process?
*/
debugMode?: boolean
}
/**
* Display a text with a part highlighted, adaptively trimmed to the maximum length around the highlight
*/
export const AdaptiveHighlightedText: FC<AdaptiveHighlightedTextProps> = ({
idPrefix = 'adaptive-highlighted-text',
text,
pattern,
options,
extraTooltip,
minLength,
debugMode,
}) => {
const fullContent = <HighlightedText text={text} pattern={pattern} options={options} />
return text ? (
<AdaptiveDynamicTrimmer
idPrefix={idPrefix}
getFullContent={() => ({
content: fullContent,
length: text.length,
})}
getShortenedContent={wantedLength => {
const content = (
<HighlightedTrimmedText
fragmentLength={wantedLength}
text={text}
pattern={pattern}
options={options}
/>
)
return {
content,
length: wantedLength,
}
}}
extraTooltip={
extraTooltip ? (
<>
<InfoIcon />
{extraTooltip}
</>
) : undefined
}
debugMode={debugMode}
minLength={minLength}
/>
) : undefined
}