react-native-gifted-chat 3.2.3 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ import { ImageStyle, StyleProp, TextStyle, ViewStyle } from 'react-native'
2
+ import { SharedValue } from 'react-native-reanimated'
3
+
4
+ import { MessageReplyProps } from '../components/MessageReply'
5
+ import { ReplyPreviewProps } from '../components/ReplyPreview'
6
+ import { IMessage, ReplyMessage } from '../Models'
7
+
8
+ /**
9
+ * Props for swipe-to-reply gesture behavior
10
+ */
11
+ export interface SwipeToReplyProps<TMessage extends IMessage> {
12
+ /** Enable swipe to reply on messages; default is false */
13
+ isEnabled?: boolean
14
+ /** Direction to swipe for reply; default is 'left' (swipe left, icon appears on right) */
15
+ direction?: 'left' | 'right'
16
+ /** Callback when swipe to reply is triggered */
17
+ onSwipe?: (message: TMessage) => void
18
+ /** Custom render for swipe action indicator */
19
+ renderAction?: (
20
+ progress: SharedValue<number>,
21
+ translation: SharedValue<number>,
22
+ position: 'left' | 'right'
23
+ ) => React.ReactNode
24
+ /** Style for the swipe action container */
25
+ actionContainerStyle?: StyleProp<ViewStyle>
26
+ }
27
+
28
+ /**
29
+ * Props for reply preview shown above input toolbar
30
+ */
31
+ export interface ReplyPreviewStyleProps {
32
+ /** Style for reply preview container */
33
+ containerStyle?: StyleProp<ViewStyle>
34
+ /** Style for reply preview text */
35
+ textStyle?: StyleProp<TextStyle>
36
+ /** Style for reply preview image */
37
+ imageStyle?: StyleProp<ImageStyle>
38
+ }
39
+
40
+ /**
41
+ * Props for message reply display inside bubble
42
+ */
43
+ export interface MessageReplyStyleProps {
44
+ /** Style for message reply container */
45
+ containerStyle?: StyleProp<ViewStyle>
46
+ /** Style for message reply container on left side */
47
+ containerStyleLeft?: StyleProp<ViewStyle>
48
+ /** Style for message reply container on right side */
49
+ containerStyleRight?: StyleProp<ViewStyle>
50
+ /** Style for message reply image */
51
+ imageStyle?: StyleProp<ImageStyle>
52
+ /** Style for message reply text */
53
+ textStyle?: StyleProp<TextStyle>
54
+ /** Style for message reply text on left side */
55
+ textStyleLeft?: StyleProp<TextStyle>
56
+ /** Style for message reply text on right side */
57
+ textStyleRight?: StyleProp<TextStyle>
58
+ }
59
+
60
+ /**
61
+ * Grouped props for reply functionality
62
+ */
63
+ export interface ReplyProps<TMessage extends IMessage> {
64
+ /** Reply message to show in input toolbar preview */
65
+ message?: ReplyMessage | null
66
+ /** Callback when reply is cleared */
67
+ onClear?: () => void
68
+ /** Callback when message reply is pressed inside bubble */
69
+ onPress?: (replyMessage: ReplyMessage) => void
70
+ /** Custom render for reply preview in input toolbar */
71
+ renderPreview?: (props: ReplyPreviewProps) => React.ReactNode
72
+ /** Custom render for message reply inside bubble */
73
+ renderMessageReply?: (props: MessageReplyProps<TMessage>) => React.ReactNode
74
+ /** Swipe-to-reply configuration */
75
+ swipe?: SwipeToReplyProps<TMessage>
76
+ /** Reply preview styling */
77
+ previewStyle?: ReplyPreviewStyleProps
78
+ /** Message reply styling */
79
+ messageStyle?: MessageReplyStyleProps
80
+ }
@@ -0,0 +1,132 @@
1
+ import React, { useMemo } from 'react'
2
+ import { StyleSheet, View, StyleProp, ViewStyle, TextStyle, Pressable } from 'react-native'
3
+
4
+ import { Text } from 'react-native-gesture-handler'
5
+ import { Color } from './Color'
6
+ import { useColorScheme } from './hooks/useColorScheme'
7
+ import { ReplyMessage } from './Models'
8
+
9
+ export interface ReplyPreviewProps {
10
+ replyMessage: ReplyMessage
11
+ onClearReply: () => void
12
+ containerStyle?: StyleProp<ViewStyle>
13
+ usernameStyle?: StyleProp<TextStyle>
14
+ textStyle?: StyleProp<TextStyle>
15
+ clearButtonStyle?: StyleProp<ViewStyle>
16
+ clearButtonTextStyle?: StyleProp<TextStyle>
17
+ }
18
+
19
+ export function ReplyPreview ({
20
+ replyMessage,
21
+ onClearReply,
22
+ containerStyle,
23
+ usernameStyle,
24
+ textStyle,
25
+ clearButtonStyle,
26
+ clearButtonTextStyle,
27
+ }: ReplyPreviewProps) {
28
+ const colorScheme = useColorScheme()
29
+
30
+ const containerStyles = useMemo(() => [
31
+ styles.container,
32
+ colorScheme === 'dark' && styles.container_dark,
33
+ containerStyle,
34
+ ], [colorScheme, containerStyle])
35
+
36
+ const usernameStyles = useMemo(() => [
37
+ styles.username,
38
+ colorScheme === 'dark' && styles.username_dark,
39
+ usernameStyle,
40
+ ], [colorScheme, usernameStyle])
41
+
42
+ const textStyles = useMemo(() => [
43
+ styles.text,
44
+ colorScheme === 'dark' && styles.text_dark,
45
+ textStyle,
46
+ ], [colorScheme, textStyle])
47
+
48
+ return (
49
+ <View style={containerStyles}>
50
+ <View style={styles.border} />
51
+ <View style={styles.content}>
52
+ <Text
53
+ style={usernameStyles}
54
+ numberOfLines={1}
55
+ >
56
+ {replyMessage.user?.name || 'User'}
57
+ </Text>
58
+ <Text
59
+ style={textStyles}
60
+ numberOfLines={1}
61
+ >
62
+ {replyMessage.text || (replyMessage.image ? 'Photo' : (replyMessage.audio ? 'Audio' : 'Message'))}
63
+ </Text>
64
+ </View>
65
+ <Pressable
66
+ onPress={onClearReply}
67
+ style={[styles.clearButton, clearButtonStyle]}
68
+ hitSlop={8}
69
+ >
70
+ <Text style={[styles.clearButtonText, clearButtonTextStyle]}>
71
+ {'✕'}
72
+ </Text>
73
+ </Pressable>
74
+ </View>
75
+ )
76
+ }
77
+
78
+ const styles = StyleSheet.create({
79
+ container: {
80
+ flexDirection: 'row',
81
+ alignItems: 'center',
82
+ paddingHorizontal: 10,
83
+ paddingVertical: 8,
84
+ backgroundColor: '#f5f5f5',
85
+ borderBottomWidth: StyleSheet.hairlineWidth,
86
+ borderBottomColor: Color.defaultColor,
87
+ },
88
+ container_dark: {
89
+ backgroundColor: '#2a2a2a',
90
+ borderBottomColor: '#444',
91
+ },
92
+ border: {
93
+ width: 3,
94
+ height: '100%',
95
+ backgroundColor: Color.defaultBlue,
96
+ borderRadius: 1.5,
97
+ marginRight: 10,
98
+ },
99
+ content: {
100
+ flex: 1,
101
+ },
102
+ username: {
103
+ fontSize: 13,
104
+ fontWeight: '600',
105
+ color: Color.defaultBlue,
106
+ marginBottom: 2,
107
+ },
108
+ username_dark: {
109
+ color: '#6eb5ff',
110
+ },
111
+ text: {
112
+ fontSize: 13,
113
+ color: '#666',
114
+ },
115
+ text_dark: {
116
+ color: '#999',
117
+ },
118
+ clearButton: {
119
+ width: 24,
120
+ height: 24,
121
+ borderRadius: 12,
122
+ backgroundColor: Color.defaultColor,
123
+ justifyContent: 'center',
124
+ alignItems: 'center',
125
+ marginLeft: 10,
126
+ },
127
+ clearButtonText: {
128
+ fontSize: 12,
129
+ fontWeight: '600',
130
+ color: '#666',
131
+ },
132
+ })
package/src/Send.tsx CHANGED
@@ -21,7 +21,10 @@ export interface SendProps<TMessage extends IMessage> {
21
21
  containerStyle?: StyleProp<ViewStyle>
22
22
  textStyle?: StyleProp<TextStyle>
23
23
  children?: React.ReactNode
24
+ /** Always show send button, even when text is empty */
24
25
  isSendButtonAlwaysVisible?: boolean
26
+ /** Text is optional, allow sending empty messages (useful for media-only messages) */
27
+ isTextOptional?: boolean
25
28
  sendButtonProps?: Partial<TouchableOpacityProps>
26
29
  onSend?(
27
30
  messages: Partial<TMessage> | Partial<TMessage>[],
@@ -36,6 +39,7 @@ export const Send = <TMessage extends IMessage = IMessage>({
36
39
  textStyle,
37
40
  label = 'Send',
38
41
  isSendButtonAlwaysVisible = false,
42
+ isTextOptional = false,
39
43
  sendButtonProps,
40
44
  onSend,
41
45
  }: SendProps<TMessage>) => {
@@ -43,11 +47,12 @@ export const Send = <TMessage extends IMessage = IMessage>({
43
47
  const opacity = useSharedValue(0)
44
48
 
45
49
  const handleOnPress = useCallback(() => {
46
- const message = { text: text?.trim() } as Partial<TMessage>
50
+ const trimmedText = text?.trim() ?? ''
51
+ const message = { text: trimmedText } as Partial<TMessage>
47
52
 
48
- if (onSend && message.text?.length)
53
+ if (onSend && (trimmedText.length || isTextOptional))
49
54
  onSend(message, true)
50
- }, [text, onSend])
55
+ }, [text, onSend, isTextOptional])
51
56
 
52
57
  const isVisible = useMemo(
53
58
  () => isSendButtonAlwaysVisible || !!text?.trim().length,
@@ -0,0 +1,54 @@
1
+ import React from 'react'
2
+ import { render } from '@testing-library/react-native'
3
+
4
+ import { MessageReply } from '../components/MessageReply'
5
+ import { IMessage, ReplyMessage } from '../Models'
6
+
7
+ const replyMessage: ReplyMessage = {
8
+ _id: 'reply-1',
9
+ text: 'Original message text',
10
+ user: {
11
+ _id: 2,
12
+ name: 'John Doe',
13
+ },
14
+ }
15
+
16
+ const currentMessage: IMessage = {
17
+ _id: 'msg-1',
18
+ text: 'Reply text',
19
+ createdAt: new Date(),
20
+ user: {
21
+ _id: 1,
22
+ name: 'Jane Doe',
23
+ },
24
+ replyMessage,
25
+ }
26
+
27
+ it('should render <MessageReply /> and compare with snapshot', () => {
28
+ const { toJSON } = render(
29
+ <MessageReply
30
+ replyMessage={replyMessage}
31
+ currentMessage={currentMessage}
32
+ position='left'
33
+ />
34
+ )
35
+
36
+ expect(toJSON()).toMatchSnapshot()
37
+ })
38
+
39
+ it('should render <MessageReply /> on right position and compare with snapshot', () => {
40
+ const currentMessageFromCurrentUser: IMessage = {
41
+ ...currentMessage,
42
+ user: replyMessage.user,
43
+ }
44
+
45
+ const { toJSON } = render(
46
+ <MessageReply
47
+ replyMessage={replyMessage}
48
+ currentMessage={currentMessageFromCurrentUser}
49
+ position='right'
50
+ />
51
+ )
52
+
53
+ expect(toJSON()).toMatchSnapshot()
54
+ })
@@ -0,0 +1,41 @@
1
+ import React from 'react'
2
+ import { render } from '@testing-library/react-native'
3
+
4
+ import { ReplyPreview } from '../components/ReplyPreview'
5
+ import { ReplyMessage } from '../Models'
6
+
7
+ const replyMessage: ReplyMessage = {
8
+ _id: 'reply-1',
9
+ text: 'Original message to reply to',
10
+ user: {
11
+ _id: 2,
12
+ name: 'John Doe',
13
+ },
14
+ }
15
+
16
+ it('should render <ReplyPreview /> and compare with snapshot', () => {
17
+ const { toJSON } = render(
18
+ <ReplyPreview
19
+ replyMessage={replyMessage}
20
+ onClearReply={() => {}}
21
+ />
22
+ )
23
+
24
+ expect(toJSON()).toMatchSnapshot()
25
+ })
26
+
27
+ it('should render <ReplyPreview /> with image and compare with snapshot', () => {
28
+ const replyWithImage: ReplyMessage = {
29
+ ...replyMessage,
30
+ image: 'https://example.com/image.jpg',
31
+ }
32
+
33
+ const { toJSON } = render(
34
+ <ReplyPreview
35
+ replyMessage={replyWithImage}
36
+ onClearReply={() => {}}
37
+ />
38
+ )
39
+
40
+ expect(toJSON()).toMatchSnapshot()
41
+ })
@@ -20,27 +20,36 @@ exports[`should render <GiftedChat/> and compare with snapshot 1`] = `
20
20
  }
21
21
  >
22
22
  <View
23
- behavior="padding"
23
+ onLayout={[Function]}
24
24
  style={
25
- {
26
- "flex": 1,
27
- }
25
+ [
26
+ {
27
+ "flex": 1,
28
+ },
29
+ {
30
+ "overflow": "hidden",
31
+ },
32
+ ]
28
33
  }
34
+ testID="GC_WRAPPER"
29
35
  >
30
36
  <View
31
- onLayout={[Function]}
37
+ behavior="padding"
38
+ keyboardVerticalOffset={50}
32
39
  style={
33
- [
40
+ {
41
+ "flex": 1,
42
+ }
43
+ }
44
+ >
45
+ <View
46
+ style={
34
47
  {
35
48
  "flex": 1,
36
- },
37
- {
38
- "overflow": "hidden",
39
- },
40
- ]
41
- }
42
- testID="GC_WRAPPER"
43
- />
49
+ }
50
+ }
51
+ />
52
+ </View>
44
53
  </View>
45
54
  </View>
46
55
  </KeyboardProvider>
@@ -67,27 +76,36 @@ exports[`should render <GiftedChat/> with dark colorScheme and compare with snap
67
76
  }
68
77
  >
69
78
  <View
70
- behavior="padding"
79
+ onLayout={[Function]}
71
80
  style={
72
- {
73
- "flex": 1,
74
- }
81
+ [
82
+ {
83
+ "flex": 1,
84
+ },
85
+ {
86
+ "overflow": "hidden",
87
+ },
88
+ ]
75
89
  }
90
+ testID="GC_WRAPPER"
76
91
  >
77
92
  <View
78
- onLayout={[Function]}
93
+ behavior="padding"
94
+ keyboardVerticalOffset={50}
79
95
  style={
80
- [
96
+ {
97
+ "flex": 1,
98
+ }
99
+ }
100
+ >
101
+ <View
102
+ style={
81
103
  {
82
104
  "flex": 1,
83
- },
84
- {
85
- "overflow": "hidden",
86
- },
87
- ]
88
- }
89
- testID="GC_WRAPPER"
90
- />
105
+ }
106
+ }
107
+ />
108
+ </View>
91
109
  </View>
92
110
  </View>
93
111
  </KeyboardProvider>
@@ -114,27 +132,36 @@ exports[`should render <GiftedChat/> with light colorScheme and compare with sna
114
132
  }
115
133
  >
116
134
  <View
117
- behavior="padding"
135
+ onLayout={[Function]}
118
136
  style={
119
- {
120
- "flex": 1,
121
- }
137
+ [
138
+ {
139
+ "flex": 1,
140
+ },
141
+ {
142
+ "overflow": "hidden",
143
+ },
144
+ ]
122
145
  }
146
+ testID="GC_WRAPPER"
123
147
  >
124
148
  <View
125
- onLayout={[Function]}
149
+ behavior="padding"
150
+ keyboardVerticalOffset={50}
126
151
  style={
127
- [
152
+ {
153
+ "flex": 1,
154
+ }
155
+ }
156
+ >
157
+ <View
158
+ style={
128
159
  {
129
160
  "flex": 1,
130
- },
131
- {
132
- "overflow": "hidden",
133
- },
134
- ]
135
- }
136
- testID="GC_WRAPPER"
137
- />
161
+ }
162
+ }
163
+ />
164
+ </View>
138
165
  </View>
139
166
  </View>
140
167
  </KeyboardProvider>
@@ -4,14 +4,12 @@ exports[`should render <InputToolbar /> and compare with snapshot 1`] = `
4
4
  <View
5
5
  style={
6
6
  [
7
- [
8
- {
9
- "backgroundColor": "#fff",
10
- "borderTopColor": "#b2b2b2",
11
- "borderTopWidth": 0.5,
12
- },
13
- undefined,
14
- ],
7
+ {
8
+ "backgroundColor": "#fff",
9
+ "borderTopColor": "#b2b2b2",
10
+ "borderTopWidth": 0.5,
11
+ },
12
+ false,
15
13
  undefined,
16
14
  ]
17
15
  }
@@ -19,13 +17,10 @@ exports[`should render <InputToolbar /> and compare with snapshot 1`] = `
19
17
  <View
20
18
  style={
21
19
  [
22
- [
23
- {
24
- "alignItems": "flex-end",
25
- "flexDirection": "row",
26
- },
27
- undefined,
28
- ],
20
+ {
21
+ "alignItems": "flex-end",
22
+ "flexDirection": "row",
23
+ },
29
24
  undefined,
30
25
  ]
31
26
  }
@@ -99,6 +94,7 @@ exports[`should render <InputToolbar /> and compare with snapshot 1`] = `
99
94
  undefined,
100
95
  ]
101
96
  }
97
+ nativeID="0"
102
98
  pointerEvents="none"
103
99
  style={
104
100
  [
@@ -65,6 +65,7 @@ exports[`MessageImage should render <MessageImage /> and compare with snapshot
65
65
  },
66
66
  ]
67
67
  }
68
+ nativeID="0"
68
69
  style={
69
70
  [
70
71
  {
@@ -125,6 +126,7 @@ exports[`MessageImage should render <MessageImage /> and compare with snapshot
125
126
  },
126
127
  ]
127
128
  }
129
+ nativeID="1"
128
130
  style={
129
131
  [
130
132
  {
@@ -196,24 +198,27 @@ exports[`MessageImage should render <MessageImage /> and compare with snapshot
196
198
  >
197
199
  <View
198
200
  style={
199
- {
200
- "flex": 1,
201
- }
201
+ [
202
+ {
203
+ "alignItems": "center",
204
+ "flex": 1,
205
+ "justifyContent": "center",
206
+ "overflow": "hidden",
207
+ },
208
+ undefined,
209
+ ]
202
210
  }
203
211
  >
204
212
  <View
205
213
  collapsable={false}
206
214
  onLayout={[Function]}
207
215
  style={
208
- [
209
- {
210
- "alignItems": "center",
211
- "flex": 1,
212
- "justifyContent": "center",
213
- "overflow": "hidden",
214
- },
215
- undefined,
216
- ]
216
+ {
217
+ "alignItems": "center",
218
+ "flex": 1,
219
+ "justifyContent": "center",
220
+ "overflow": "hidden",
221
+ }
217
222
  }
218
223
  >
219
224
  <View
@@ -227,15 +232,15 @@ exports[`MessageImage should render <MessageImage /> and compare with snapshot
227
232
  {
228
233
  "value": {
229
234
  "transform": [
230
- {
231
- "scale": 1,
232
- },
233
235
  {
234
236
  "translateX": 0,
235
237
  },
236
238
  {
237
239
  "translateY": 0,
238
240
  },
241
+ {
242
+ "scale": 1,
243
+ },
239
244
  ],
240
245
  },
241
246
  }
@@ -245,20 +250,21 @@ exports[`MessageImage should render <MessageImage /> and compare with snapshot
245
250
  undefined,
246
251
  ]
247
252
  }
253
+ nativeID="2"
248
254
  onLayout={[Function]}
249
255
  style={
250
256
  [
251
257
  {
252
258
  "transform": [
253
- {
254
- "scale": 1,
255
- },
256
259
  {
257
260
  "translateX": 0,
258
261
  },
259
262
  {
260
263
  "translateY": 0,
261
264
  },
265
+ {
266
+ "scale": 1,
267
+ },
262
268
  ],
263
269
  },
264
270
  undefined,