forked from FaridSafi/react-native-gifted-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble.js
132 lines (122 loc) · 3.31 KB
/
Bubble.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import ParsedText from 'react-native-parsed-text';
const styles = StyleSheet.create({
bubble: {
borderRadius: 15,
paddingLeft: 14,
paddingRight: 14,
paddingBottom: 10,
paddingTop: 8,
},
text: {
color: '#000',
},
textLeft: {
},
textRight: {
color: '#fff',
},
textCenter: {
textAlign: 'center',
},
bubbleLeft: {
marginRight: 70,
backgroundColor: '#e6e6eb',
alignSelf: 'flex-start',
},
bubbleRight: {
marginLeft: 70,
backgroundColor: '#007aff',
alignSelf: 'flex-end',
},
bubbleCenter: {
backgroundColor: '#007aff',
alignSelf: 'center',
},
bubbleError: {
backgroundColor: '#e01717',
},
});
export default class Bubble extends React.Component {
componentWillMount() {
Object.assign(styles, this.props.styles);
}
renderText(text = '', position) {
if (this.props.renderCustomText && this.props.renderCustomText(this.props) !==false) {
return this.props.renderCustomText(this.props);
}
if (this.props.parseText === true) {
return (
<ParsedText
style={[styles.text, (position === 'left' ? styles.textLeft : position === 'right' ? styles.textRight : styles.textCenter)]}
parse={
[
{
type: 'url',
style: {
textDecorationLine: 'underline',
},
onPress: this.props.handleUrlPress,
},
{
type: 'phone',
style: {
textDecorationLine: 'underline',
},
onPress: this.props.handlePhonePress,
},
{
type: 'email',
style: {
textDecorationLine: 'underline',
},
onPress: this.props.handleEmailPress,
},
]
}
>
{text}
</ParsedText>
);
}
return (
<Text style={[styles.text, (position === 'left' ? styles.textLeft : position === 'right' ? styles.textRight : styles.textCenter)]}>
{text}
</Text>
);
}
render() {
const flexStyle = {};
const realLength = function(str) {
return str.replace(/[^\x00-\xff]/g, "**").length; // [^\x00-\xff] - Matching non double byte character
};
if (this.props.text) {
if (realLength(this.props.text) > 40) {
flexStyle.flex = 1;
}
}
return (
<View style={[styles.bubble,
(this.props.position === 'left' ? styles.bubbleLeft : this.props.position === 'right' ? styles.bubbleRight : styles.bubbleCenter),
(this.props.status === 'ErrorButton' ? styles.bubbleError : null),
flexStyle]}
>
{this.props.name}
{this.renderText(this.props.text, this.props.position)}
</View>
);
}
}
Bubble.propTypes = {
position: React.PropTypes.oneOf(['left', 'right', 'center']),
status: React.PropTypes.string,
text: React.PropTypes.string,
renderCustomText: React.PropTypes.func,
styles: React.PropTypes.object,
parseText: React.PropTypes.bool,
name: React.PropTypes.element,
handleUrlPress: React.PropTypes.func,
handlePhonePress: React.PropTypes.func,
handleEmailPress: React.PropTypes.func,
};