Skip to content

Commit

Permalink
fix issue with linkify default prop
Browse files Browse the repository at this point in the history
  • Loading branch information
pawan_m02111 committed Dec 11, 2019
1 parent 980b411 commit 6081be5
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/Hyperlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,15 @@ import {
} from 'react-native'
import mdurl from 'mdurl'

const linkify = require('linkify-it')()

const textPropTypes = Text.propTypes || {}
const { OS } = Platform

class Hyperlink extends Component {
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.linkify !== prevState.linkifyIt) {
return {
linkifyIt: nextProps.linkify
};
}

return null
}

constructor(props){
constructor(props) {
super(props)

this.state = {
linkifyIt: props.linkify || require('linkify-it')()
};
this.state = { linkifyIt: props.linkify || linkify }
}

render() {
Expand Down Expand Up @@ -114,10 +103,7 @@ class Hyperlink extends Component {
}

parse = component => {
let {
props: { children } = {},
type: { displayName } = {},
} = component || {}
let { props: { children } = {} } = component || {}
if (!children)
return component

Expand Down Expand Up @@ -150,25 +136,31 @@ Hyperlink.propTypes = {
onLongPress: PropTypes.func,
}

Hyperlink.defaultProps = { linkify }

Hyperlink.getDerivedStateFromProps = (nextProps, prevState) => (nextProps.linkify !== prevState.linkifyIt)
? { linkifyIt: nextProps.linkify }
: null

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

handleLink (url) {
const urlObject = mdurl.parse(url);
urlObject.protocol = urlObject.protocol.toLowerCase();
const urlObject = mdurl.parse(url)
urlObject.protocol = urlObject.protocol.toLowerCase()
const normalizedURL = mdurl.format(urlObject)

Linking.canOpenURL(normalizedURL)
.then(supported => supported && Linking.openURL(normalizedURL));
.then(supported => supported && Linking.openURL(normalizedURL))
}

render () {
const onPress = this.handleLink || this.props.onPress
if (this.props.linkDefault)
return <Hyperlink { ...this.props } onPress={ onPress }/>
if (this.props.linkDefault)
return <Hyperlink { ...this.props } onPress={ onPress }/>
return <Hyperlink { ...this.props } />
}
}

0 comments on commit 6081be5

Please sign in to comment.