zl-react-native-gifted-chat
Version:
The most complete chat UI for React Native, based on 'react-native-gifted-chat'
130 lines (118 loc) • 3.09 kB
JavaScript
import React from 'react';
import {
Linking,
StyleSheet,
View,
Platform
} from 'react-native';
import ParsedText from 'react-native-parsed-text';
import Communications from 'react-native-communications';
export default class MessageText extends React.Component {
constructor(props) {
super(props);
this.onUrlPress = this.onUrlPress.bind(this);
this.onPhonePress = this.onPhonePress.bind(this);
this.onEmailPress = this.onEmailPress.bind(this);
}
onUrlPress(url) {
Linking.openURL(url);
}
onPhonePress(phone) {
if(Platform.OS === 'android'){
Communications.phonecall(phone, true)
return
}
const options = [
'拨通电话',
'发送短信',
'取消',
];
const cancelButtonIndex = options.length - 1;
this.context.actionSheet().showActionSheetWithOptions({
options,
cancelButtonIndex,
},
(buttonIndex) => {
switch (buttonIndex) {
case 0:
Communications.phonecall(phone, true);
break;
case 1:
Communications.text(phone);
break;
}
});
}
onEmailPress(email) {
Communications.email(email, null, null, null, null);
}
render() {
return (
<View style={[styles[this.props.position].container, this.props.containerStyle[this.props.position]]}>
<ParsedText
style={[styles[this.props.position].text, this.props.textStyle[this.props.position]]}
parse={[
{type: 'url', style: StyleSheet.flatten([styles[this.props.position].link, this.props.linkStyle[this.props.position]]), onPress: this.onUrlPress},
{type: 'phone', style: StyleSheet.flatten([styles[this.props.position].link, this.props.linkStyle[this.props.position]]), onPress: this.onPhonePress},
{type: 'email', style: StyleSheet.flatten([styles[this.props.position].link, this.props.linkStyle[this.props.position]]), onPress: this.onEmailPress},
]}
>
{this.props.currentMessage.text}
</ParsedText>
</View>
);
}
}
const textStyle = {
fontSize: 16,
lineHeight: 20,
marginTop: 5,
marginBottom: 5,
marginLeft: 10,
marginRight: 10,
};
const styles = {
left: StyleSheet.create({
container: {
},
text: {
color: 'black',
...textStyle,
},
link: {
color: 'black',
textDecorationLine: 'underline',
},
}),
right: StyleSheet.create({
container: {
},
text: {
color: 'white',
...textStyle,
},
link: {
color: 'white',
textDecorationLine: 'underline',
},
}),
};
MessageText.contextTypes = {
actionSheet: React.PropTypes.func,
};
MessageText.defaultProps = {
containerStyle: {},
position: 'left',
textStyle: {},
linkStyle: {},
currentMessage: {
text: '',
},
};
MessageText.propTypes = {
containerStyle: React.PropTypes.object,
position: React.PropTypes.oneOf(['left', 'right']),
textStyle: React.PropTypes.object,
linkStyle: React.PropTypes.object,
currentMessage: React.PropTypes.object,
};