-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added highlight to search suggestions (#117)
* Added highlight to search suggestions * Using JSS class for highlight instead * Added highlight tests
- Loading branch information
Showing
6 changed files
with
99 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react' | ||
import { Typography } from '@material-ui/core' | ||
|
||
const escapeHtml = unsafe => | ||
unsafe | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"') | ||
.replace(/'/g, ''') | ||
|
||
const addHighlight = (query, text, className = '') => { | ||
if (!text) return '' | ||
return escapeHtml(text).replace( | ||
new RegExp(query, 'gi'), | ||
match => `<span class="${className}">${match}</span>`, | ||
) | ||
} | ||
|
||
export default function Highlight({ query, text, classes = {}, ...props }) { | ||
return ( | ||
<Typography | ||
{...props} | ||
dangerouslySetInnerHTML={{ __html: addHighlight(query, text, classes.highlight) }} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import Highlight from 'react-storefront/Highlight' | ||
|
||
describe('Highlight', () => { | ||
it('should not blow up if empty props', () => { | ||
const wrapper = mount(<Highlight />) | ||
expect(wrapper.text()).toBe('') | ||
}) | ||
it('should not add highlights if no matches', () => { | ||
const wrapper = mount(<Highlight text="the fox jumps over" query="dog" />) | ||
expect(wrapper.text()).toBe('the fox jumps over') | ||
}) | ||
it('should escape text', () => { | ||
const wrapper = mount(<Highlight text={`"foo" > 'bar' < zat`} />) | ||
expect(wrapper.text()).toBe('"foo" > 'bar' < zat') | ||
}) | ||
it('should add highlights to matches', () => { | ||
const wrapper = mount( | ||
<Highlight text="the fox jumps over the ox" query="ox" classes={{ highlight: 'foo' }} />, | ||
) | ||
const matches = wrapper.html().match(/<span class="foo">ox<\/span>/g) | ||
expect(matches.length).toBe(2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters