import classNames from 'classnames'; import * as React from 'react'; import { View } from '../View'; import { strHasLength } from '../shared/utils'; import { getTestId } from '../utils/getTestId'; import { ComponentClassNames } from '../shared/constants'; import type { BaseHighlightMatchProps, HighlightMatchProps, ForwardRefPrimitive, Primitive, } from '../types'; export const HighlightMatchPrimitive: Primitive = ( { children, className, query, testId, ...rest }, ref ) => { const matchTestId = getTestId(testId, 'match'); const startIndex = children ?.toLocaleLowerCase() .indexOf(query?.toLocaleLowerCase()); if (strHasLength(query) && startIndex !== -1) { const match = children.substring(startIndex, startIndex + query.length); return ( {children.substring(0, startIndex)} {match} {children.substring(startIndex + query.length)} ); } return ( {children} ); }; /** * [📖 Docs](https://ui.docs.amplify.aws/react/components/highlightmatch) */ export const HighlightMatch: ForwardRefPrimitive< BaseHighlightMatchProps, 'span' > = React.forwardRef(HighlightMatchPrimitive); HighlightMatch.displayName = 'HighlightMatch';