File tree 4 files changed +44
-2
lines changed
4 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -190,7 +190,7 @@ export const Autolink = React.memo(
190
190
const uid = useRef ( Math . floor ( Math . random ( ) * 0x10000000000 ) . toString ( 16 ) ) ;
191
191
const [ generateToken , tokenRegexp ] = makeTokenGenerator ( uid . current ) ;
192
192
193
- const matches : { [ token : string ] : Match } = { } ;
193
+ const matches : { [ token : string ] : Match | CustomMatch } = { } ;
194
194
let linkedText : string ;
195
195
196
196
try {
@@ -242,7 +242,11 @@ export const Autolink = React.memo(
242
242
243
243
// Check if rendering link or text node
244
244
if ( match ?. getType ( ) ) {
245
- return ( renderLinkProp || renderLink ) ( match . getAnchorText ( ) , match , index ) ;
245
+ return ( ( match as any ) . getRenderFn ?.( ) || renderLinkProp || renderLink ) (
246
+ match . getAnchorText ( ) ,
247
+ match ,
248
+ index ,
249
+ ) ;
246
250
}
247
251
248
252
return renderText ? (
Original file line number Diff line number Diff line change 1
1
import { Match , MatchConfig } from 'autolinker/dist/es2015' ;
2
+ import React from 'react' ;
2
3
import { StyleProp , TextStyle } from 'react-native' ;
3
4
4
5
// The variadic arguments of a regex replacer function, wrapped in an array.
@@ -20,6 +21,8 @@ export interface CustomMatcher {
20
21
getLinkText ?: ( replacerArgs : ReplacerArgs ) => string ;
21
22
/* Custom function for extracting link URL using regex replacer args */
22
23
getLinkUrl ?: ( replacerArgs : ReplacerArgs ) => string ;
24
+ /* Custom function for rendering link - remember to attach press handlers! */
25
+ renderLink ?: ( text : string , match : CustomMatch , index : number ) => React . ReactNode ;
23
26
}
24
27
25
28
export interface CustomMatchConfig extends MatchConfig {
@@ -57,4 +60,8 @@ export class CustomMatch extends Match {
57
60
getReplacerArgs ( ) : ReplacerArgs {
58
61
return this . replacerArgs ;
59
62
}
63
+
64
+ getRenderFn ( ) : CustomMatcher [ 'renderLink' ] {
65
+ return this . matcher . renderLink ;
66
+ }
60
67
}
Original file line number Diff line number Diff line change @@ -342,5 +342,26 @@ describe('<Autolink />', () => {
342
342
expect ( onPress . mock . calls [ 0 ] [ 0 ] ) . toBe ( '__LINK_URL__' ) ;
343
343
expect ( onPress . mock . calls [ 0 ] [ 1 ] ) . toBeInstanceOf ( CustomMatch ) ;
344
344
} ) ;
345
+
346
+ test ( 'uses renderLink when rendering link' , ( ) => {
347
+ const onPress = jest . fn ( ) ;
348
+ const renderLink = jest
349
+ . fn ( )
350
+ . mockImplementation ( ( text : string ) => < Text onPress = { onPress } > { text } </ Text > ) ;
351
+ const tree = renderer . create (
352
+ < Autolink
353
+ text = "+14085550123"
354
+ onPress = { onPress }
355
+ matchers = { [ { ...IntlPhoneMatcher , renderLink } ] }
356
+ /> ,
357
+ ) ;
358
+ expect ( renderLink . mock . calls . length ) . toBe ( 1 ) ;
359
+ expect ( renderLink . mock . calls [ 0 ] [ 0 ] ) . toBe ( '+14085550123' ) ;
360
+ expect ( renderLink . mock . calls [ 0 ] [ 1 ] ) . toBeInstanceOf ( CustomMatch ) ;
361
+ expect ( renderLink . mock . calls [ 0 ] [ 2 ] ) . toBe ( 0 ) ;
362
+ expect ( tree ) . toMatchSnapshot ( ) ;
363
+ tree . root . findAllByType ( Text ) [ 1 ] . props . onPress ( ) ;
364
+ expect ( onPress . mock . calls . length ) . toBe ( 1 ) ;
365
+ } ) ;
345
366
} ) ;
346
367
} ) ;
Original file line number Diff line number Diff line change @@ -190,6 +190,16 @@ exports[`<Autolink /> matchers uses getLinkText when rendering link 1`] = `
190
190
</Text >
191
191
` ;
192
192
193
+ exports [` <Autolink /> matchers uses renderLink when rendering link 1` ] = `
194
+ <Text >
195
+ <Text
196
+ onPress = { [MockFunction ]}
197
+ >
198
+ +14085550123
199
+ </Text >
200
+ </Text >
201
+ ` ;
202
+
193
203
exports [` <Autolink /> matchers wraps text based on supplied custom matchers 1` ] = `
194
204
<Text >
195
205
<Text
You can’t perform that action at this time.
0 commit comments