({
text,
query,
numberOfLines,
style,
highlightStyle,
bounceAnim: externalBounceAnim,
caseSensitive = false,
highlightOnlyFirstMatch = false,
})
| 19 | } |
| 20 | |
| 21 | const HighlightedText: React.FC<HighlightedTextProps> = ({ |
| 22 | text, |
| 23 | query, |
| 24 | numberOfLines, |
| 25 | style, |
| 26 | highlightStyle, |
| 27 | bounceAnim: externalBounceAnim, |
| 28 | caseSensitive = false, |
| 29 | highlightOnlyFirstMatch = false, |
| 30 | }) => { |
| 31 | const internalBounceAnim = useBounceAnimation(query); |
| 32 | const bounceAnim = externalBounceAnim || internalBounceAnim; |
| 33 | const [queryKey, setQueryKey] = useState<string>(''); |
| 34 | |
| 35 | useEffect(() => { |
| 36 | setQueryKey(query); |
| 37 | }, [query]); |
| 38 | |
| 39 | const baseTextStyle = useMemo(() => { |
| 40 | if (!style) return {}; |
| 41 | |
| 42 | if (Array.isArray(style)) { |
| 43 | return style.reduce((acc, curr) => ({ ...acc, ...curr }), {}); |
| 44 | } |
| 45 | |
| 46 | return style; |
| 47 | }, [style]); |
| 48 | |
| 49 | const highlightedStyle = useMemo( |
| 50 | () => ({ |
| 51 | ...styles.highlight, |
| 52 | ...(highlightStyle || {}), |
| 53 | fontSize: baseTextStyle.fontSize, |
| 54 | fontFamily: baseTextStyle.fontFamily, |
| 55 | fontWeight: baseTextStyle.fontWeight || '600', |
| 56 | lineHeight: baseTextStyle.lineHeight, |
| 57 | letterSpacing: baseTextStyle.letterSpacing, |
| 58 | transform: Platform.OS === 'ios' ? [{ scale: bounceAnim }] : undefined, |
| 59 | }), |
| 60 | [bounceAnim, highlightStyle, baseTextStyle], |
| 61 | ); |
| 62 | |
| 63 | // Create a style for non-highlighted text parts that ensures it looks the same as original text |
| 64 | const nonHighlightedStyle = useMemo( |
| 65 | () => ({ |
| 66 | ...baseTextStyle, // Copy all original text properties |
| 67 | }), |
| 68 | [baseTextStyle], |
| 69 | ); |
| 70 | |
| 71 | const renderTextPart = useCallback( |
| 72 | (part: TextPart, index: number) => { |
| 73 | if (part.isMatch) { |
| 74 | return ( |
| 75 | <Animated.View |
| 76 | key={`highlight-container-${index}-${queryKey}`} |
| 77 | style={[styles.highlightContainer, { transform: [{ scale: bounceAnim }] }]} |
| 78 | > |
nothing calls this directly
no test coverage detected