diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsx index 9d4fe97bed4..7d740a2ad47 100644 --- a/packages/ui/src/components/Command/Command.utils.tsx +++ b/packages/ui/src/components/Command/Command.utils.tsx @@ -4,10 +4,10 @@ import * as React from 'react' import { cn } from './../../lib/utils' import { DetailedHTMLProps, HTMLAttributes, KeyboardEventHandler } from 'react' +import { LoadingLine } from '../LoadingLine/LoadingLine' import { Modal } from '../Modal' import { ModalProps } from '../Modal/Modal' import { useCommandMenu } from './CommandMenuProvider' -import { LoadingLine } from '../LoadingLine/LoadingLine' type CommandPrimitiveElement = React.ElementRef type CommandPrimitiveProps = React.ComponentPropsWithoutRef @@ -312,28 +312,24 @@ CommandShortcut.displayName = 'CommandLabel' export interface TextHighlighterProps extends DetailedHTMLProps, HTMLSpanElement> { - text: string | undefined - query: string | undefined + text: string + query: string } export const TextHighlighter = ({ text, query, ...props }: TextHighlighterProps) => { - const highlightMatches = (text?: string) => { - if (!text) { - return '' + // Wrap all instances of `query` in a span to make them bold + const elements = text.split(query).flatMap((part, index, parts) => { + const returnValue = [<>{part}] + + // Add back the wrapped `query` (if it's not the last element) + if (index !== parts.length - 1) { + returnValue.push({query}) } - if (!query) { - return text - } + return returnValue + }) - const regex = new RegExp(query, 'gi') - return text.replace( - regex, - (match) => `${match}` - ) - } - - return + return {elements} } TextHighlighter.displayName = 'TextHighlighter'