-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from shhhplus/dev/v2
v2.0.0
- Loading branch information
Showing
8 changed files
with
206 additions
and
148 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 |
---|---|---|
@@ -1,25 +1,44 @@ | ||
# react-keywords-highlight | ||
# react-highlight-words | ||
|
||
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/shhhplus/react-keywords-highlight/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/@shhhplus/react-keywords-highlight.svg?style=flat)](https://www.npmjs.com/package/@shhhplus/react-keywords-highlight) [![codecov](https://img.shields.io/codecov/c/github/shhhplus/react-keywords-highlight/main?token=4MY5JFP8BX)](https://codecov.io/gh/shhhplus/react-keywords-highlight) [![build status](https://img.shields.io/github/actions/workflow/status/shhhplus/react-keywords-highlight/cd.yml)](https://github.com/shhhplus/react-keywords-highlight/actions) | ||
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/shhhplus/react-highlight-words/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/@shhhplus/react-highlight-words.svg?style=flat)](https://www.npmjs.com/package/@shhhplus/react-highlight-words) [![codecov](https://img.shields.io/codecov/c/github/shhhplus/react-highlight-words/main?token=4MY5JFP8BX)](https://codecov.io/gh/shhhplus/react-highlight-words) [![build status](https://img.shields.io/github/actions/workflow/status/shhhplus/react-highlight-words/cd.yml)](https://github.com/shhhplus/react-highlight-words/actions) | ||
|
||
A component that can highlight matching keywords with customizable styles. | ||
A component that can highlight matching words with customizable style. | ||
|
||
## Install | ||
|
||
```sh | ||
npm install @shhhplus/react-keywords-highlight --save | ||
npm install @shhhplus/react-highlight-words --save | ||
``` | ||
|
||
## How to use | ||
## Usage | ||
|
||
```javascript | ||
import KeywordsHighlight from '@shhhplus/react-keywords-highlight'; | ||
### Basic | ||
|
||
```jsx | ||
import Highlightwords from '@shhhplus/react-highlight-words'; | ||
|
||
const Demo = () => { | ||
return ( | ||
<Highlightwords words="birthday"> | ||
Welcome everyone to come and join my birthday party. | ||
</Highlightwords> | ||
); | ||
}; | ||
``` | ||
|
||
### Customize style | ||
|
||
```jsx | ||
import Highlightwords from '@shhhplus/react-highlight-words'; | ||
|
||
const Demo = () => { | ||
return ( | ||
<KeywordsHighlight keywords="birthday" style={{ color: 'red' }}> | ||
<Highlightwords | ||
words="birthday" | ||
render={(word) => <span style={{ color: 'red' }}>{word}</span>} | ||
> | ||
Welcome everyone to come and join my birthday party. | ||
</KeywordsHighlight> | ||
</Highlightwords> | ||
); | ||
}; | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,102 +1,77 @@ | ||
import compile from './compile'; | ||
|
||
test('empty content should works', () => { | ||
expect(compile('', 'party')).toMatchObject([]); | ||
expect(compile('', ['party'])).toMatchObject([]); | ||
}); | ||
|
||
test('empty keywords should works', () => { | ||
const content = 'Welcome everyone to my party'; | ||
expect(compile(content, '')).toMatchObject([content]); | ||
expect(compile(content, [''])).toMatchObject([content]); | ||
expect(compile(content, [])).toMatchObject([content]); | ||
}); | ||
|
||
test('empty content and empty keywords should works', () => { | ||
expect(compile('', '')).toMatchObject([]); | ||
expect(compile('', [''])).toMatchObject([]); | ||
expect(compile('', [])).toMatchObject([]); | ||
}); | ||
|
||
test('no matched word should works', () => { | ||
const content = 'Welcome everyone to come and join my birthday party.'; | ||
expect(compile(content, 'tom')).toMatchObject([ | ||
{ | ||
text: content, | ||
matched: false, | ||
}, | ||
]); | ||
expect(compile(content, 'tom')).toMatchObject([content]); | ||
}); | ||
|
||
test('a single matched word should works', () => { | ||
const content = 'Welcome everyone to come and join my birthday party.'; | ||
expect(compile(content, 'party')).toMatchObject([ | ||
{ | ||
text: 'Welcome everyone to come and join my birthday ', | ||
matched: false, | ||
}, | ||
{ | ||
text: 'party', | ||
matched: true, | ||
}, | ||
{ | ||
text: '.', | ||
matched: false, | ||
}, | ||
'Welcome everyone to come and join my birthday ', | ||
{ text: 'party', matched: true }, | ||
'.', | ||
]); | ||
}); | ||
|
||
test(`multiple matched words should works`, () => { | ||
const content = | ||
'hi, party time. Welcome everyone to come and join my birthday party.'; | ||
expect(compile(content, 'party')).toMatchObject([ | ||
{ | ||
text: 'hi, ', | ||
matched: false, | ||
}, | ||
{ | ||
text: 'party', | ||
matched: true, | ||
}, | ||
{ | ||
text: ' time. Welcome everyone to come and join my birthday ', | ||
matched: false, | ||
}, | ||
{ | ||
text: 'party', | ||
matched: true, | ||
}, | ||
{ | ||
text: '.', | ||
matched: false, | ||
}, | ||
'hi, ', | ||
{ text: 'party', matched: true }, | ||
' time. Welcome everyone to come and join my birthday ', | ||
{ text: 'party', matched: true }, | ||
'.', | ||
]); | ||
}); | ||
|
||
test('matched word at begin should works', () => { | ||
const content = 'party.Welcome everyone.'; | ||
expect(compile(content, 'party')).toMatchObject([ | ||
{ | ||
text: 'party', | ||
matched: true, | ||
}, | ||
{ | ||
text: '.Welcome everyone.', | ||
matched: false, | ||
}, | ||
{ text: 'party', matched: true }, | ||
'.Welcome everyone.', | ||
]); | ||
}); | ||
|
||
test('matched word at end should works', () => { | ||
const content = 'Welcome everyone to my party'; | ||
expect(compile(content, 'party')).toMatchObject([ | ||
{ | ||
text: 'Welcome everyone to my ', | ||
matched: false, | ||
}, | ||
{ | ||
text: 'party', | ||
matched: true, | ||
}, | ||
'Welcome everyone to my ', | ||
{ text: 'party', matched: true }, | ||
]); | ||
}); | ||
|
||
test('empty content should works', () => { | ||
expect(compile('', 'party')).toMatchObject([]); | ||
}); | ||
|
||
test('empty keywords should works', () => { | ||
test('multiple keywords as props should works', () => { | ||
const content = 'Welcome everyone to my party'; | ||
expect(compile(content, '')).toMatchObject([ | ||
{ | ||
text: content, | ||
matched: false, | ||
}, | ||
|
||
expect(compile(content, ['party', 'to'])).toMatchObject([ | ||
'Welcome everyone ', | ||
{ text: 'to', matched: true }, | ||
' my ', | ||
{ text: 'party', matched: true }, | ||
]); | ||
}); | ||
|
||
test('empty content and empty keywords should works', () => { | ||
expect(compile('', '')).toMatchObject([]); | ||
expect(compile(content, ['party', 'other'])).toMatchObject([ | ||
'Welcome everyone to my ', | ||
{ text: 'party', matched: true }, | ||
]); | ||
}); |
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.