@@ -2,18 +2,40 @@ import React, { PropTypes } from 'react';
2
2
import { render } from 'react-dom' ;
3
3
import reactReplaceString from 'react-string-replace' ;
4
4
5
- const HighlightNumbers = React . createClass ( {
5
+ const Demo = React . createClass ( {
6
6
propTypes : {
7
7
content : PropTypes . string . isRequired ,
8
8
} ,
9
9
10
+ /**
11
+ * NOTE: In many React examples you will see the `i` or `index` variable used
12
+ * as the key for JSX tags (such as the `<a>` tags in this example), however
13
+ * in this case we are iterating in three separate loops. This menas that we
14
+ * cannot use `key={i}` because all three JSX tags could get the same key.
15
+ */
10
16
render ( ) {
17
+ const text = 'Hey @ian_989, check out this link https://github.com/iansinnott/ Hope to see you at #reactconf' ;
18
+ let replacedText ;
19
+
20
+ // Match URLs
21
+ replacedText = reactReplaceString ( text , / ( h t t p s ? : \/ \/ \S + ) / g, match => (
22
+ < a key = { match } href = { match } > { match } </ a >
23
+ ) ) ;
24
+
25
+ // Match @-mentions
26
+ replacedText = reactReplaceString ( replacedText , / @ ( \w + ) / g, match => (
27
+ < a key = { match } href = { `https://twitter.com/${ match } ` } > @{ match } </ a >
28
+ ) ) ;
29
+
30
+ // Match hashtags
31
+ replacedText = reactReplaceString ( replacedText , / # ( \w + ) / g, match => (
32
+ < a key = { match } href = { `https://twitter.com/hashtag/${ match } ` } > #{ match } </ a >
33
+ ) ) ;
34
+
11
35
return (
12
36
< div >
13
- < h1 > Highlight Numbers</ h1 >
14
- { reactReplaceString ( this . props . content , / ( \d + ) / g, ( match , i ) => (
15
- < span key = { i } style = { { color : 'red' } } > { match } </ span >
16
- ) ) }
37
+ < h1 > React String Replace Demo</ h1 >
38
+ { replacedText }
17
39
</ div >
18
40
) ;
19
41
} ,
@@ -22,4 +44,4 @@ const HighlightNumbers = React.createClass({
22
44
const content = 'Hey my number is 555-555-5555.' ;
23
45
24
46
// Render the app
25
- render ( < HighlightNumbers content = { content } /> , document . getElementById ( 'root' ) ) ;
47
+ render ( < Demo content = { content } /> , document . getElementById ( 'root' ) ) ;
0 commit comments