Skip to content

Commit b3f047e

Browse files
nmquebbjoshswan
authored andcommitted
Add showAlert prop for displaying an alert before leaving app (#13)
1 parent 7c002c9 commit b3f047e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class MyComponent extends Component {
4646
| `truncate` | `Number` | `32` | Truncate long link text for display (e.g. `https://www.google.com/../something.html`). Possible values: `0` to disable, `1+` to truncate to that maximum length. |
4747
| `truncateChars` | `String` | `..` | Characters to replace truncated url segments with, if enabled. |
4848
| `webFallback` | `Boolean` | Android: `true` iOS: `false` | Link to web versions of Instagram/Twitter for hashtag and mention links when users don't have the respective app installed. *Requires `LSApplicationQueriesSchemes` on iOS. See: https://facebook.github.io/react-native/docs/linking.html* |
49+
| `showAlert` | `Boolean` | `false` | Displays an alert before leaving the app to help with accidental clicks. Possible values: `true`, `false` |
4950

5051
**Any other props will be passed through to the main Text node (e.g. style, numberOfLines).**
5152

src/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import React, {Component, PropTypes, createElement} from 'react';
1111
import Autolinker from 'autolinker';
12-
import {Linking, Platform, StyleSheet, Text} from 'react-native';
12+
import {Alert, Linking, Platform, StyleSheet, Text} from 'react-native';
1313

1414
export default class Autolink extends Component {
1515
getURL(match) {
@@ -73,14 +73,29 @@ export default class Autolink extends Component {
7373
}
7474
}
7575

76+
_handlePress(url, match) {
77+
if (this.props.showAlert) {
78+
Alert.alert(
79+
'Leaving App',
80+
'Do you want to continue?',
81+
[
82+
{ text: 'Cancel', style: 'cancel' },
83+
{ text: 'OK', onPress: () => this._onPress(url, match)},
84+
]
85+
)
86+
} else {
87+
this._onPress(url, match)
88+
}
89+
}
90+
7691
renderLink(text, url, match, index) {
7792
let truncated = (this.props.truncate > 0) ? Autolinker.truncate.TruncateSmart(text, this.props.truncate, this.props.truncateChars) : text;
7893

7994
return (
8095
<Text
8196
key={index}
8297
style={[styles.link, this.props.linkStyle]}
83-
onPress={this._onPress.bind(this, url, match)}>
98+
onPress={this._handlePress.bind(this, url, match)}>
8499
{truncated}
85100
</Text>
86101
);
@@ -188,6 +203,7 @@ Autolink.defaultProps = {
188203
twitter: false,
189204
url: true,
190205
webFallback: Platform.OS !== 'ios', // iOS requires LSApplicationQueriesSchemes for Linking.canOpenURL
206+
showAlert: false,
191207
};
192208

193209
Autolink.propTypes = {
@@ -206,4 +222,5 @@ Autolink.propTypes = {
206222
twitter: PropTypes.bool,
207223
url: PropTypes.bool,
208224
webFallback: PropTypes.bool,
225+
showAlert: PropTypes.bool,
209226
};

0 commit comments

Comments
 (0)