Commit 223b7a33 authored by Mike ไมค์ Thuchchai.J's avatar Mike ไมค์ Thuchchai.J :speech_balloon:
Browse files

fixed nested text

No related merge requests found
Showing with 25 additions and 20 deletions
+25 -20
......@@ -6,7 +6,7 @@
* https://github.com/joshswan/react-native-autolink/blob/master/LICENSE
*/
import React, { Component, createElement } from 'react';
import React, { Component, createElement, Fragment } from 'react';
import PropTypes from 'prop-types'
import Autolinker from 'autolinker';
import { Alert, Linking, Platform, StyleSheet, Text } from 'react-native';
......@@ -18,14 +18,6 @@ const styles = StyleSheet.create({
});
export default class Autolink extends Component {
constructor(props) {
super(props)
if (Text.defaultProps == null) {
Text.defaultProps = {};
}
Text.defaultProps.allowFontScaling = false;
}
onPress(match, alertShown) {
// Check if alert needs to be shown
if (this.props.showAlert && !alertShown) {
......@@ -110,15 +102,16 @@ export default class Autolink extends Component {
}
}
renderLink(text, match, index) {
renderLink(text, match, index, textProps) {
const truncated = (this.props.truncate > 0) ?
Autolinker.truncate.TruncateSmart(text, this.props.truncate, this.props.truncateChars) :
text;
return (
<Text
{...textProps}
key={index}
style={[styles.link, this.props.linkStyle]}
style={this.props.linkStyle || styles.link}
onPress={() => this.onPress(match)}
>
{truncated}
......@@ -126,6 +119,17 @@ export default class Autolink extends Component {
);
}
renderNormalText(text, index, textProps) {
return (
<Text
{...textProps}
key={index}
>
{text}
</Text>
);
}
render() {
// Destructure props
/* eslint-disable no-unused-vars */
......@@ -146,7 +150,7 @@ export default class Autolink extends Component {
twitter,
url,
webFallback,
...other
...otherProps
} = this.props;
// Backwards compatibility for Twitter prop
......@@ -194,7 +198,7 @@ export default class Autolink extends Component {
.map((part, index) => {
const match = matches[part];
if (!match) return part;
if (!match) return this.renderNormalText(part, index, otherProps);
switch (match.getType()) {
case 'email':
......@@ -203,17 +207,18 @@ export default class Autolink extends Component {
case 'phone':
case 'url':
return (renderLink) ?
renderLink(match.getAnchorText(), match, index) :
this.renderLink(match.getAnchorText(), match, index);
renderLink(match.getAnchorText(), match, index, otherProps) :
this.renderLink(match.getAnchorText(), match, index, otherProps);
default:
return part;
return this.renderNormalText(part, index, otherProps);
}
});
return createElement(Text, {
ref: (component) => { this._root = component; }, // eslint-disable-line no-underscore-dangle
...other,
}, ...nodes);
return (
<Fragment>
{nodes}
</Fragment>
);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment