zl-react-native-gifted-chat
Version:
The most complete chat UI for React Native, based on 'react-native-gifted-chat'
191 lines (176 loc) • 5 kB
JavaScript
import React from 'react';
import {
Clipboard,
StyleSheet,
TouchableWithoutFeedback,
View,
} from 'react-native';
import MessageText from './MessageText';
import MessageImage from './MessageImage';
import Time from './Time';
export default class Bubble extends React.Component {
constructor(props) {
super(props);
this.onLongPress = this.onLongPress.bind(this);
}
handleBubbleToNext() {
if (this.props.isSameUser(this.props.currentMessage, this.props.nextMessage) && this.props.isSameDay(this.props.currentMessage, this.props.nextMessage)) {
return StyleSheet.flatten([styles[this.props.position].containerToNext, this.props.containerToNextStyle[this.props.position]]);
}
return null;
}
handleBubbleToPrevious() {
if (this.props.isSameUser(this.props.currentMessage, this.props.previousMessage) && this.props.isSameDay(this.props.currentMessage, this.props.previousMessage)) {
return StyleSheet.flatten([styles[this.props.position].containerToPrevious, this.props.containerToPreviousStyle[this.props.position]]);
}
return null;
}
renderMessageText() {
if (this.props.currentMessage.text) {
if (this.props.renderMessageText) {
return this.props.renderMessageText(this.props);
}
return <MessageText {...this.props}/>;
}
return null;
}
renderMessageImage() {
if (this.props.currentMessage.image) {
if (this.props.renderMessageImage) {
return this.props.renderMessageImage(this.props);
}
return <MessageImage {...this.props}/>;
}
return null;
}
renderTime() {
if (this.props.currentMessage.createdAt) {
if (this.props.renderTime) {
return this.props.renderTime(this.props);
}
return <Time {...this.props}/>;
}
return null;
}
renderCustomView() {
if (this.props.renderCustomView) {
return this.props.renderCustomView(this.props);
}
return null;
}
onLongPress() {
if (this.props.currentMessage.text) {
const options = [
'复制文字',
'取消',
];
const cancelButtonIndex = options.length - 1;
this.context.actionSheet().showActionSheetWithOptions({
options,
cancelButtonIndex,
},
(buttonIndex) => {
switch (buttonIndex) {
case 0:
Clipboard.setString(this.props.currentMessage.text);
break;
}
});
}
}
render() {
return (
<View style={[styles[this.props.position].container, this.props.containerStyle[this.props.position]]}>
<View style={[styles[this.props.position].wrapper, this.props.wrapperStyle[this.props.position], this.handleBubbleToNext(), this.handleBubbleToPrevious()]}>
<TouchableWithoutFeedback
onLongPress={this.onLongPress}
>
<View>
{this.renderCustomView()}
{this.renderMessageImage()}
{this.renderMessageText()}
{this.renderTime()}
</View>
</TouchableWithoutFeedback>
</View>
</View>
);
}
}
const styles = {
left: StyleSheet.create({
container: {
alignItems: 'flex-start',
},
wrapper: {
borderRadius: 15,
backgroundColor: '#f0f0f0',
marginRight: 60,
minHeight: 20,
justifyContent: 'flex-end',
},
containerToNext: {
borderBottomLeftRadius: 3,
},
containerToPrevious: {
borderTopLeftRadius: 3,
},
}),
right: StyleSheet.create({
container: {
alignItems: 'flex-end',
},
wrapper: {
borderRadius: 15,
backgroundColor: '#0084ff',
marginLeft: 60,
minHeight: 20,
justifyContent: 'flex-end',
},
containerToNext: {
borderBottomRightRadius: 3,
},
containerToPrevious: {
borderTopRightRadius: 3,
},
}),
};
Bubble.contextTypes = {
actionSheet: React.PropTypes.func,
};
Bubble.defaultProps = {
containerStyle: {},
wrapperStyle: {},
containerToNextStyle: {},
containerToPreviousStyle: {},
renderMessageImage: null,
renderMessageText: null,
renderCustomView: null,
renderTime: null,
isSameUser: () => {},
isSameDay: () => {},
position: 'left',
currentMessage: {
text: null,
createdAt: null,
image: null,
},
nextMessage: {},
previousMessage: {},
};
Bubble.propTypes = {
containerStyle: React.PropTypes.object,
wrapperStyle: React.PropTypes.object,
containerToNextStyle: React.PropTypes.object,
containerToPreviousStyle: React.PropTypes.object,
renderMessageImage: React.PropTypes.func,
renderMessageText: React.PropTypes.func,
renderCustomView: React.PropTypes.func,
renderTime: React.PropTypes.func,
isSameUser: React.PropTypes.func,
isSameDay: React.PropTypes.func,
position: React.PropTypes.oneOf(['left', 'right']),
currentMessage: React.PropTypes.object,
nextMessage: React.PropTypes.object,
previousMessage: React.PropTypes.object,
};