11import * as React from 'react'
2- import { MatchInfo , findTextMatches , NO_MATCH , NormalizerOptions , PositiveMatchInfo } from './text-matching'
2+ import { findTextMatches , NormalizerOptions , PositiveMatchInfo } from './text-matching'
33import { FC , ReactNode } from 'react'
44import { SxProps } from '@mui/material/styles'
55import Box from '@mui/material/Box'
@@ -56,7 +56,7 @@ interface HighlightedTextProps {
5656 * If not given, we will just search for the pattern.
5757 * If given, this will be executed, and the pattern will not even be considered.
5858 */
59- parts ?: MatchInfo [ ]
59+ partsToHighlight ?: PositiveMatchInfo [ ]
6060
6161 /**
6262 * Options for highlighting (case sensitivity, styling, etc.)
@@ -72,21 +72,18 @@ interface HighlightedTextProps {
7272export const HighlightedText : FC < HighlightedTextProps > = ( {
7373 text,
7474 pattern = NoHighlights ,
75- parts ,
75+ partsToHighlight ,
7676 options = defaultHighlight ,
7777} ) => {
7878 const { sx = defaultHighlightStyle , findOptions = { } } = options
7979
8080 // Have we been told what to highlight exactly? If not, look for the pattern
81- const tasks = parts ?? findTextMatches ( text , pattern , findOptions )
82-
83- // Get the real matches
84- const realTasks = tasks . filter ( ( task ) : task is PositiveMatchInfo => task !== NO_MATCH )
81+ const matches = partsToHighlight ?? findTextMatches ( text , pattern , findOptions )
8582
8683 if ( text === undefined ) return undefined // Nothing to display
87- if ( ! realTasks . length ) return text // We don't have to highlight anything
84+ if ( ! matches . length ) return text // We don't have to highlight anything
8885
89- const sortedTasks = realTasks . sort ( ( a , b ) =>
86+ const sortedMatches = matches . sort ( ( a , b ) =>
9087 a . startPos > b . startPos ? 1 : a . startPos < b . startPos ? - 1 : 0 ,
9188 )
9289
@@ -96,9 +93,9 @@ export const HighlightedText: FC<HighlightedTextProps> = ({
9693
9794 while ( processedChars < text . length ) {
9895 // Do we still have tasks?
99- if ( processedTasks < sortedTasks . length ) {
96+ if ( processedTasks < sortedMatches . length ) {
10097 // Yes, there are more tasks
101- const task = sortedTasks [ processedTasks ]
98+ const task = sortedMatches [ processedTasks ]
10299 if ( task . startPos < processedChars ) {
103100 // This task with collude
104101 processedTasks ++ // just skip this task
0 commit comments