Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prop added to make Hashtags recognize according to the regex provided #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The component `ReactHashtag` is actually pretty generic. Is not something that s
| ---- | ---- | -----------
| renderHashtag(value: String, onClick: Function) | function | Returns the custom element to be renderer instead of a `<span>`. You can go wild here.
| onHashtagClick(value: String, e: Event) | function | The click handler for each hashtags. This will be called with the hashtag value that got clicked.
| hashtagRegex(value: Regex) | Regex | Here user can provide the custom regex. According to this regex matched values will be enclosed in `<span>`.


## Examples
Expand All @@ -75,6 +76,20 @@ const Card = (props) => (
);
```

### With Custom Hashtag Regex Prop
```jsx harmony

const Card = (props) => (
<p>
<ReactHashtag
hashtagRegex={/(?:(?<=\s)|^)#(\w*[a-zA-z]\w*)+\b(?!#)/gm}
>
{props.children}
</ReactHashtag>
</p>
);
```

### With styled components
```jsx harmony

Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const rule = /([#|#][^\s]+)/g;

export const parse = (value, renderer, action) => {
export const parse = (value, renderer, action, hashtagRegex = rule) => {
return value.split(rule).map((chunk) => {

if (chunk.match(rule)) {
if (hashtagRegex.test(chunk)) {
return renderer(chunk, action);
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default props => {
const hashtagRenderer =
props.renderHashtag || defaultHashtagRenderer(createElement);
const onHashtagClick = props.onHashtagClick;
const parsed = parse(contents, hashtagRenderer, onHashtagClick);
const parsed = parse(contents, hashtagRenderer, onHashtagClick, props.hashtagRegex);

return canRenderArray ? parsed : createElement("span", null, parsed);
};
2 changes: 2 additions & 0 deletions tests/__snapshots__/usual-flow.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
exports[`ReactHashtag should behave Should render custom hashtags 1`] = `"<div><div>#lots</div> <div>#of</div> <div>#hashtags</div> <div>#🤷🏿‍♀️</div> <div>#thatWasAUnicodeSet</div></div>"`;

exports[`ReactHashtag should behave Should render hashtags 1`] = `"<div>Hello <span>#there</span> fellow <span>#dude#Works?</span></div>"`;

exports[`ReactHashtag should behave Should render hashtags with custom regex 1`] = `"<div>Hello <span>#there</span> fellow #dude#Works?</div>"`;
12 changes: 12 additions & 0 deletions tests/usual-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ describe('ReactHashtag should behave', () => {
expect(context.html()).toMatchSnapshot();
});

it('Should render hashtags with custom regex', () => {

const context = shallow(
<div>
<ReactHashtag hashtagRegex={/(?:(?<=\s)|^)#(\w*[a-zA-z]\w*)+\b(?!#)/gm}>
Hello #there fellow #dude#Works?
</ReactHashtag>
</div>
);

expect(context.html()).toMatchSnapshot();
});

it('Should handle the hashtag actions', () => {
const onHashtagClick = jest.fn();
Expand Down