This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
forked from algolia/autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): create highlighting components
- Loading branch information
1 parent
c6c4da5
commit fb49161
Showing
5 changed files
with
169 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export { getAlgoliaHits, getAlgoliaResults } from './results'; | ||
export { | ||
highlightAlgoliaHit, | ||
reverseHighlightAlgoliaHit, | ||
snippetAlgoliaHit, | ||
parseHighlightedAttribute, | ||
parseReverseHighlightedAttribute, | ||
parseSnippetedAttribute, | ||
} from './formatting'; |
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,102 @@ | ||
import React from 'react'; | ||
import { | ||
parseHighlightedAttribute, | ||
parseReverseHighlightedAttribute, | ||
parseSnippetedAttribute, | ||
} from '@francoischalifour/autocomplete-preset-algolia'; | ||
|
||
type HighlighterProps = { | ||
[prop: string]: unknown; | ||
tagName: string; | ||
parts: ReturnType<typeof parseHighlightedAttribute>; | ||
}; | ||
|
||
function Highlighter({ tagName, parts, ...rest }: HighlighterProps) { | ||
return ( | ||
<span {...rest}> | ||
{parts.map((part, index) => { | ||
if (part.isHighlighted) { | ||
return React.createElement(tagName, { key: index }, part.value); | ||
} | ||
|
||
return part.value; | ||
})} | ||
</span> | ||
); | ||
} | ||
|
||
type HighlightProps = { | ||
[prop: string]: unknown; | ||
hit: any; | ||
attribute: string; | ||
tagName?: string; | ||
}; | ||
|
||
export function Highlight({ | ||
hit, | ||
attribute, | ||
tagName = 'mark', | ||
...rest | ||
}: HighlightProps) { | ||
let parts: ReturnType<typeof parseHighlightedAttribute> = []; | ||
|
||
try { | ||
parts = parseHighlightedAttribute({ | ||
hit, | ||
attribute, | ||
highlightPreTag: `<${tagName}>`, | ||
highlightPostTag: `</${tagName}>`, | ||
}); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.warn(error.message); | ||
} | ||
|
||
return <Highlighter tagName={tagName} parts={parts} {...rest} />; | ||
} | ||
|
||
export function Snippet({ | ||
hit, | ||
attribute, | ||
tagName = 'mark', | ||
...rest | ||
}: HighlightProps) { | ||
let parts: ReturnType<typeof parseSnippetedAttribute> = []; | ||
|
||
try { | ||
parts = parseSnippetedAttribute({ | ||
hit, | ||
attribute, | ||
highlightPreTag: `<${tagName}>`, | ||
highlightPostTag: `</${tagName}>`, | ||
}); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.warn(error.message); | ||
} | ||
|
||
return <Highlighter tagName={tagName} parts={parts} {...rest} />; | ||
} | ||
|
||
export function ReverseHighlight({ | ||
hit, | ||
attribute, | ||
tagName = 'mark', | ||
...rest | ||
}: HighlightProps) { | ||
let parts: ReturnType<typeof parseReverseHighlightedAttribute> = []; | ||
|
||
try { | ||
parts = parseReverseHighlightedAttribute({ | ||
hit, | ||
attribute, | ||
highlightPreTag: `<${tagName}>`, | ||
highlightPostTag: `</${tagName}>`, | ||
}); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.warn(error.message); | ||
} | ||
|
||
return <Highlighter tagName={tagName} parts={parts} {...rest} />; | ||
} |
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,9 +1,4 @@ | ||
export { Autocomplete } from './Autocomplete'; | ||
export { Highlight, ReverseHighlight, Snippet } from './Highlight'; | ||
export { getAlgoliaHits } from './getAlgoliaHits'; | ||
export { getAlgoliaResults } from './getAlgoliaResults'; | ||
// @TODO: provide React helpers without relying on `innerHTML`. | ||
export { | ||
highlightAlgoliaHit, | ||
reverseHighlightAlgoliaHit, | ||
snippetAlgoliaHit, | ||
} from '@francoischalifour/autocomplete-preset-algolia'; |