mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-15 01:16:22 +02:00
Adds a Biome rule for "no unused imports", which is something we sometimes have trouble catching. We're adding this as a warning for now. It is safely and easily fixable with `yarn lint:fix`.  
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { VFC } from 'react';
|
|
import { safeRegExp } from '@server/util/escape-regex';
|
|
import { styled } from '@mui/material';
|
|
|
|
interface IHighlighterProps {
|
|
search?: string;
|
|
children?: string;
|
|
caseSensitive?: boolean;
|
|
}
|
|
|
|
export const StyledSpan = styled('span')(({ theme }) => ({
|
|
'&>mark': {
|
|
backgroundColor: theme.palette.highlight,
|
|
},
|
|
}));
|
|
|
|
export const Highlighter: VFC<IHighlighterProps> = ({
|
|
search,
|
|
children,
|
|
caseSensitive,
|
|
}) => {
|
|
if (!children) {
|
|
return null;
|
|
}
|
|
|
|
if (!search) {
|
|
return <>{children}</>;
|
|
}
|
|
|
|
const searchTerms = search.split(',').map((term) => term.trim());
|
|
const searchRegex = searchTerms
|
|
.map((term) => safeRegExp(term, '').source)
|
|
.join('|');
|
|
const regex = new RegExp(searchRegex, caseSensitive ? 'g' : 'gi');
|
|
|
|
const parts = children.split(regex);
|
|
|
|
const matches = Array.from(children.matchAll(regex)).map(
|
|
(match) => match[0],
|
|
);
|
|
|
|
const highlightedText = parts.flatMap((part, index) => {
|
|
return index < matches.length
|
|
? // biome-ignore lint/suspicious/noArrayIndexKey: <explanation>
|
|
[part, <mark key={index}>{matches[index]}</mark>]
|
|
: [part];
|
|
});
|
|
|
|
return <StyledSpan>{highlightedText}</StyledSpan>;
|
|
};
|