Skip to content

Commit

Permalink
update @shhhplus/react-text-matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
shhhplus committed May 21, 2023
1 parent e2c8fa1 commit c532872
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![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 words in the text with custom styles. It is a lightweight wrapper based on [react-text-matcher](https://www.npmjs.com/package/@shhhplus/react-text-matcher). If you want more customization capabilities, please use [react-text-matcher](https://www.npmjs.com/package/@shhhplus/react-text-matcher).
A component that can highlight words in the text with custom styles. It is a lightweight wrapper based on [react-text-matcher](https://www.npmjs.com/package/@shhhplus/react-text-matcher).

If you want more customization capabilities, please use [react-text-matcher](https://www.npmjs.com/package/@shhhplus/react-text-matcher).

## Install

Expand Down Expand Up @@ -33,7 +35,10 @@ import Highlightwords from '@shhhplus/react-highlight-words';

const Demo = () => {
return (
<Highlightwords words={['weather', 'nice']} style={{ color: 'green' }}>
<Highlightwords
words={['weather', new RegExp('nice')]}
style={{ color: 'green' }}
>
The weather is nice today.
</Highlightwords>
);
Expand Down
18 changes: 12 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-highlight-words",
"version": "3.0.0",
"version": "3.0.1",
"description": "A component that can highlight words in the text with custom styles.",
"keywords": [
"react",
Expand Down Expand Up @@ -56,7 +56,7 @@
"typescript": "^5.0.4"
},
"dependencies": {
"@shhhplus/react-text-matcher": "^1.0.0",
"@shhhplus/react-text-matcher": "^2.0.1",
"react": "^18.0.0"
}
}
5 changes: 4 additions & 1 deletion src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ describe('HighlightWords', () => {
test('multi words should works', () => {
const text = 'AppleTodayFoodHappySunFood';
const { container } = render(
<HighlightWords words={['Food', 'Apple']} style={{ color: 'red' }}>
<HighlightWords
words={['Food', new RegExp('Apple')]}
style={{ color: 'red' }}
>
{text}
</HighlightWords>,
);
Expand Down
46 changes: 32 additions & 14 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CSSProperties, FC, useMemo } from 'react';
import { CSSProperties, FC, Fragment, useMemo } from 'react';
import TextMatcher from '@shhhplus/react-text-matcher';

type HighlightWordsProps = {
words: string | string[];
words: string | RegExp | (string | RegExp)[];
style?: CSSProperties;
children: string;
};
Expand All @@ -14,24 +14,42 @@ const HighlightWordsInner: FC<HighlightWordsProps> = ({
style,
children,
}) => {
const rules = useMemo(() => {
const { rules, render } = useMemo(() => {
const style2use = style || _globalStyle;
if (!style2use) {
return [];
}
const render = (word: string) => {
return <mark style={style2use}>{word}</mark>;
};

const list = typeof words === 'string' ? [words] : words;
return list.map((word) => {
if (style2use) {
return {
rules: words,
render,
};
} else {
return {
pattern: word,
render: (word: string) => {
return <mark style={style2use}>{word}</mark>;
},
rules: [],
render,
};
});
}
}, [words, style]);

return <TextMatcher rules={rules}>{children}</TextMatcher>;
return (
<TextMatcher rules={rules} text={children}>
{(nodes) => {
return (
<Fragment>
{nodes.map((node, idx) => {
return (
<Fragment key={idx}>
{typeof node === 'string' ? node : render(node.text)}
</Fragment>
);
})}
</Fragment>
);
}}
</TextMatcher>
);
};

const HighlightWords = HighlightWordsInner as typeof HighlightWordsInner & {
Expand Down

0 comments on commit c532872

Please sign in to comment.