1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

feat: Multi highlighter (#5666)

This commit is contained in:
Mateusz Kwasniewski 2023-12-18 10:58:07 +01:00 committed by GitHub
parent 7fdd720aa3
commit 5ba588f59a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -41,3 +41,21 @@ test('respects case sensitivity when specified', () => {
expect(container.innerHTML).not.toContain('<mark>');
});
test('highlights multiple search terms', () => {
const { container } = render(
<Highlighter search='Test,Text'>Test Text</Highlighter>,
);
expect(container.innerHTML).toContain('<mark>Test</mark>');
expect(container.innerHTML).toContain('<mark>Text</mark>');
});
test('highlights first match on conflict', () => {
const { container } = render(
<Highlighter search='Test,stText'>TestText</Highlighter>,
);
expect(container.innerHTML).toContain('<mark>Test</mark>');
expect(container.innerHTML).not.toContain('<mark>stText</mark>');
});

View File

@ -27,7 +27,11 @@ export const Highlighter: VFC<IHighlighterProps> = ({
return <>{children}</>;
}
const regex = safeRegExp(search, caseSensitive ? 'g' : 'gi');
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);