mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	feat: Multi highlighter (#5666)
This commit is contained in:
		
							parent
							
								
									7fdd720aa3
								
							
						
					
					
						commit
						5ba588f59a
					
				| @ -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>'); | ||||
| }); | ||||
|  | ||||
| @ -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); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user