Skip to content

Commit b312195

Browse files
committed
feat: add caseIgnored props.
1 parent ad65f1c commit b312195

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

core/README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,35 @@ export default function Demo() {
9898
}
9999
```
100100

101+
### caseIgnored
102+
103+
Case is ignored by default `caseIgnored=true`.
104+
105+
```jsx mdx:preview
106+
import React, { useState, Fragment } from 'react';
107+
import Keywords from 'react-keywords';
108+
109+
export default function Demo() {
110+
const [value, setValue] = useState('re');
111+
const text = `caseIgnored={true} Highlight A Keyword In A Piece Of Text And Return A React Element.`
112+
return (
113+
<Fragment>
114+
<input value={value} onChange={(evn) => setValue(evn.target.value)} />
115+
<br />
116+
<Keywords value={value} color="red" backgroundColor="">
117+
{text}
118+
</Keywords>
119+
<br />
120+
<Keywords
121+
value={value}
122+
caseIgnored={false}
123+
children={`caseIgnored={false} Highlight a keyword in a piece of text and return a React element.`}
124+
/>
125+
</Fragment>
126+
);
127+
}
128+
```
129+
101130
## Support bundle
102131

103132
```html
@@ -137,14 +166,16 @@ export default function Demo() {
137166
## API
138167

139168
```ts
169+
import { FC, PropsWithChildren } from 'react';
140170
export interface KeywordsProps {
141171
value?: string;
142-
children?: string;
143172
color?: string;
173+
caseIgnored?: boolean;
144174
backgroundColor?: string;
145175
render?: (keyword: string, color: string, backgroundColor: string) => JSX.Element;
146176
}
147-
export default function Keywords(props: KeywordsProps): JSX.Element | undefined;
177+
declare const KeywordsInner: FC<PropsWithChildren<KeywordsProps>>;
178+
export default KeywordsInner;
148179
```
149180

150181
## Contributors

core/src/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FC, Fragment, PropsWithChildren, useMemo } from 'react';
33
export interface KeywordsProps {
44
value?: string;
55
color?: string;
6+
caseIgnored?: boolean;
67
backgroundColor?: string;
78
render?: (keyword: string, color: string, backgroundColor: string) => JSX.Element;
89
}
@@ -23,9 +24,10 @@ const Highlight: FC<PropsWithChildren<HighlightProps>> = (props) => {
2324
};
2425

2526
const KeywordsInner: FC<PropsWithChildren<KeywordsProps>> = (props) => {
26-
const { children, color = 'inherit', backgroundColor = '#ffff00', value, render } = props;
27+
const { children, caseIgnored = true, color = 'inherit', backgroundColor = '#ffff00', value, render } = props;
2728
if (typeof children !== 'string') return <Fragment>{children}</Fragment>;
28-
const splitMatch = new RegExp(`${value}`, 'ig');
29+
const splitMatch = new RegExp(`${value}`, caseIgnored ? 'ig' : 'g');
30+
const values = value ? children.match(splitMatch) : [];
2931
const matched = children.split(splitMatch);
3032
return (
3133
<Fragment>
@@ -34,7 +36,7 @@ const KeywordsInner: FC<PropsWithChildren<KeywordsProps>> = (props) => {
3436
<Highlight
3537
key={idx}
3638
color={color}
37-
value={matched.length > idx + 1 ? value : undefined}
39+
value={matched.length > idx + 1 ? (values as string[])[idx] : undefined}
3840
render={render}
3941
backgroundColor={backgroundColor}
4042
>

0 commit comments

Comments
 (0)