Skip to content

Commit a6ad011

Browse files
deminothFacebook Github Bot 6
authored andcommitted
Add Share module
Summary: revision of facebook/react-native#5476 It has only one method `shareTextContent` and next will be`shareBinaryContent`. In Android, Promise can't receive a result, because `startActivityForResult` is not working with `Intent.ACTION_SEND`. Maybe we can use `createChooser(Intent target, CharSequence title, IntentSender sender)` which requires API level 22. Closes facebook/react-native#5904 Differential Revision: D3612889 fbshipit-source-id: 0e7aaf34b076a99089cc76bd649e6da067d9a760
1 parent 49b75e9 commit a6ad011

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

UIExplorer/js/ShareExample.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* The examples provided by Facebook are for non-commercial testing and
3+
* evaluation purposes only.
4+
*
5+
* Facebook reserves all rights not expressly granted.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
8+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
10+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
11+
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
12+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
*
14+
* @flow
15+
*/
16+
'use strict';
17+
18+
var React = require('react');
19+
var ReactNative = require('react-native');
20+
var {
21+
StyleSheet,
22+
View,
23+
Text,
24+
TouchableHighlight,
25+
Share,
26+
} = ReactNative;
27+
28+
exports.framework = 'React';
29+
exports.title = 'Share';
30+
exports.description = 'Share data with other Apps.';
31+
exports.examples = [{
32+
title: 'Share Text Content',
33+
render() {
34+
return <ShareMessageExample />;
35+
}
36+
}];
37+
38+
class ShareMessageExample extends React.Component {
39+
_shareMessage: Function;
40+
_shareText: Function;
41+
_showResult: Function;
42+
state: any;
43+
44+
constructor(props) {
45+
super(props);
46+
47+
this._shareMessage = this._shareMessage.bind(this);
48+
this._shareText = this._shareText.bind(this);
49+
this._showResult = this._showResult.bind(this);
50+
51+
this.state = {
52+
result: ''
53+
};
54+
}
55+
56+
render() {
57+
return (
58+
<View>
59+
<TouchableHighlight style={styles.wrapper}
60+
onPress={this._shareMessage}>
61+
<View style={styles.button}>
62+
<Text>Click to share message</Text>
63+
</View>
64+
</TouchableHighlight>
65+
<TouchableHighlight style={styles.wrapper}
66+
onPress={this._shareText}>
67+
<View style={styles.button}>
68+
<Text>Click to share message, URL and title</Text>
69+
</View>
70+
</TouchableHighlight>
71+
<Text>{this.state.result}</Text>
72+
</View>
73+
);
74+
}
75+
76+
_shareMessage() {
77+
Share.share({
78+
message: 'React Native | A framework for building native apps using React'
79+
})
80+
.then(this._showResult)
81+
.catch((error) => this.setState({result: 'error: ' + error.message}));
82+
}
83+
84+
_shareText() {
85+
Share.share({
86+
message: 'A framework for building native apps using React',
87+
url: 'http://facebook.github.io/react-native/',
88+
title: 'React Native'
89+
}, {
90+
dialogTitle: 'Share React Native website',
91+
excludedActivityTypes: [
92+
'com.apple.UIKit.activity.PostToTwitter'
93+
],
94+
tintColor: 'green'
95+
})
96+
.then(this._showResult)
97+
.catch((error) => this.setState({result: 'error: ' + error.message}));
98+
}
99+
100+
_showResult(result) {
101+
if (result.action === Share.sharedAction) {
102+
if (result.activityType) {
103+
this.setState({result: 'shared with an activityType: ' + result.activityType});
104+
} else {
105+
this.setState({result: 'shared'});
106+
}
107+
} else if (result.action === Share.dismissedAction) {
108+
this.setState({result: 'dismissed'});
109+
}
110+
}
111+
112+
}
113+
114+
115+
var styles = StyleSheet.create({
116+
wrapper: {
117+
borderRadius: 5,
118+
marginBottom: 5,
119+
},
120+
button: {
121+
backgroundColor: '#eeeeee',
122+
padding: 10,
123+
},
124+
});

UIExplorer/js/UIExplorerList.android.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ const APIExamples = [
181181
key: 'PointerEventsExample',
182182
module: require('./PointerEventsExample'),
183183
},
184+
{
185+
key: 'ShareExample',
186+
module: require('./ShareExample'),
187+
},
184188
{
185189
key: 'TimePickerAndroidExample',
186190
module: require('./TimePickerAndroidExample'),

UIExplorer/js/UIExplorerList.ios.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ const APIExamples: Array<UIExplorerExample> = [
247247
key: 'RCTRootViewIOSExample',
248248
module: require('./RCTRootViewIOSExample'),
249249
},
250+
{
251+
key: 'ShareExample',
252+
module: require('./ShareExample'),
253+
},
250254
{
251255
key: 'SnapshotExample',
252256
module: require('./SnapshotExample'),

0 commit comments

Comments
 (0)