Skip to content

Commit

Permalink
support Linking
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawan committed Jul 9, 2017
1 parent 2b0fb45 commit 0edf56d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
26 changes: 24 additions & 2 deletions Hyperlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
**/

import React, { Component, PropTypes } from 'react'
import { View, Text } from 'react-native'
import {
View,
Text,
Linking,
} from 'react-native'

const textPropTypes = Text.propTypes || {}

Expand Down Expand Up @@ -109,6 +113,7 @@ class Hyperlink extends Component {
}

Hyperlink.propTypes = {
linkDefault: PropTypes.bool,
linkify: PropTypes.object,
linkStyle: textPropTypes.style,
linkText: PropTypes.oneOfType([
Expand All @@ -119,4 +124,21 @@ Hyperlink.propTypes = {
onLongPress: PropTypes.func,
}

module.exports = Hyperlink
export default class extends Component {
constructor (props) {
super(props)
this.handleLink = this.handleLink.bind(this)
}

handleLink (url) {
Linking.canOpenURL(url)
.then(supported => supported && Linking.openURL(url))
}

render () {
const onPress = this.handleLink || this.props.onPress
if (this.props.linkDefault)
return <Hyperlink { ...this.props } onPress={ onPress }/>
return <Hyperlink { ...this.props } />
}
}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@ npm i --save react-native-hyperlink
| `linkText` | A string or a func to replace parsed text | `oneOfType([ string, func ])` |
| `onPress` | func to handle click over a clickable text with parsed text as arg | `func` |
| `onLongPress` | func to handle long click over a clickable text with parsed text as arg | `func` |
|`linkDefault`|A platform specific fallback to handle `onPress`. Uses [Linking](https://facebook.github.io/react-native/docs/linking.html). Disabled by default | `bool`

## Examples
Wrap any component that has `<Text>` (works for [nested ](https://facebook.github.io/react-native/docs/text.html#nested-text) text too) in it

```javascript
import Hyperlink from 'react-native-hyperlink'

export const defaultLink = () =>
<Hyperlink linkDefault={ true }>
<Text style={ { fontSize: 15 } }>
This text will be parsed to check for clickable strings like https://github.com/obipawan/hyperlink and made clickable.
</Text>
</Hyperlink>

export const regularText = () =>
<Hyperlink onPress={ url => alert(url) }>
<Text style={ { fontSize: 15 } }>
This text will be parsed to check for clickable strings like https://github.com/obipawan/hyperlink and made clickable.
</Text>
</Hyperlink>

export const regularTextLongPress = () =>
<Hyperlink onLongPress={ url => alert(url) }>
<Text style={ { fontSize: 15 } }>
Expand Down

0 comments on commit 0edf56d

Please sign in to comment.