1 | import PropTypes from 'prop-types';
|
2 | import React, { Component } from 'react';
|
3 | import { Text, StyleSheet, View, TouchableOpacity, } from 'react-native';
|
4 | import Color from './Color';
|
5 | import { warning, StylePropType } from './utils';
|
6 | const styles = StyleSheet.create({
|
7 | container: {
|
8 | flexDirection: 'row',
|
9 | flexWrap: 'wrap',
|
10 | maxWidth: 300,
|
11 | },
|
12 | quickReply: {
|
13 | justifyContent: 'center',
|
14 | alignItems: 'center',
|
15 | borderWidth: 1,
|
16 | maxWidth: 200,
|
17 | paddingVertical: 7,
|
18 | paddingHorizontal: 12,
|
19 | minHeight: 50,
|
20 | borderRadius: 13,
|
21 | margin: 3,
|
22 | },
|
23 | quickReplyText: {
|
24 | overflow: 'visible',
|
25 | },
|
26 | sendLink: {
|
27 | borderWidth: 0,
|
28 | },
|
29 | sendLinkText: {
|
30 | color: Color.defaultBlue,
|
31 | fontWeight: '600',
|
32 | fontSize: 17,
|
33 | },
|
34 | });
|
35 | const sameReply = (currentReply) => (reply) => currentReply.value === reply.value;
|
36 | const diffReply = (currentReply) => (reply) => currentReply.value !== reply.value;
|
37 | export default class QuickReplies extends Component {
|
38 | constructor() {
|
39 | super(...arguments);
|
40 | this.state = {
|
41 | replies: [],
|
42 | };
|
43 | this.handlePress = (reply) => () => {
|
44 | const { currentMessage } = this.props;
|
45 | const { replies } = this.state;
|
46 | if (currentMessage) {
|
47 | const { type } = currentMessage.quickReplies;
|
48 | switch (type) {
|
49 | case 'radio': {
|
50 | this.handleSend([reply])();
|
51 | return;
|
52 | }
|
53 | case 'checkbox': {
|
54 | if (replies.find(sameReply(reply))) {
|
55 | this.setState({
|
56 | replies: this.state.replies.filter(diffReply(reply)),
|
57 | });
|
58 | }
|
59 | else {
|
60 | this.setState({ replies: [...this.state.replies, reply] });
|
61 | }
|
62 | return;
|
63 | }
|
64 | default: {
|
65 | warning(`onQuickReply unknown type: ${type}`);
|
66 | return;
|
67 | }
|
68 | }
|
69 | }
|
70 | };
|
71 | this.handleSend = (replies) => () => {
|
72 | const { currentMessage } = this.props;
|
73 | if (this.props.onQuickReply) {
|
74 | this.props.onQuickReply(replies.map((reply) => ({
|
75 | ...reply,
|
76 | messageId: currentMessage._id,
|
77 | })));
|
78 | }
|
79 | };
|
80 | this.shouldComponentDisplay = () => {
|
81 | const { currentMessage, nextMessage } = this.props;
|
82 | const hasReplies = !!currentMessage && !!currentMessage.quickReplies;
|
83 | const hasNext = !!nextMessage && !!nextMessage._id;
|
84 | const keepIt = currentMessage.quickReplies.keepIt;
|
85 | if (hasReplies && !hasNext) {
|
86 | return true;
|
87 | }
|
88 | if (hasReplies && hasNext && keepIt) {
|
89 | return true;
|
90 | }
|
91 | return false;
|
92 | };
|
93 | this.renderQuickReplySend = () => {
|
94 | const { replies } = this.state;
|
95 | const { sendText, renderQuickReplySend: customSend } = this.props;
|
96 | return (<TouchableOpacity style={[styles.quickReply, styles.sendLink]} onPress={this.handleSend(replies)}>
|
97 | {customSend ? (customSend()) : (<Text style={styles.sendLinkText}>{sendText}</Text>)}
|
98 | </TouchableOpacity>);
|
99 | };
|
100 | }
|
101 | render() {
|
102 | const { currentMessage, color, quickReplyStyle } = this.props;
|
103 | const { replies } = this.state;
|
104 | if (!this.shouldComponentDisplay()) {
|
105 | return null;
|
106 | }
|
107 | const { type } = currentMessage.quickReplies;
|
108 | return (<View style={styles.container}>
|
109 | {currentMessage.quickReplies.values.map((reply, index) => {
|
110 | const selected = type === 'checkbox' && replies.find(sameReply(reply));
|
111 | return (<TouchableOpacity onPress={this.handlePress(reply)} style={[
|
112 | styles.quickReply,
|
113 | quickReplyStyle,
|
114 | { borderColor: color },
|
115 | selected && { backgroundColor: color },
|
116 | ]} key={`${reply.value}-${index}`}>
|
117 | <Text numberOfLines={10} ellipsizeMode={'tail'} style={[
|
118 | styles.quickReplyText,
|
119 | { color: selected ? Color.white : color },
|
120 | ]}>
|
121 | {reply.title}
|
122 | </Text>
|
123 | </TouchableOpacity>);
|
124 | })}
|
125 | {replies.length > 0 && this.renderQuickReplySend()}
|
126 | </View>);
|
127 | }
|
128 | }
|
129 | QuickReplies.defaultProps = {
|
130 | currentMessage: {
|
131 | quickReplies: [],
|
132 | },
|
133 | onQuickReply: () => { },
|
134 | color: Color.peterRiver,
|
135 | sendText: 'Send',
|
136 | keepReplies: false,
|
137 | renderQuickReplySend: undefined,
|
138 | quickReplyStyle: undefined,
|
139 | };
|
140 | QuickReplies.propTypes = {
|
141 | currentMessage: PropTypes.object.isRequired,
|
142 | onQuickReply: PropTypes.func,
|
143 | color: PropTypes.string,
|
144 | sendText: PropTypes.string,
|
145 | keepReplies: PropTypes.bool,
|
146 | renderQuickReplySend: PropTypes.func,
|
147 | quickReplyStyle: StylePropType,
|
148 | };
|
149 |
|
\ | No newline at end of file |