react-keywords 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ <!--rehype:ignore:start-->
2
+ react-keywords
3
+ ===
4
+ <!--rehype:ignore:end-->
5
+
6
+ [![Build & Deploy](https://github.com/uiwjs/react-keywords/actions/workflows/ci.yml/badge.svg)](https://github.com/uiwjs/react-keywords/actions/workflows/ci.yml)
7
+ [![npm version](https://img.shields.io/npm/v/react-keywords.svg)](https://www.npmjs.com/package/react-keywords)
8
+ [![Open in unpkg](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/react-keywords/file/README.md)
9
+
10
+ Highlight a keyword in a piece of text and return a React element.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm i react-keywords
16
+ ```
17
+
18
+ ## Basic Usage
19
+
20
+ ```jsx mdx:preview
21
+ import React, { useState, Fragment } from 'react';
22
+ import Keywords from 'react-keywords';
23
+
24
+ export default function Demo() {
25
+ const [value, setValue] = useState('react');
26
+ return (
27
+ <Fragment>
28
+ <input value={value} onChange={(evn) => setValue(evn.target.value)} />
29
+ <Keywords value={value}>
30
+ Highlight a keyword in a piece of text and return a React element.
31
+ </Keywords>
32
+ </Fragment>
33
+ );
34
+ }
35
+ ```
36
+
37
+ ## render
38
+
39
+ ```jsx mdx:preview
40
+ import React, { useState, Fragment } from 'react';
41
+ import Keywords from 'react-keywords';
42
+
43
+ export default function Demo() {
44
+ const [value, setValue] = useState('react');
45
+ const highlight = (txt) => <span style={{ background: 'red', color: '#fff' }}>{txt}</span>;
46
+ return (
47
+ <Fragment>
48
+ <input value={value} onChange={(evn) => setValue(evn.target.value)} />
49
+ <Keywords value={value} render={highlight}>
50
+ Highlight a keyword in a piece of text and return a React element.
51
+ </Keywords>
52
+ </Fragment>
53
+ );
54
+ }
55
+ ```
56
+
57
+ ## color
58
+
59
+ ```jsx mdx:preview
60
+ import React, { useState, Fragment } from 'react';
61
+ import Keywords from 'react-keywords';
62
+
63
+ export default function Demo() {
64
+ const [value, setValue] = useState('react');
65
+ const highlight = (txt) => <span style={{ background: 'red', color: '#fff' }}>{txt}</span>;
66
+ return (
67
+ <Fragment>
68
+ <input value={value} onChange={(evn) => setValue(evn.target.value)} />
69
+ <Keywords value={value} color="red" backgroundColor="">
70
+ Highlight a keyword in a piece of text and return a React element.
71
+ </Keywords>
72
+ </Fragment>
73
+ );
74
+ }
75
+ ```
76
+
77
+ ## API
78
+
79
+ ```ts
80
+ export interface KeywordsProps {
81
+ value?: string;
82
+ children?: string;
83
+ color?: string;
84
+ backgroundColor?: string;
85
+ render?: (keyword: string, color: string, backgroundColor: string) => JSX.Element;
86
+ }
87
+ export default function Keywords(props: KeywordsProps): JSX.Element | undefined;
88
+ ```
89
+
90
+ ## Contributors
91
+
92
+ As always, thanks to our amazing contributors!
93
+
94
+ <a href="https://github.com/uiwjs/react-keywords/graphs/contributors">
95
+ <img src="https://uiwjs.github.io/react-keywords/CONTRIBUTORS.svg" />
96
+ </a>
97
+
98
+ Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors).
99
+
100
+ ### License
101
+
102
+ Licensed under the MIT License.
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "react-keywords",
3
+ "version": "0.0.1",
4
+ "description": "Highlight a keyword in a piece of text and return a React element.",
5
+ "homepage": "https://uiwjs.github.io/react-keywords/",
6
+ "author": "kenny wang <wowohoo@qq.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/uiwjs/react-keywords"
11
+ },
12
+ "main": "./cjs/index.js",
13
+ "module": "./esm/index.js",
14
+ "scripts": {
15
+ "watch": "tsbb watch",
16
+ "build": "tsbb build",
17
+ "test": "tsbb test --env=jsdom",
18
+ "coverage": "tsbb test --env=jsdom --coverage --bail"
19
+ },
20
+ "keywords": [
21
+ "react",
22
+ "react-keywords",
23
+ "highlight",
24
+ "keywords"
25
+ ],
26
+ "peerDependencies": {
27
+ "react": ">=16.8.0",
28
+ "react-dom": ">=16.8.0"
29
+ }
30
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,48 @@
1
+ import { FC, Fragment, PropsWithChildren, useMemo } from 'react';
2
+
3
+ export interface KeywordsProps {
4
+ value?: string;
5
+ children?: string;
6
+ color?: string;
7
+ backgroundColor?: string;
8
+ render?: (keyword: string, color: string, backgroundColor: string) => JSX.Element;
9
+ }
10
+
11
+ interface HighlightProps extends KeywordsProps {}
12
+ const Highlight: FC<PropsWithChildren<HighlightProps>> = (props) => {
13
+ const { children, value, color, backgroundColor, render } = props;
14
+ const child = useMemo(
15
+ () => (render ? render(value!, color!, backgroundColor!) : <span style={{ color, backgroundColor }}>{value}</span>),
16
+ [color, backgroundColor, value],
17
+ );
18
+ return (
19
+ <Fragment>
20
+ {children}
21
+ {value && child}
22
+ </Fragment>
23
+ );
24
+ };
25
+
26
+ export default function Keywords(props: KeywordsProps) {
27
+ const { children, color = 'inherit', backgroundColor = '#ffff00', value, render } = props;
28
+ if (typeof children !== 'string') return children;
29
+ const splitMatch = new RegExp(`${value}`, 'ig');
30
+ const matched = children.split(splitMatch);
31
+ return (
32
+ <Fragment>
33
+ {matched.map((item, idx) => {
34
+ return (
35
+ <Highlight
36
+ key={idx}
37
+ color={color}
38
+ value={matched.length > idx + 1 ? value : undefined}
39
+ render={render}
40
+ backgroundColor={backgroundColor}
41
+ >
42
+ {item}
43
+ </Highlight>
44
+ );
45
+ })}
46
+ </Fragment>
47
+ );
48
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../tsconfig",
3
+ "include": ["src", ".kktrc.ts"],
4
+ "compilerOptions": {
5
+ "outDir": "./cjs",
6
+ "baseUrl": ".",
7
+ "noEmit": false
8
+ }
9
+ }