-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
8,263 additions
and
1,817 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
45 changes: 45 additions & 0 deletions
45
src/components/Highlighter/StringHighlighter/StringHighlighter.tsx
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,45 @@ | ||
import * as React from 'react'; | ||
import { HighlighterCore, HighlighterCoreAccuracy } from '../../../utils/HighlighterCore'; | ||
|
||
export type HighlighterRenderer = ({ key: number, substring }) => ReactNode; | ||
|
||
interface Props { | ||
/** Target string for highlight searching */ | ||
string: string; | ||
/** Array of words or RegExp that is used for search */ | ||
searchTerms: string[] | RegExp; | ||
/** Define the accuracy of search */ | ||
accuracy?: HighlighterCoreAccuracy; | ||
/** If enabled the diacritic characters should be matched */ | ||
ignoreDiacritics?: boolean; | ||
/** Ignore case sensitivity */ | ||
ignoreCase?: boolean; | ||
/** Method is called on render founded substrings */ | ||
highlightRenderer: HighlighterRenderer; | ||
} | ||
|
||
export const StringHighlighter: React.FC<Props> = (props) => { | ||
const { string, highlightRenderer, ...highlighterOptions } = props; | ||
const highlighter = React.useMemo(() => new HighlighterCore(highlighterOptions), [ | ||
highlighterOptions, | ||
]); | ||
const stringParts = highlighter.find(string); | ||
return ( | ||
<> | ||
{stringParts.map(({ substring, highlighted }, index) => { | ||
if (highlighted) { | ||
return highlightRenderer({ key: index, substring }); | ||
} | ||
return substring; | ||
})} | ||
</> | ||
); | ||
}; | ||
|
||
StringHighlighter.displayName = 'StringHighlighter'; | ||
|
||
StringHighlighter.defaultProps = { | ||
accuracy: 'exact', | ||
ignoreDiacritics: true, | ||
ignoreCase: true, | ||
}; |
80 changes: 80 additions & 0 deletions
80
src/components/Highlighter/StringHighlighter/__tests__/StringHighlighter.spec.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,80 @@ | ||
import React from 'react'; | ||
import toJson from 'enzyme-to-json'; | ||
import { StringHighlighter } from '../StringHighlighter'; | ||
|
||
describe('StringHighlighter', () => { | ||
const string = 'We are looking for php, java and javascript developer'; | ||
const searchTerms = []; | ||
const ignoreDiacritics = true; | ||
const ignoreCase = true; | ||
// eslint-disable-next-line react/display-name, react/prop-types | ||
const highlightRenderer = ({ substring, ...props }) => <b {...props}>{substring}</b>; | ||
// eslint-disable-next-line react/display-name, react/prop-types | ||
const renderComponent = (props) => { | ||
return mount( | ||
<StringHighlighter | ||
string={string} | ||
searchTerms={searchTerms} | ||
ignoreDiacritics={ignoreDiacritics} | ||
ignoreCase={ignoreCase} | ||
highlightRenderer={highlightRenderer} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
describe('not highlighted', () => { | ||
it('should initially render empty component correctly', () => { | ||
const wrapper = renderComponent({ | ||
string: '', | ||
}); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
it('should not highlight not existed terms', () => { | ||
const wrapper = renderComponent({ | ||
searchTerms: ['notExisting', 'dev', 'a'], | ||
}); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
it('should not highlight case sensitive terms if ignoreCase is false', () => { | ||
const wrapper = renderComponent({ | ||
searchTerms: ['DEVELOPER', 'Php'], | ||
ignoreCase: false, | ||
}); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
it('should not highlight terms with diacritics if ignoreDiacritics is false', () => { | ||
const wrapper = renderComponent({ | ||
searchTerms: ['dévéloper'], | ||
ignoreDiacritics: false, | ||
}); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
}); | ||
describe('highlighted', () => { | ||
it('should highlight list of terms', () => { | ||
const wrapper = renderComponent({ | ||
searchTerms: ['javascript developer', 'php', 'java'], | ||
}); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
it('should highlight case sensitive terms by default', () => { | ||
const wrapper = renderComponent({ | ||
searchTerms: ['DEVELOPER', 'Php'], | ||
}); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
it('should highlight terms with diacritics by default', () => { | ||
const wrapper = renderComponent({ | ||
searchTerms: ['dévéloper'], | ||
}); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
it('should highlight overlapping terms correctly', () => { | ||
const wrapper = renderComponent({ | ||
searchTerms: ['javascript developer', 'java and javascript'], | ||
}); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
176 changes: 176 additions & 0 deletions
176
...ents/Highlighter/StringHighlighter/__tests__/__snapshots__/StringHighlighter.spec.js.snap
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,176 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`StringHighlighter highlighted should highlight case sensitive terms by default 1`] = ` | ||
<StringHighlighter | ||
accuracy="exact" | ||
highlightRenderer={[Function]} | ||
ignoreCase={true} | ||
ignoreDiacritics={true} | ||
searchTerms={ | ||
Array [ | ||
"DEVELOPER", | ||
"Php", | ||
] | ||
} | ||
string="We are looking for php, java and javascript developer" | ||
> | ||
We are looking for | ||
<b | ||
key="1" | ||
> | ||
php | ||
</b> | ||
, java and javascript | ||
<b | ||
key="3" | ||
> | ||
developer | ||
</b> | ||
</StringHighlighter> | ||
`; | ||
|
||
exports[`StringHighlighter highlighted should highlight list of terms 1`] = ` | ||
<StringHighlighter | ||
accuracy="exact" | ||
highlightRenderer={[Function]} | ||
ignoreCase={true} | ||
ignoreDiacritics={true} | ||
searchTerms={ | ||
Array [ | ||
"javascript developer", | ||
"php", | ||
"java", | ||
] | ||
} | ||
string="We are looking for php, java and javascript developer" | ||
> | ||
We are looking for | ||
<b | ||
key="1" | ||
> | ||
php | ||
</b> | ||
, | ||
<b | ||
key="3" | ||
> | ||
java | ||
</b> | ||
and | ||
<b | ||
key="5" | ||
> | ||
javascript developer | ||
</b> | ||
</StringHighlighter> | ||
`; | ||
|
||
exports[`StringHighlighter highlighted should highlight overlapping terms correctly 1`] = ` | ||
<StringHighlighter | ||
accuracy="exact" | ||
highlightRenderer={[Function]} | ||
ignoreCase={true} | ||
ignoreDiacritics={true} | ||
searchTerms={ | ||
Array [ | ||
"java and javascript", | ||
"javascript developer", | ||
] | ||
} | ||
string="We are looking for php, java and javascript developer" | ||
> | ||
We are looking for php, | ||
<b | ||
key="1" | ||
> | ||
java and javascript developer | ||
</b> | ||
</StringHighlighter> | ||
`; | ||
|
||
exports[`StringHighlighter highlighted should highlight terms with diacritics by default 1`] = ` | ||
<StringHighlighter | ||
accuracy="exact" | ||
highlightRenderer={[Function]} | ||
ignoreCase={true} | ||
ignoreDiacritics={true} | ||
searchTerms={ | ||
Array [ | ||
"dévéloper", | ||
] | ||
} | ||
string="We are looking for php, java and javascript developer" | ||
> | ||
We are looking for php, java and javascript | ||
<b | ||
key="1" | ||
> | ||
developer | ||
</b> | ||
</StringHighlighter> | ||
`; | ||
|
||
exports[`StringHighlighter not highlighted should initially render empty component correctly 1`] = ` | ||
<StringHighlighter | ||
accuracy="exact" | ||
highlightRenderer={[Function]} | ||
ignoreCase={true} | ||
ignoreDiacritics={true} | ||
searchTerms={Array []} | ||
string="" | ||
/> | ||
`; | ||
|
||
exports[`StringHighlighter not highlighted should not highlight case sensitive terms if ignoreCase is false 1`] = ` | ||
<StringHighlighter | ||
accuracy="exact" | ||
highlightRenderer={[Function]} | ||
ignoreCase={false} | ||
ignoreDiacritics={true} | ||
searchTerms={ | ||
Array [ | ||
"DEVELOPER", | ||
"Php", | ||
] | ||
} | ||
string="We are looking for php, java and javascript developer" | ||
> | ||
We are looking for php, java and javascript developer | ||
</StringHighlighter> | ||
`; | ||
|
||
exports[`StringHighlighter not highlighted should not highlight not existed terms 1`] = ` | ||
<StringHighlighter | ||
accuracy="exact" | ||
highlightRenderer={[Function]} | ||
ignoreCase={true} | ||
ignoreDiacritics={true} | ||
searchTerms={ | ||
Array [ | ||
"notExisting", | ||
"dev", | ||
"a", | ||
] | ||
} | ||
string="We are looking for php, java and javascript developer" | ||
> | ||
We are looking for php, java and javascript developer | ||
</StringHighlighter> | ||
`; | ||
|
||
exports[`StringHighlighter not highlighted should not highlight terms with diacritics if ignoreDiacritics is false 1`] = ` | ||
<StringHighlighter | ||
accuracy="exact" | ||
highlightRenderer={[Function]} | ||
ignoreCase={true} | ||
ignoreDiacritics={false} | ||
searchTerms={ | ||
Array [ | ||
"dévéloper", | ||
] | ||
} | ||
string="We are looking for php, java and javascript developer" | ||
> | ||
We are looking for php, java and javascript developer | ||
</StringHighlighter> | ||
`; |
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 @@ | ||
export { StringHighlighter } from './StringHighlighter'; |
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 @@ | ||
export { StringHighlighter } from './StringHighlighter'; |
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
Oops, something went wrong.