|
| 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 | +}); |
0 commit comments