Skip to content

Commit acab131

Browse files
committed
Add renderText method
1 parent 6fdc9f0 commit acab131

File tree

6 files changed

+65
-10
lines changed

6 files changed

+65
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
lib
33
assets
4+
.idea

Example/index.ios.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const {
1111
View,
1212
LinkingIOS,
1313
AlertIOS,
14-
} = React;
14+
} = React;
1515

1616
import ParsedText from 'react-native-parsed-text';
1717

@@ -34,6 +34,10 @@ class Example extends React.Component {
3434
AlertIOS.alert(`send email to ${email}`);
3535
}
3636

37+
renderText(string) {
38+
return `^^${string}^^`;
39+
}
40+
3741
render() {
3842
return (
3943
<View style={styles.container}>
@@ -45,11 +49,12 @@ class Example extends React.Component {
4549
{type: 'phone', style: styles.phone, onPress: this.handlePhonePress},
4650
{type: 'email', style: styles.email, onPress: this.handleEmailPress},
4751
{pattern: /Bob|David/, style: styles.name, onPress: this.handleNamePress},
52+
{pattern: /Bob|David/, style: styles.name, onPress: this.handleNamePress, renderText: this.renderText},
4853
{pattern: /42/, style: styles.magicNumber},
4954
{pattern: /#(\w+)/, style: styles.hashTag},
5055
]
5156
}
52-
>
57+
>
5358
Hello this is an example of the ParsedText, links like http://www.google.com or http://www.facebook.com are clickable and phone number 444-555-6666 can call too.
5459
But you can also do more with this package, for example Bob will change style and David too. [email protected]
5560
And the magic number is 42!

README.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,24 @@ class Example extends React.Component {
3535
AlertIOS.alert(`send email to ${email}`);
3636
}
3737

38+
renderText(string) {
39+
return `^^${string}^^`;
40+
}
41+
3842
render() {
3943
return (
4044
<View style={styles.container}>
4145
<ParsedText
4246
style={styles.text}
4347
parse={
4448
[
45-
{type: 'url', style: styles.url, onPress: this.handleUrlPress},
46-
{type: 'phone', style: styles.phone, onPress: this.handlePhonePress},
47-
{type: 'email', style: styles.email, onPress: this.handleEmailPress},
48-
{pattern: /Bob|David/, style: styles.name, onPress: this.handleNamePress},
49-
{pattern: /42/, style: styles.magicNumber},
50-
{pattern: /#(\w+)/, style: styles.hashTag},
49+
{type: 'url', style: styles.url, onPress: this.handleUrlPress},
50+
{type: 'phone', style: styles.phone, onPress: this.handlePhonePress},
51+
{type: 'email', style: styles.email, onPress: this.handleEmailPress},
52+
{pattern: /Bob|David/, style: styles.name, onPress: this.handleNamePress},
53+
{pattern: /Bob|David/, style: styles.name, onPress: this.handleNamePress, renderChildren: this.renderText},
54+
{pattern: /42/, style: styles.magicNumber},
55+
{pattern: /#(\w+)/, style: styles.hashTag},
5156
]
5257
}
5358
>
@@ -110,6 +115,13 @@ const styles = StyleSheet.create({
110115

111116
`npm install --save react-native-parsed-text`
112117

118+
## Test using mocha
119+
120+
```
121+
mocha --compilers js:babel/register
122+
```
123+
113124
## TODO
114125

115126
* Add nested text parsing
127+

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-parsed-text",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Parse text and make them into multiple React Native Text elements",
55
"main": "lib/ParsedText.js",
66
"scripts": {

src/lib/TextExtraction.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,14 @@ class TextExtraction {
7979
}
8080
});
8181

82+
var children = text;
83+
if (props.renderText && typeof props.renderText === 'function') {
84+
children = props.renderText(children);
85+
}
86+
8287
return {
8388
...props,
84-
children: text,
89+
children: children,
8590
_matched: true,
8691
};
8792
}

test/TextExtraction.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,39 @@ describe('TextExtraction', () => {
8686
{children: ' is good.'},
8787
]);
8888
});
89+
});
90+
91+
describe('renderText prop', () => {
92+
it('checks that renderText is a function', (done) => {
93+
const textExtraction = new TextExtraction('hello Bob, David, Michel', [
94+
{ pattern: /Bob|David/, renderText: 'foo'}
95+
]);
96+
97+
const parsedText = textExtraction.parse();
98+
99+
expect(parsedText[0]).to.eql({children: 'hello '});
100+
expect(parsedText[1].children).to.eql('Bob');
101+
expect(parsedText[2].children).to.eql(', ');
102+
expect(parsedText[3].children).to.eql('David');
103+
expect(parsedText[4].children).to.eql(', Michel');
104+
105+
done();
106+
});
107+
it('pass the values to the callbacks', (done) => {
108+
const textExtraction = new TextExtraction('hello Bob, David, Michel', [
109+
{ pattern: /Bob|David/, renderText: (string) => { return `^^${string}^^`}}
110+
]);
111+
112+
const parsedText = textExtraction.parse();
113+
114+
expect(parsedText[0]).to.eql({children: 'hello '});
115+
expect(parsedText[1].children).to.eql('^^Bob^^');
116+
expect(parsedText[2].children).to.eql(', ');
117+
expect(parsedText[3].children).to.eql('^^David^^');
118+
expect(parsedText[4].children).to.eql(', Michel');
89119

120+
done();
121+
});
90122
});
91123

92124
});

0 commit comments

Comments
 (0)