1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: remove dangerouslySetInnerHTML (#4181)

This commit is contained in:
Jaanus Sellin 2023-07-07 13:54:21 +03:00 committed by GitHub
parent f995a0ed6c
commit 5388eaf48c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import { VFC } from 'react'; import { Fragment, VFC } from 'react';
import { safeRegExp } from '@server/util/escape-regex'; import { safeRegExp } from '@server/util/escape-regex';
import { styled } from '@mui/material'; import { styled } from '@mui/material';
@ -29,11 +29,18 @@ export const Highlighter: VFC<IHighlighterProps> = ({
const regex = safeRegExp(search, caseSensitive ? 'g' : 'gi'); const regex = safeRegExp(search, caseSensitive ? 'g' : 'gi');
return ( const parts = children.split(regex);
<StyledSpan
dangerouslySetInnerHTML={{ const highlightedText = parts.map((part, index) =>
__html: children?.replaceAll(regex, '<mark>$&</mark>') || '', index < parts.length - 1 ? (
}} <Fragment key={index}>
/> {part}
<mark>{search}</mark>
</Fragment>
) : (
part
)
); );
return <StyledSpan>{highlightedText}</StyledSpan>;
}; };