Skip to content

Commit

Permalink
implements #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawan committed Jan 5, 2018
1 parent e9d4154 commit 971b866
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ export const defaultLink = () =>
</Hyperlink>

export const regularText = () =>
<Hyperlink onPress={ url => alert(url) }>
<Hyperlink onPress={ (url, text) => alert(url + ", " + text) }>
<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) }>
<Hyperlink onLongPress={ (url, text) => alert(url + ", " + text) }>
<Text style={ { fontSize: 15 } }>
This text will be parsed to check for clickable strings like https://github.com/obipawan/hyperlink and made clickable for long click.
</Text>
</Hyperlink>

export const nestedText = () =>
<Hyperlink onPress={ url => alert(url) }>
<Hyperlink onPress={ (url, text) => alert(url + ", " + text) }>
<View>
<Text style={ { fontSize: 15 } }>
A nested Text component https://facebook.github.io/react-native/docs/text.html works equally well <Text>with https://github.com/obipawan/hyperlink</Text>
Expand Down Expand Up @@ -82,4 +82,4 @@ PRs highly appreciated

License
----
MIT License
MIT License
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var Hyperlink = function (_Component) {

if (OS !== 'web') {
componentProps.onLongPress = function () {
return _this2.props.onLongPress && _this2.props.onLongPress(url);
return _this2.props.onLongPress && _this2.props.onLongPress(url, text);
};
}

Expand All @@ -122,7 +122,7 @@ var Hyperlink = function (_Component) {
key: url + index,
style: [component.props.style, _this2.props.linkStyle],
onPress: function onPress() {
return _this2.props.onPress && _this2.props.onPress(url);
return _this2.props.onPress && _this2.props.onPress(url, text);
}
}),
text
Expand Down
48 changes: 24 additions & 24 deletions src/Hyperlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ class Hyperlink extends Component {
const { ...viewProps } = this.props
delete viewProps.onPress
delete viewProps.linkDefault
delete viewProps.onLongPress
delete viewProps.linkStyle
delete viewProps.onLongPress
delete viewProps.linkStyle

return (
<View {...viewProps} style={this.props.style}>
{!this.props.onPress && !this.props.onLongPress && !this.props.linkStyle
<View { ...viewProps } style={ this.props.style }>
{ !this.props.onPress && !this.props.onLongPress && !this.props.linkStyle
? this.props.children
: this.parse(this).props.children}
: this.parse(this).props.children }
</View>
)
}

isTextNested(component) {
if (!React.isValidElement(component))
throw new Error('Invalid component')
if (!React.isValidElement(component))
throw new Error('Invalid component')
let { type: { displayName } = {} } = component
if (displayName !== 'Text')
throw new Error('Not a Text component')
if (displayName !== 'Text')
throw new Error('Not a Text component')
return typeof component.props.children !== 'string'
}

linkify(component){
if (
!this.linkifyIt.pretest(component.props.children)
|| !this.linkifyIt.test(component.props.children)
!this.linkifyIt.pretest(component.props.children)
|| !this.linkifyIt.test(component.props.children)
)
return component

Expand All @@ -78,15 +78,15 @@ class Hyperlink extends Component {
: this.props.linkText

if (OS !== 'web') {
componentProps.onLongPress = () => this.props.onLongPress && this.props.onLongPress(url)
componentProps.onLongPress = () => this.props.onLongPress && this.props.onLongPress(url, text)
}

elements.push(
<Text
{ ...componentProps }
key={ url + index }
style={ [ component.props.style, this.props.linkStyle ] }
onPress={ () => this.props.onPress && this.props.onPress(url) }
onPress={ () => this.props.onPress && this.props.onPress(url, text) }
>
{ text }
</Text>
Expand All @@ -101,8 +101,8 @@ class Hyperlink extends Component {

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

const componentProps = {
...component.props,
Expand All @@ -111,13 +111,13 @@ class Hyperlink extends Component {
}

return React.cloneElement(component, componentProps, React.Children.map(children, child => {
let { type : { displayName } = {} } = child
if (typeof child === 'string' && this.linkifyIt.pretest(child))
return this.linkify(<Text { ...componentProps } style={ component.props.style }>{ child }</Text>)
if (displayName === 'Text' && !this.isTextNested(child))
return this.linkify(child)
return this.parse(child)
}))
let { type : { displayName } = {} } = child
if (typeof child === 'string' && this.linkifyIt.pretest(child))
return this.linkify(<Text { ...componentProps } style={ component.props.style }>{ child }</Text>)
if (displayName === 'Text' && !this.isTextNested(child))
return this.linkify(child)
return this.parse(child)
}))
}
}

Expand All @@ -126,8 +126,8 @@ Hyperlink.propTypes = {
linkify: PropTypes.object,
linkStyle: textPropTypes.style,
linkText: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.string,
PropTypes.func,
]),
onPress: PropTypes.func,
onLongPress: PropTypes.func,
Expand Down

0 comments on commit 971b866

Please sign in to comment.