Skip to content

Commit

Permalink
feat: add caseIgnored props.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Aug 4, 2022
1 parent ad65f1c commit b312195
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
35 changes: 33 additions & 2 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,35 @@ export default function Demo() {
}
```

### caseIgnored

Case is ignored by default `caseIgnored=true`.

```jsx mdx:preview
import React, { useState, Fragment } from 'react';
import Keywords from 'react-keywords';

export default function Demo() {
const [value, setValue] = useState('re');
const text = `caseIgnored={true} Highlight A Keyword In A Piece Of Text And Return A React Element.`
return (
<Fragment>
<input value={value} onChange={(evn) => setValue(evn.target.value)} />
<br />
<Keywords value={value} color="red" backgroundColor="">
{text}
</Keywords>
<br />
<Keywords
value={value}
caseIgnored={false}
children={`caseIgnored={false} Highlight a keyword in a piece of text and return a React element.`}
/>
</Fragment>
);
}
```

## Support bundle

```html
Expand Down Expand Up @@ -137,14 +166,16 @@ export default function Demo() {
## API

```ts
import { FC, PropsWithChildren } from 'react';
export interface KeywordsProps {
value?: string;
children?: string;
color?: string;
caseIgnored?: boolean;
backgroundColor?: string;
render?: (keyword: string, color: string, backgroundColor: string) => JSX.Element;
}
export default function Keywords(props: KeywordsProps): JSX.Element | undefined;
declare const KeywordsInner: FC<PropsWithChildren<KeywordsProps>>;
export default KeywordsInner;
```

## Contributors
Expand Down
8 changes: 5 additions & 3 deletions core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FC, Fragment, PropsWithChildren, useMemo } from 'react';
export interface KeywordsProps {
value?: string;
color?: string;
caseIgnored?: boolean;
backgroundColor?: string;
render?: (keyword: string, color: string, backgroundColor: string) => JSX.Element;
}
Expand All @@ -23,9 +24,10 @@ const Highlight: FC<PropsWithChildren<HighlightProps>> = (props) => {
};

const KeywordsInner: FC<PropsWithChildren<KeywordsProps>> = (props) => {
const { children, color = 'inherit', backgroundColor = '#ffff00', value, render } = props;
const { children, caseIgnored = true, color = 'inherit', backgroundColor = '#ffff00', value, render } = props;
if (typeof children !== 'string') return <Fragment>{children}</Fragment>;
const splitMatch = new RegExp(`${value}`, 'ig');
const splitMatch = new RegExp(`${value}`, caseIgnored ? 'ig' : 'g');
const values = value ? children.match(splitMatch) : [];
const matched = children.split(splitMatch);
return (
<Fragment>
Expand All @@ -34,7 +36,7 @@ const KeywordsInner: FC<PropsWithChildren<KeywordsProps>> = (props) => {
<Highlight
key={idx}
color={color}
value={matched.length > idx + 1 ? value : undefined}
value={matched.length > idx + 1 ? (values as string[])[idx] : undefined}
render={render}
backgroundColor={backgroundColor}
>
Expand Down

0 comments on commit b312195

Please sign in to comment.