-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(snippet): add a snippet widget to be able to highlight snippet r…
…esults (#1797)
- Loading branch information
Showing
13 changed files
with
193 additions
and
34 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
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 |
---|---|---|
@@ -1,19 +1,13 @@ | ||
import React from 'react'; | ||
import Highlighter from './Highlighter'; | ||
|
||
export default function Highlight({hit, attributeName, highlight}) { | ||
const parsedHighlightedValue = highlight({hit, attributeName}); | ||
const reactHighlighted = parsedHighlightedValue.map((v, i) => { | ||
const key = `split-${i}-${v.value}`; | ||
if (!v.isHighlighted) { | ||
return <span key={key} className="ais-Highlight__nonHighlighted">{v.value}</span>; | ||
} | ||
return <em key={key} className="ais-Highlight__highlighted">{v.value}</em>; | ||
}); | ||
return <span className="ais-Highlight">{reactHighlighted}</span>; | ||
export default function Highlight(props) { | ||
return <Highlighter highlightProperty="_highlightResult" {...props}/>; | ||
} | ||
|
||
Highlight.propTypes = { | ||
hit: React.PropTypes.object.isRequired, | ||
attributeName: React.PropTypes.string.isRequired, | ||
highlight: React.PropTypes.func.isRequired, | ||
}; | ||
|
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
20 changes: 20 additions & 0 deletions
20
packages/react-instantsearch/src/components/Highlighter.js
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,20 @@ | ||
import React from 'react'; | ||
|
||
export default function Highlighter({hit, attributeName, highlight, highlightProperty}) { | ||
const parsedHighlightedValue = highlight({hit, attributeName, highlightProperty}); | ||
const reactHighlighted = parsedHighlightedValue.map((v, i) => { | ||
const key = `split-${i}-${v.value}`; | ||
if (!v.isHighlighted) { | ||
return <span key={key} className="ais-Highlight__nonHighlighted">{v.value}</span>; | ||
} | ||
return <em key={key} className="ais-Highlight__highlighted">{v.value}</em>; | ||
}); | ||
return <span className="ais-Highlight">{reactHighlighted}</span>; | ||
} | ||
|
||
Highlighter.propTypes = { | ||
hit: React.PropTypes.object.isRequired, | ||
attributeName: React.PropTypes.string.isRequired, | ||
highlight: React.PropTypes.func.isRequired, | ||
highlightProperty: React.PropTypes.string.isRequired, | ||
}; |
44 changes: 44 additions & 0 deletions
44
packages/react-instantsearch/src/components/Highlighter.test.js
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,44 @@ | ||
/* eslint-env jest, jasmine */ | ||
|
||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
|
||
import Highlighter from './Highlighter'; | ||
import parseAlgoliaHit from '../core/highlight'; | ||
|
||
describe('Highlighter', () => { | ||
it('parses an highlighted attribute of hit object', () => { | ||
const hitFromAPI = { | ||
objectID: 0, | ||
deep: {attribute: {value: 'awesome highlighted hit!'}}, | ||
_highlightProperty: { | ||
deep: { | ||
attribute: { | ||
value: { | ||
value: 'awesome <ais-highlight>hi</ais-highlight>ghlighted <ais-highlight>hi</ais-highlight>t!', | ||
fullyHighlighted: true, | ||
matchLevel: 'full', | ||
matchedWords: [''], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const highlight = ({hit, attributeName, highlightProperty}) => parseAlgoliaHit({ | ||
preTag: '<ais-highlight>', | ||
postTag: '</ais-highlight>', | ||
attributeName, | ||
hit, | ||
highlightProperty, | ||
}); | ||
|
||
const tree = renderer.create( | ||
<Highlighter attributeName="deep.attribute.value" | ||
hit={hitFromAPI} | ||
highlight={highlight} | ||
highlightProperty="_highlightProperty"/> | ||
); | ||
expect(tree.toJSON()).toMatchSnapshot(); | ||
}); | ||
}); |
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,13 @@ | ||
import React from 'react'; | ||
|
||
import Highlighter from './Highlighter'; | ||
|
||
export default function Snippet(props) { | ||
return <Highlighter highlightProperty="_snippetResult" {...props}/>; | ||
} | ||
|
||
Snippet.propTypes = { | ||
hit: React.PropTypes.object.isRequired, | ||
attributeName: React.PropTypes.string.isRequired, | ||
highlight: React.PropTypes.func.isRequired, | ||
}; |
37 changes: 37 additions & 0 deletions
37
packages/react-instantsearch/src/components/Snippet.test.js
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,37 @@ | ||
/* eslint-env jest, jasmine */ | ||
|
||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
|
||
import Snippet from './Snippet'; | ||
import parseAlgoliaHit from '../core/highlight'; | ||
|
||
describe('Snippet', () => { | ||
it('parses an highlighted snippet attribute of hit object', () => { | ||
const hitFromAPI = { | ||
objectID: 0, | ||
deep: {attribute: {value: 'awesome highlighted hit!'}}, | ||
_snippetResults: { | ||
deep: {attribute: {value: { | ||
value: 'awesome <ais-highlight>hi</ais-highlight>ghlighted <ais-highlight>hi</ais-highlight>t!', | ||
fullyHighlighted: true, | ||
matchLevel: 'full', | ||
matchedWords: [''], | ||
}}}, | ||
}, | ||
}; | ||
|
||
const highlight = ({hit, attributeName, highlightProperty}) => parseAlgoliaHit({ | ||
preTag: '<ais-highlight>', | ||
postTag: '</ais-highlight>', | ||
attributeName, | ||
hit, | ||
highlightProperty, | ||
}); | ||
|
||
const tree = renderer.create( | ||
<Snippet attributeName="deep.attribute.value" hit={hitFromAPI} highlight={highlight}/> | ||
); | ||
expect(tree.toJSON()).toMatchSnapshot(); | ||
}); | ||
}); |
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
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,31 @@ | ||
import connectHighlight from '../connectors/connectHighlight.js'; | ||
import SnippetComponent from '../components/Snippet.js'; | ||
|
||
/** | ||
* Renders an highlighted snippet attribute. | ||
* @name Snippet | ||
* @kind widget | ||
* @propType {string} attributeName - the location of the highlighted snippet attribute in the hit | ||
* @propType {object} hit - the hit object containing the highlighted snippet attribute | ||
* @example | ||
* import React from 'react'; | ||
* | ||
* import {InstantSearch, connectHits, Snippet} from 'InstantSearch'; | ||
* | ||
* const CustomHits = connectHits(hits => { | ||
* return hits.map((hit) => <p><Snippet attributeName='description' hit={hit}/></p>); | ||
* }); | ||
* | ||
* export default function App() { | ||
* return ( | ||
* <InstantSearch | ||
* appId="latency" | ||
* apiKey="6be0576ff61c053d5f9a3225e2a90f76" | ||
* indexName="ikea" | ||
* > | ||
* <CustomHits /> | ||
* </InstantSearch> | ||
* ); | ||
* } | ||
*/ | ||
export default connectHighlight(SnippetComponent); |
Oops, something went wrong.