/
QuickReplies.tsx
200 lines (180 loc) · 4.71 KB
/
QuickReplies.tsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import {
Text,
StyleSheet,
View,
TouchableOpacity,
StyleProp,
ViewStyle,
} from 'react-native'
import { IMessage, Reply } from './types'
import Color from './Color'
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
flexWrap: 'wrap',
maxWidth: 300,
},
quickReply: {
justifyContent: 'center',
alignItems: 'center',
borderWidth: 1,
maxWidth: 160,
paddingVertical: 7,
paddingHorizontal: 12,
height: 50,
borderRadius: 13,
margin: 3,
},
sendLink: {
borderWidth: 0,
},
sendLinkText: {
color: Color.defaultBlue,
fontWeight: '600',
fontSize: 17,
},
})
export interface QuickRepliesProps {
nextMessage?: IMessage
currentMessage?: IMessage
color?: string
sendText?: string
quickReplyStyle?: StyleProp<ViewStyle>
onQuickReply?(reply: Reply[]): void
renderQuickReplySend?(): React.ReactNode
}
export interface QuickRepliesState {
replies: Reply[]
}
const sameReply = (currentReply: Reply) => (reply: Reply) =>
currentReply.value === reply.value
const diffReply = (currentReply: Reply) => (reply: Reply) =>
currentReply.value !== reply.value
export default class QuickReplies extends Component<
QuickRepliesProps,
QuickRepliesState
> {
static defaultProps = {
currentMessage: {
quickReplies: [],
},
onQuickReply: () => {},
color: Color.peterRiver,
sendText: 'Send',
keepReplies: false,
renderQuickReplySend: undefined,
quickReplyStyle: undefined,
}
static propTypes = {
currentMessage: PropTypes.object.isRequired,
onQuickReply: PropTypes.func,
color: PropTypes.string,
}
state = {
replies: [],
}
handlePress = (reply: Reply) => () => {
const { currentMessage } = this.props
const { replies } = this.state
if (currentMessage) {
const { type } = currentMessage.quickReplies!
switch (type) {
case 'radio': {
this.handleSend([reply])()
return
}
case 'checkbox': {
if (replies.find(sameReply(reply))) {
this.setState({
replies: this.state.replies.filter(diffReply(reply)),
})
} else {
this.setState({ replies: [...this.state.replies, reply] })
}
return
}
default: {
console.warn(`[GiftedChat.onQuickReply] unknown type: ` + type)
return
}
}
}
}
handleSend = (replies: Reply[]) => () => {
const { currentMessage } = this.props
if (this.props.onQuickReply) {
this.props.onQuickReply(
replies.map((reply: Reply) => ({
...reply,
messageId: currentMessage!._id,
})),
)
}
}
shouldComponentDisplay = () => {
const { currentMessage, nextMessage } = this.props
const hasReplies = !!currentMessage && !!currentMessage!.quickReplies
const hasNext = !!nextMessage && !!nextMessage!._id
if (hasReplies && !hasNext) {
return true
}
if (hasReplies && hasNext && currentMessage!.quickReplies!.keepIt) {
return true
}
return false
}
renderQuickReplySend = () => {
const { replies } = this.state
const { sendText, renderQuickReplySend: customSend } = this.props
return (
<TouchableOpacity
style={[styles.quickReply, styles.sendLink]}
onPress={this.handleSend(replies)}
>
{customSend ? (
customSend()
) : (
<Text style={styles.sendLinkText}>{sendText}</Text>
)}
</TouchableOpacity>
)
}
render() {
const { currentMessage, color, quickReplyStyle } = this.props
const { replies } = this.state
if (!this.shouldComponentDisplay()) {
return null
}
const { type } = currentMessage!.quickReplies!
return (
<View style={styles.container}>
{currentMessage!.quickReplies!.values.map((reply: Reply) => {
const selected = type === 'checkbox' && replies.find(sameReply(reply))
return (
<TouchableOpacity
onPress={this.handlePress(reply)}
style={[
styles.quickReply,
quickReplyStyle,
{ borderColor: color },
selected && { backgroundColor: color },
]}
key={reply.value}
>
<Text
numberOfLines={2}
ellipsizeMode={'tail'}
style={{ color: selected ? Color.white : color }}
>
{reply.title}
</Text>
</TouchableOpacity>
)
})}
{replies.length > 0 && this.renderQuickReplySend()}
</View>
)
}
}