react-native-gifted-chat 2.6.5 → 2.7.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.
- package/README.md +7 -16
- package/lib/Actions.d.ts +0 -11
- package/lib/Actions.js +2 -14
- package/lib/Actions.js.map +1 -1
- package/lib/Avatar.d.ts +1 -23
- package/lib/Avatar.js +32 -40
- package/lib/Avatar.js.map +1 -1
- package/lib/Bubble/index.d.ts +30 -0
- package/lib/{Bubble.js → Bubble/index.js} +31 -181
- package/lib/Bubble/index.js.map +1 -0
- package/lib/Bubble/styles.d.ts +69 -0
- package/lib/Bubble/styles.js +72 -0
- package/lib/Bubble/styles.js.map +1 -0
- package/lib/Bubble/types.d.ts +47 -0
- package/lib/Bubble/types.js +2 -0
- package/lib/Bubble/types.js.map +1 -0
- package/lib/Composer.d.ts +0 -17
- package/lib/Composer.js +3 -18
- package/lib/Composer.js.map +1 -1
- package/lib/Constant.d.ts +1 -1
- package/lib/Constant.js +1 -1
- package/lib/Constant.js.map +1 -1
- package/lib/Day/index.d.ts +4 -0
- package/lib/Day/index.js +39 -0
- package/lib/Day/index.js.map +1 -0
- package/lib/Day/styles.d.ts +20 -0
- package/lib/Day/styles.js +22 -0
- package/lib/Day/styles.js.map +1 -0
- package/lib/Day/types.d.ts +9 -0
- package/lib/Day/types.js +2 -0
- package/lib/Day/types.js.map +1 -0
- package/lib/GiftedAvatar.d.ts +1 -11
- package/lib/GiftedAvatar.js +26 -33
- package/lib/GiftedAvatar.js.map +1 -1
- package/lib/GiftedChat/index.d.ts +26 -0
- package/lib/{GiftedChat.js → GiftedChat/index.js} +47 -52
- package/lib/GiftedChat/index.js.map +1 -0
- package/lib/GiftedChat/styles.d.ts +6 -0
- package/lib/GiftedChat/styles.js +7 -0
- package/lib/GiftedChat/styles.js.map +1 -0
- package/lib/{GiftedChat.d.ts → GiftedChat/types.d.ts} +27 -37
- package/lib/GiftedChat/types.js +2 -0
- package/lib/GiftedChat/types.js.map +1 -0
- package/lib/InputToolbar.d.ts +1 -14
- package/lib/InputToolbar.js +0 -12
- package/lib/InputToolbar.js.map +1 -1
- package/lib/LoadEarlier.d.ts +0 -14
- package/lib/LoadEarlier.js +2 -16
- package/lib/LoadEarlier.js.map +1 -1
- package/lib/Message/index.d.ts +6 -0
- package/lib/Message/index.js +85 -0
- package/lib/Message/index.js.map +1 -0
- package/lib/Message/styles.d.ts +21 -0
- package/lib/Message/styles.js +22 -0
- package/lib/Message/styles.js.map +1 -0
- package/lib/Message/types.d.ts +22 -0
- package/lib/Message/types.js +2 -0
- package/lib/Message/types.js.map +1 -0
- package/lib/MessageAudio.js +1 -2
- package/lib/MessageAudio.js.map +1 -1
- package/lib/MessageContainer/components/DayAnimated/index.d.ts +5 -0
- package/lib/MessageContainer/components/DayAnimated/index.js +85 -0
- package/lib/MessageContainer/components/DayAnimated/index.js.map +1 -0
- package/lib/MessageContainer/components/DayAnimated/styles.d.ts +11 -0
- package/lib/MessageContainer/components/DayAnimated/styles.js +12 -0
- package/lib/MessageContainer/components/DayAnimated/styles.js.map +1 -0
- package/lib/MessageContainer/components/DayAnimated/types.d.ts +17 -0
- package/lib/MessageContainer/components/DayAnimated/types.js +2 -0
- package/lib/MessageContainer/components/DayAnimated/types.js.map +1 -0
- package/lib/MessageContainer/components/Item/index.d.ts +22 -0
- package/lib/MessageContainer/components/Item/index.js +69 -0
- package/lib/MessageContainer/components/Item/index.js.map +1 -0
- package/lib/MessageContainer/components/Item/types.d.ts +18 -0
- package/lib/MessageContainer/components/Item/types.js +2 -0
- package/lib/MessageContainer/components/Item/types.js.map +1 -0
- package/lib/MessageContainer/index.d.ts +6 -0
- package/lib/MessageContainer/index.js +175 -0
- package/lib/MessageContainer/index.js.map +1 -0
- package/lib/MessageContainer/styles.d.ts +34 -0
- package/lib/MessageContainer/styles.js +31 -0
- package/lib/MessageContainer/styles.js.map +1 -0
- package/lib/MessageContainer/types.d.ts +54 -0
- package/lib/MessageContainer/types.js +2 -0
- package/lib/MessageContainer/types.js.map +1 -0
- package/lib/MessageImage.d.ts +1 -12
- package/lib/MessageImage.js +2 -12
- package/lib/MessageImage.js.map +1 -1
- package/lib/MessageText.d.ts +1 -24
- package/lib/MessageText.js +0 -22
- package/lib/MessageText.js.map +1 -1
- package/lib/MessageVideo.js +1 -2
- package/lib/MessageVideo.js.map +1 -1
- package/lib/QuickReplies.d.ts +1 -12
- package/lib/QuickReplies.js +10 -20
- package/lib/QuickReplies.js.map +1 -1
- package/lib/Send.d.ts +2 -16
- package/lib/Send.js +0 -13
- package/lib/Send.js.map +1 -1
- package/lib/SystemMessage.d.ts +1 -10
- package/lib/SystemMessage.js +2 -12
- package/lib/SystemMessage.js.map +1 -1
- package/lib/Time.d.ts +1 -17
- package/lib/Time.js +0 -15
- package/lib/Time.js.map +1 -1
- package/lib/TypingIndicator/index.d.ts +5 -0
- package/lib/{TypingIndicator.js → TypingIndicator/index.js} +9 -30
- package/lib/TypingIndicator/index.js.map +1 -0
- package/lib/TypingIndicator/styles.d.ts +20 -0
- package/lib/TypingIndicator/styles.js +22 -0
- package/lib/TypingIndicator/styles.js.map +1 -0
- package/lib/TypingIndicator/types.d.ts +3 -0
- package/lib/TypingIndicator/types.js +2 -0
- package/lib/TypingIndicator/types.js.map +1 -0
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/styles.d.ts +10 -0
- package/lib/styles.js +11 -0
- package/lib/styles.js.map +1 -0
- package/lib/{Models.d.ts → types.d.ts} +1 -1
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/lib/utils.d.ts +1 -3
- package/lib/utils.js +0 -7
- package/lib/utils.js.map +1 -1
- package/package.json +21 -29
- package/src/Actions.tsx +3 -15
- package/src/Avatar.tsx +43 -52
- package/src/{Bubble.tsx → Bubble/index.tsx} +52 -269
- package/src/Bubble/styles.ts +73 -0
- package/src/Bubble/types.ts +90 -0
- package/src/Composer.tsx +3 -19
- package/src/Constant.ts +1 -1
- package/src/Day/index.tsx +63 -0
- package/src/Day/styles.ts +22 -0
- package/src/Day/types.ts +14 -0
- package/src/GiftedAvatar.tsx +31 -38
- package/src/GiftedChat/index.tsx +503 -0
- package/src/GiftedChat/styles.ts +7 -0
- package/src/GiftedChat/types.ts +206 -0
- package/src/InputToolbar.tsx +1 -14
- package/src/LoadEarlier.tsx +2 -17
- package/src/Message/index.tsx +135 -0
- package/src/Message/styles.ts +22 -0
- package/src/Message/types.ts +26 -0
- package/src/MessageAudio.tsx +1 -4
- package/src/MessageContainer/components/DayAnimated/index.tsx +143 -0
- package/src/MessageContainer/components/DayAnimated/styles.ts +12 -0
- package/src/MessageContainer/components/DayAnimated/types.ts +12 -0
- package/src/MessageContainer/components/Item/index.tsx +136 -0
- package/src/MessageContainer/components/Item/types.ts +13 -0
- package/src/MessageContainer/index.tsx +316 -0
- package/src/MessageContainer/styles.ts +31 -0
- package/src/MessageContainer/types.ts +60 -0
- package/src/MessageImage.tsx +3 -14
- package/src/MessageText.tsx +1 -24
- package/src/MessageVideo.tsx +1 -4
- package/src/QuickReplies.tsx +14 -25
- package/src/Send.tsx +1 -15
- package/src/SystemMessage.tsx +3 -14
- package/src/Time.tsx +1 -17
- package/src/{TypingIndicator.tsx → TypingIndicator/index.tsx} +12 -35
- package/src/TypingIndicator/styles.ts +22 -0
- package/src/TypingIndicator/types.ts +3 -0
- package/src/__tests__/Bubble.test.tsx +7 -4
- package/src/__tests__/GiftedAvatar.test.tsx +6 -2
- package/src/__tests__/GiftedChat.test.tsx +20 -14
- package/src/__tests__/Message.test.tsx +4 -1
- package/src/__tests__/__snapshots__/Actions.test.tsx.snap +6 -2
- package/src/__tests__/__snapshots__/Bubble.test.tsx.snap +3 -1
- package/src/__tests__/__snapshots__/Composer.test.tsx.snap +2 -0
- package/src/__tests__/__snapshots__/Constant.test.tsx.snap +1 -1
- package/src/__tests__/__snapshots__/Day.test.tsx.snap +1 -33
- package/src/__tests__/__snapshots__/GiftedAvatar.test.tsx.snap +4 -2
- package/src/__tests__/__snapshots__/GiftedChat.test.tsx.snap +22 -9
- package/src/__tests__/__snapshots__/InputToolbar.test.tsx.snap +2 -0
- package/src/__tests__/__snapshots__/LoadEarlier.test.tsx.snap +3 -1
- package/src/__tests__/__snapshots__/Message.test.tsx.snap +21 -100
- package/src/__tests__/__snapshots__/MessageContainer.test.tsx.snap +282 -82
- package/src/__tests__/__snapshots__/MessageImage.test.tsx.snap +8 -4
- package/src/__tests__/__snapshots__/SystemMessage.test.tsx.snap +5 -1
- package/src/__tests__/data.ts +1 -1
- package/src/index.ts +0 -2
- package/src/styles.ts +11 -0
- package/src/{Models.ts → types.ts} +1 -1
- package/src/utils.ts +1 -9
- package/lib/Actions.js.flow +0 -21
- package/lib/Avatar.js.flow +0 -27
- package/lib/Bubble.d.ts +0 -148
- package/lib/Bubble.js.flow +0 -69
- package/lib/Bubble.js.map +0 -1
- package/lib/Composer.js.flow +0 -24
- package/lib/Day.d.ts +0 -15
- package/lib/Day.js +0 -36
- package/lib/Day.js.flow +0 -23
- package/lib/Day.js.map +0 -1
- package/lib/GiftedAvatar.js.flow +0 -17
- package/lib/GiftedChat.js.flow +0 -163
- package/lib/GiftedChat.js.map +0 -1
- package/lib/InputToolbar.js.flow +0 -31
- package/lib/LoadEarlier.js.flow +0 -20
- package/lib/Message.d.ts +0 -67
- package/lib/Message.js +0 -157
- package/lib/Message.js.flow +0 -32
- package/lib/Message.js.map +0 -1
- package/lib/MessageAudio.js.flow +0 -15
- package/lib/MessageContainer.d.ts +0 -105
- package/lib/MessageContainer.js +0 -224
- package/lib/MessageContainer.js.flow +0 -39
- package/lib/MessageContainer.js.map +0 -1
- package/lib/MessageImage.js.flow +0 -20
- package/lib/MessageText.js.flow +0 -23
- package/lib/MessageVideo.js.flow +0 -16
- package/lib/Models.js +0 -2
- package/lib/Models.js.map +0 -1
- package/lib/QuickReplies.js.flow +0 -25
- package/lib/Send.js.flow +0 -19
- package/lib/SystemMessage.js.flow +0 -18
- package/lib/Time.js.flow +0 -19
- package/lib/TypingIndicator.d.ts +0 -6
- package/lib/TypingIndicator.js.map +0 -1
- package/lib/index.js.flow +0 -2
- package/lib/types.js.flow +0 -43
- package/lib/utils.js.flow +0 -16
- package/src/Day.tsx +0 -71
- package/src/GiftedChat.tsx +0 -689
- package/src/Message.tsx +0 -229
- package/src/MessageContainer.tsx +0 -361
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { StyleSheet } from 'react-native'
|
|
2
|
+
import Color from '../Color'
|
|
3
|
+
|
|
4
|
+
export default StyleSheet.create({
|
|
5
|
+
container: {
|
|
6
|
+
marginTop: 5,
|
|
7
|
+
marginBottom: 10,
|
|
8
|
+
},
|
|
9
|
+
wrapper: {
|
|
10
|
+
backgroundColor: 'rgba(0, 0, 0, 0.75)',
|
|
11
|
+
paddingTop: 6,
|
|
12
|
+
paddingBottom: 6,
|
|
13
|
+
paddingLeft: 10,
|
|
14
|
+
paddingRight: 10,
|
|
15
|
+
borderRadius: 15,
|
|
16
|
+
},
|
|
17
|
+
text: {
|
|
18
|
+
color: Color.white,
|
|
19
|
+
fontSize: 12,
|
|
20
|
+
fontWeight: '600',
|
|
21
|
+
},
|
|
22
|
+
})
|
package/src/Day/types.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StyleProp,
|
|
3
|
+
ViewStyle,
|
|
4
|
+
TextStyle,
|
|
5
|
+
} from 'react-native'
|
|
6
|
+
|
|
7
|
+
export interface DayProps {
|
|
8
|
+
createdAt: Date | number
|
|
9
|
+
dateFormat?: string
|
|
10
|
+
dateFormatCalendar?: object
|
|
11
|
+
containerStyle?: StyleProp<ViewStyle>
|
|
12
|
+
wrapperStyle?: StyleProp<ViewStyle>
|
|
13
|
+
textStyle?: StyleProp<TextStyle>
|
|
14
|
+
}
|
package/src/GiftedAvatar.tsx
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import React, { useCallback, useRef } from 'react'
|
|
1
|
+
import React, { useCallback, useEffect, useState } from 'react'
|
|
3
2
|
import {
|
|
4
3
|
Image,
|
|
5
4
|
Text,
|
|
@@ -11,8 +10,8 @@ import {
|
|
|
11
10
|
TextStyle,
|
|
12
11
|
} from 'react-native'
|
|
13
12
|
import Color from './Color'
|
|
14
|
-
import { User } from './
|
|
15
|
-
import
|
|
13
|
+
import { User } from './types'
|
|
14
|
+
import stylesCommon from './styles'
|
|
16
15
|
|
|
17
16
|
const {
|
|
18
17
|
carrot,
|
|
@@ -26,8 +25,6 @@ const {
|
|
|
26
25
|
|
|
27
26
|
const styles = StyleSheet.create({
|
|
28
27
|
avatarStyle: {
|
|
29
|
-
justifyContent: 'center',
|
|
30
|
-
alignItems: 'center',
|
|
31
28
|
width: 40,
|
|
32
29
|
height: 40,
|
|
33
30
|
borderRadius: 20,
|
|
@@ -54,29 +51,29 @@ export interface GiftedAvatarProps {
|
|
|
54
51
|
export function GiftedAvatar (
|
|
55
52
|
props: GiftedAvatarProps
|
|
56
53
|
) {
|
|
57
|
-
const
|
|
58
|
-
const
|
|
54
|
+
const [avatarName, setAvatarName] = useState<string | undefined>(undefined)
|
|
55
|
+
const [backgroundColor, setBackgroundColor] = useState<string | undefined>(undefined)
|
|
59
56
|
|
|
60
57
|
const {
|
|
61
|
-
user
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
},
|
|
65
|
-
avatarStyle = {},
|
|
66
|
-
textStyle = {},
|
|
58
|
+
user,
|
|
59
|
+
avatarStyle,
|
|
60
|
+
textStyle,
|
|
67
61
|
onPress,
|
|
68
62
|
} = props
|
|
69
63
|
|
|
70
64
|
const setAvatarColor = useCallback(() => {
|
|
71
|
-
|
|
65
|
+
if (backgroundColor)
|
|
66
|
+
return
|
|
67
|
+
|
|
68
|
+
const userName = user?.name || ''
|
|
72
69
|
const name = userName.toUpperCase().split(' ')
|
|
73
70
|
|
|
74
71
|
if (name.length === 1)
|
|
75
|
-
|
|
72
|
+
setAvatarName(`${name[0].charAt(0)}`)
|
|
76
73
|
else if (name.length > 1)
|
|
77
|
-
|
|
74
|
+
setAvatarName(`${name[0].charAt(0)}${name[1].charAt(0)}`)
|
|
78
75
|
else
|
|
79
|
-
|
|
76
|
+
setAvatarName('')
|
|
80
77
|
|
|
81
78
|
let sumChars = 0
|
|
82
79
|
for (let i = 0; i < userName.length; i += 1)
|
|
@@ -94,39 +91,39 @@ export function GiftedAvatar (
|
|
|
94
91
|
midnightBlue,
|
|
95
92
|
]
|
|
96
93
|
|
|
97
|
-
|
|
98
|
-
}, [user
|
|
94
|
+
setBackgroundColor(colors[sumChars % colors.length])
|
|
95
|
+
}, [user?.name, backgroundColor])
|
|
99
96
|
|
|
100
97
|
const renderAvatar = useCallback(() => {
|
|
101
|
-
switch (typeof user
|
|
98
|
+
switch (typeof user?.avatar) {
|
|
102
99
|
case 'function':
|
|
103
|
-
return user.avatar([styles.avatarStyle, avatarStyle])
|
|
100
|
+
return user.avatar([stylesCommon.centerItems, styles.avatarStyle, avatarStyle])
|
|
104
101
|
case 'string':
|
|
105
102
|
return (
|
|
106
103
|
<Image
|
|
107
104
|
source={{ uri: user.avatar }}
|
|
108
|
-
style={[styles.avatarStyle, avatarStyle]}
|
|
105
|
+
style={[stylesCommon.centerItems, styles.avatarStyle, avatarStyle]}
|
|
109
106
|
/>
|
|
110
107
|
)
|
|
111
108
|
case 'number':
|
|
112
109
|
return (
|
|
113
110
|
<Image
|
|
114
111
|
source={user.avatar}
|
|
115
|
-
style={[styles.avatarStyle, avatarStyle]}
|
|
112
|
+
style={[stylesCommon.centerItems, styles.avatarStyle, avatarStyle]}
|
|
116
113
|
/>
|
|
117
114
|
)
|
|
118
115
|
default:
|
|
119
116
|
return null
|
|
120
117
|
}
|
|
121
|
-
}, [user
|
|
118
|
+
}, [user, avatarStyle])
|
|
122
119
|
|
|
123
120
|
const renderInitials = useCallback(() => {
|
|
124
121
|
return (
|
|
125
122
|
<Text style={[styles.textStyle, textStyle]}>
|
|
126
|
-
{
|
|
123
|
+
{avatarName}
|
|
127
124
|
</Text>
|
|
128
125
|
)
|
|
129
|
-
}, [textStyle])
|
|
126
|
+
}, [textStyle, avatarName])
|
|
130
127
|
|
|
131
128
|
const handleOnPress = () => {
|
|
132
129
|
const {
|
|
@@ -148,11 +145,16 @@ export function GiftedAvatar (
|
|
|
148
145
|
onLongPress(rest)
|
|
149
146
|
}
|
|
150
147
|
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
setAvatarColor()
|
|
150
|
+
}, [setAvatarColor])
|
|
151
|
+
|
|
151
152
|
if (!user || (!user.name && !user.avatar))
|
|
152
153
|
// render placeholder
|
|
153
154
|
return (
|
|
154
155
|
<View
|
|
155
156
|
style={[
|
|
157
|
+
stylesCommon.centerItems,
|
|
156
158
|
styles.avatarStyle,
|
|
157
159
|
styles.avatarTransparent,
|
|
158
160
|
avatarStyle,
|
|
@@ -173,16 +175,15 @@ export function GiftedAvatar (
|
|
|
173
175
|
</TouchableOpacity>
|
|
174
176
|
)
|
|
175
177
|
|
|
176
|
-
setAvatarColor()
|
|
177
|
-
|
|
178
178
|
return (
|
|
179
179
|
<TouchableOpacity
|
|
180
180
|
disabled={!onPress}
|
|
181
181
|
onPress={handleOnPress}
|
|
182
182
|
onLongPress={handleOnLongPress}
|
|
183
183
|
style={[
|
|
184
|
+
stylesCommon.centerItems,
|
|
184
185
|
styles.avatarStyle,
|
|
185
|
-
{ backgroundColor
|
|
186
|
+
{ backgroundColor },
|
|
186
187
|
avatarStyle,
|
|
187
188
|
]}
|
|
188
189
|
accessibilityRole='image'
|
|
@@ -191,11 +192,3 @@ export function GiftedAvatar (
|
|
|
191
192
|
</TouchableOpacity>
|
|
192
193
|
)
|
|
193
194
|
}
|
|
194
|
-
|
|
195
|
-
GiftedAvatar.propTypes = {
|
|
196
|
-
user: PropTypes.object,
|
|
197
|
-
onPress: PropTypes.func,
|
|
198
|
-
onLongPress: PropTypes.func,
|
|
199
|
-
avatarStyle: StylePropType,
|
|
200
|
-
textStyle: StylePropType,
|
|
201
|
-
}
|
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
createRef,
|
|
3
|
+
useEffect,
|
|
4
|
+
useMemo,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
useCallback,
|
|
8
|
+
RefObject,
|
|
9
|
+
} from 'react'
|
|
10
|
+
import {
|
|
11
|
+
ActionSheetProvider,
|
|
12
|
+
ActionSheetProviderRef,
|
|
13
|
+
} from '@expo/react-native-action-sheet'
|
|
14
|
+
import dayjs from 'dayjs'
|
|
15
|
+
import localizedFormat from 'dayjs/plugin/localizedFormat'
|
|
16
|
+
import {
|
|
17
|
+
Platform,
|
|
18
|
+
TextInput,
|
|
19
|
+
View,
|
|
20
|
+
LayoutChangeEvent,
|
|
21
|
+
} from 'react-native'
|
|
22
|
+
import { Actions } from '../Actions'
|
|
23
|
+
import { Avatar } from '../Avatar'
|
|
24
|
+
import Bubble from '../Bubble'
|
|
25
|
+
import { Composer } from '../Composer'
|
|
26
|
+
import { MAX_COMPOSER_HEIGHT, MIN_COMPOSER_HEIGHT, TEST_ID } from '../Constant'
|
|
27
|
+
import { Day } from '../Day'
|
|
28
|
+
import { GiftedAvatar } from '../GiftedAvatar'
|
|
29
|
+
import { GiftedChatContext } from '../GiftedChatContext'
|
|
30
|
+
import { InputToolbar } from '../InputToolbar'
|
|
31
|
+
import { LoadEarlier } from '../LoadEarlier'
|
|
32
|
+
import Message from '../Message'
|
|
33
|
+
import MessageContainer, { AnimatedList } from '../MessageContainer'
|
|
34
|
+
import { MessageImage } from '../MessageImage'
|
|
35
|
+
import { MessageText } from '../MessageText'
|
|
36
|
+
import {
|
|
37
|
+
IMessage,
|
|
38
|
+
} from '../types'
|
|
39
|
+
import { Send } from '../Send'
|
|
40
|
+
import { SystemMessage } from '../SystemMessage'
|
|
41
|
+
import { Time } from '../Time'
|
|
42
|
+
import * as utils from '../utils'
|
|
43
|
+
import Animated, {
|
|
44
|
+
useAnimatedStyle,
|
|
45
|
+
useAnimatedReaction,
|
|
46
|
+
useSharedValue,
|
|
47
|
+
withTiming,
|
|
48
|
+
runOnJS,
|
|
49
|
+
} from 'react-native-reanimated'
|
|
50
|
+
import { KeyboardProvider, useReanimatedKeyboardAnimation } from 'react-native-keyboard-controller'
|
|
51
|
+
import { GiftedChatProps } from './types'
|
|
52
|
+
|
|
53
|
+
import stylesCommon from '../styles'
|
|
54
|
+
import styles from './styles'
|
|
55
|
+
|
|
56
|
+
dayjs.extend(localizedFormat)
|
|
57
|
+
|
|
58
|
+
function GiftedChat<TMessage extends IMessage = IMessage> (
|
|
59
|
+
props: GiftedChatProps
|
|
60
|
+
) {
|
|
61
|
+
const {
|
|
62
|
+
messages = [],
|
|
63
|
+
initialText = '',
|
|
64
|
+
isTyping,
|
|
65
|
+
messageIdGenerator = () => crypto.randomUUID(),
|
|
66
|
+
user = {},
|
|
67
|
+
onSend,
|
|
68
|
+
locale = 'en',
|
|
69
|
+
renderLoading,
|
|
70
|
+
actionSheet = null,
|
|
71
|
+
textInputProps,
|
|
72
|
+
renderChatFooter = null,
|
|
73
|
+
renderInputToolbar = null,
|
|
74
|
+
bottomOffset = 0,
|
|
75
|
+
focusOnInputWhenOpeningKeyboard = true,
|
|
76
|
+
keyboardShouldPersistTaps = Platform.select({
|
|
77
|
+
ios: 'never',
|
|
78
|
+
android: 'always',
|
|
79
|
+
default: 'never',
|
|
80
|
+
}),
|
|
81
|
+
onInputTextChanged = null,
|
|
82
|
+
maxInputLength = null,
|
|
83
|
+
inverted = true,
|
|
84
|
+
minComposerHeight = MIN_COMPOSER_HEIGHT,
|
|
85
|
+
maxComposerHeight = MAX_COMPOSER_HEIGHT,
|
|
86
|
+
isKeyboardInternallyHandled = true,
|
|
87
|
+
} = props
|
|
88
|
+
|
|
89
|
+
const actionSheetRef = useRef<ActionSheetProviderRef>(null)
|
|
90
|
+
|
|
91
|
+
const messageContainerRef = useMemo(
|
|
92
|
+
() => props.messageContainerRef || createRef<AnimatedList>(),
|
|
93
|
+
[props.messageContainerRef]
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
const textInputRef = useMemo(
|
|
97
|
+
() => props.textInputRef || createRef<TextInput>(),
|
|
98
|
+
[props.textInputRef]
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
const isTextInputWasFocused: RefObject<boolean> = useRef(false)
|
|
102
|
+
|
|
103
|
+
const [isInitialized, setIsInitialized] = useState<boolean>(false)
|
|
104
|
+
const [composerHeight, setComposerHeight] = useState<number>(
|
|
105
|
+
minComposerHeight!
|
|
106
|
+
)
|
|
107
|
+
const [text, setText] = useState<string | undefined>(() => props.text || '')
|
|
108
|
+
const [isTypingDisabled, setIsTypingDisabled] = useState<boolean>(false)
|
|
109
|
+
|
|
110
|
+
const keyboard = useReanimatedKeyboardAnimation()
|
|
111
|
+
const trackingKeyboardMovement = useSharedValue(false)
|
|
112
|
+
const debounceEnableTypingTimeoutId = useRef<ReturnType<typeof setTimeout>>(undefined)
|
|
113
|
+
const keyboardOffsetBottom = useSharedValue(0)
|
|
114
|
+
|
|
115
|
+
const contentStyleAnim = useAnimatedStyle(
|
|
116
|
+
() => ({
|
|
117
|
+
transform: [
|
|
118
|
+
{ translateY: keyboard.height.value - keyboardOffsetBottom.value },
|
|
119
|
+
],
|
|
120
|
+
}),
|
|
121
|
+
[keyboard, keyboardOffsetBottom]
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const getTextFromProp = useCallback(
|
|
125
|
+
(fallback: string) => {
|
|
126
|
+
if (props.text === undefined)
|
|
127
|
+
return fallback
|
|
128
|
+
|
|
129
|
+
return props.text
|
|
130
|
+
},
|
|
131
|
+
[props.text]
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Store text input focus status when keyboard hide to retrieve
|
|
136
|
+
* it afterwards if needed.
|
|
137
|
+
* `onKeyboardWillHide` may be called twice in sequence so we
|
|
138
|
+
* make a guard condition (eg. showing image picker)
|
|
139
|
+
*/
|
|
140
|
+
const handleTextInputFocusWhenKeyboardHide = useCallback(() => {
|
|
141
|
+
if (!isTextInputWasFocused.current)
|
|
142
|
+
isTextInputWasFocused.current =
|
|
143
|
+
textInputRef.current?.isFocused() || false
|
|
144
|
+
}, [textInputRef])
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Refocus the text input only if it was focused before showing keyboard.
|
|
148
|
+
* This is needed in some cases (eg. showing image picker).
|
|
149
|
+
*/
|
|
150
|
+
const handleTextInputFocusWhenKeyboardShow = useCallback(() => {
|
|
151
|
+
if (
|
|
152
|
+
textInputRef.current &&
|
|
153
|
+
isTextInputWasFocused.current &&
|
|
154
|
+
!textInputRef.current.isFocused()
|
|
155
|
+
)
|
|
156
|
+
textInputRef.current.focus()
|
|
157
|
+
|
|
158
|
+
// Reset the indicator since the keyboard is shown
|
|
159
|
+
isTextInputWasFocused.current = false
|
|
160
|
+
}, [textInputRef])
|
|
161
|
+
|
|
162
|
+
const disableTyping = useCallback(() => {
|
|
163
|
+
clearTimeout(debounceEnableTypingTimeoutId.current)
|
|
164
|
+
setIsTypingDisabled(true)
|
|
165
|
+
}, [])
|
|
166
|
+
|
|
167
|
+
const enableTyping = useCallback(() => {
|
|
168
|
+
clearTimeout(debounceEnableTypingTimeoutId.current)
|
|
169
|
+
setIsTypingDisabled(false)
|
|
170
|
+
}, [])
|
|
171
|
+
|
|
172
|
+
const debounceEnableTyping = useCallback(() => {
|
|
173
|
+
clearTimeout(debounceEnableTypingTimeoutId.current)
|
|
174
|
+
debounceEnableTypingTimeoutId.current = setTimeout(() => {
|
|
175
|
+
enableTyping()
|
|
176
|
+
}, 50)
|
|
177
|
+
}, [enableTyping])
|
|
178
|
+
|
|
179
|
+
const scrollToBottom = useCallback(
|
|
180
|
+
(isAnimated = true) => {
|
|
181
|
+
if (!messageContainerRef?.current)
|
|
182
|
+
return
|
|
183
|
+
|
|
184
|
+
if (inverted) {
|
|
185
|
+
messageContainerRef.current.scrollToOffset({
|
|
186
|
+
offset: 0,
|
|
187
|
+
animated: isAnimated,
|
|
188
|
+
})
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
messageContainerRef.current.scrollToEnd({ animated: isAnimated })
|
|
193
|
+
},
|
|
194
|
+
[inverted, messageContainerRef]
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
const renderMessages = useMemo(() => {
|
|
198
|
+
if (!isInitialized)
|
|
199
|
+
return null
|
|
200
|
+
|
|
201
|
+
const { messagesContainerStyle, ...messagesContainerProps } = props
|
|
202
|
+
|
|
203
|
+
const fragment = (
|
|
204
|
+
<View style={[stylesCommon.fill, messagesContainerStyle]}>
|
|
205
|
+
<MessageContainer
|
|
206
|
+
{...messagesContainerProps}
|
|
207
|
+
invertibleScrollViewProps={{
|
|
208
|
+
inverted,
|
|
209
|
+
keyboardShouldPersistTaps,
|
|
210
|
+
}}
|
|
211
|
+
messages={messages}
|
|
212
|
+
forwardRef={messageContainerRef}
|
|
213
|
+
isTyping={isTyping}
|
|
214
|
+
/>
|
|
215
|
+
{renderChatFooter?.()}
|
|
216
|
+
</View>
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
return fragment
|
|
220
|
+
}, [
|
|
221
|
+
isInitialized,
|
|
222
|
+
isTyping,
|
|
223
|
+
messages,
|
|
224
|
+
props,
|
|
225
|
+
inverted,
|
|
226
|
+
keyboardShouldPersistTaps,
|
|
227
|
+
messageContainerRef,
|
|
228
|
+
renderChatFooter,
|
|
229
|
+
])
|
|
230
|
+
|
|
231
|
+
const notifyInputTextReset = useCallback(() => {
|
|
232
|
+
onInputTextChanged?.('')
|
|
233
|
+
}, [onInputTextChanged])
|
|
234
|
+
|
|
235
|
+
const resetInputToolbar = useCallback(() => {
|
|
236
|
+
textInputRef.current?.clear()
|
|
237
|
+
|
|
238
|
+
notifyInputTextReset()
|
|
239
|
+
|
|
240
|
+
setComposerHeight(minComposerHeight!)
|
|
241
|
+
setText(getTextFromProp(''))
|
|
242
|
+
enableTyping()
|
|
243
|
+
}, [
|
|
244
|
+
minComposerHeight,
|
|
245
|
+
getTextFromProp,
|
|
246
|
+
textInputRef,
|
|
247
|
+
notifyInputTextReset,
|
|
248
|
+
enableTyping,
|
|
249
|
+
])
|
|
250
|
+
|
|
251
|
+
const _onSend = useCallback(
|
|
252
|
+
(messages: TMessage[] = [], shouldResetInputToolbar = false) => {
|
|
253
|
+
if (!Array.isArray(messages))
|
|
254
|
+
messages = [messages]
|
|
255
|
+
|
|
256
|
+
const newMessages: TMessage[] = messages.map(message => {
|
|
257
|
+
return {
|
|
258
|
+
...message,
|
|
259
|
+
user: user!,
|
|
260
|
+
createdAt: new Date(),
|
|
261
|
+
_id: messageIdGenerator?.(),
|
|
262
|
+
}
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
if (shouldResetInputToolbar === true) {
|
|
266
|
+
disableTyping()
|
|
267
|
+
|
|
268
|
+
resetInputToolbar()
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
onSend?.(newMessages)
|
|
272
|
+
|
|
273
|
+
setTimeout(() => scrollToBottom(), 10)
|
|
274
|
+
},
|
|
275
|
+
[messageIdGenerator, onSend, user, resetInputToolbar, disableTyping, scrollToBottom]
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
const onInputSizeChanged = useCallback(
|
|
279
|
+
(size: { height: number }) => {
|
|
280
|
+
const newComposerHeight = Math.max(
|
|
281
|
+
minComposerHeight!,
|
|
282
|
+
Math.min(maxComposerHeight!, size.height)
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
setComposerHeight(newComposerHeight)
|
|
286
|
+
},
|
|
287
|
+
[maxComposerHeight, minComposerHeight]
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
const _onInputTextChanged = useCallback(
|
|
291
|
+
(_text: string) => {
|
|
292
|
+
if (isTypingDisabled)
|
|
293
|
+
return
|
|
294
|
+
|
|
295
|
+
onInputTextChanged?.(_text)
|
|
296
|
+
|
|
297
|
+
// Only set state if it's not being overridden by a prop.
|
|
298
|
+
if (props.text === undefined)
|
|
299
|
+
setText(_text)
|
|
300
|
+
},
|
|
301
|
+
[onInputTextChanged, isTypingDisabled, props.text]
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
const onInitialLayoutViewLayout = useCallback(
|
|
305
|
+
(e: LayoutChangeEvent) => {
|
|
306
|
+
if (isInitialized)
|
|
307
|
+
return
|
|
308
|
+
|
|
309
|
+
const { layout } = e.nativeEvent
|
|
310
|
+
|
|
311
|
+
if (layout.height <= 0)
|
|
312
|
+
return
|
|
313
|
+
|
|
314
|
+
notifyInputTextReset()
|
|
315
|
+
|
|
316
|
+
setIsInitialized(true)
|
|
317
|
+
setComposerHeight(minComposerHeight!)
|
|
318
|
+
setText(getTextFromProp(initialText))
|
|
319
|
+
},
|
|
320
|
+
[isInitialized, initialText, minComposerHeight, notifyInputTextReset, getTextFromProp]
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
const inputToolbarFragment = useMemo(() => {
|
|
324
|
+
if (!isInitialized)
|
|
325
|
+
return null
|
|
326
|
+
|
|
327
|
+
const inputToolbarProps = {
|
|
328
|
+
...props,
|
|
329
|
+
text: getTextFromProp(text!),
|
|
330
|
+
composerHeight: Math.max(minComposerHeight!, composerHeight),
|
|
331
|
+
onSend: _onSend,
|
|
332
|
+
onInputSizeChanged,
|
|
333
|
+
onTextChanged: _onInputTextChanged,
|
|
334
|
+
textInputProps: {
|
|
335
|
+
...textInputProps,
|
|
336
|
+
ref: textInputRef,
|
|
337
|
+
maxLength: isTypingDisabled ? 0 : maxInputLength,
|
|
338
|
+
},
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (renderInputToolbar)
|
|
342
|
+
return renderInputToolbar(inputToolbarProps)
|
|
343
|
+
|
|
344
|
+
return <InputToolbar {...inputToolbarProps} />
|
|
345
|
+
}, [
|
|
346
|
+
isInitialized,
|
|
347
|
+
_onSend,
|
|
348
|
+
getTextFromProp,
|
|
349
|
+
maxInputLength,
|
|
350
|
+
minComposerHeight,
|
|
351
|
+
onInputSizeChanged,
|
|
352
|
+
props,
|
|
353
|
+
text,
|
|
354
|
+
renderInputToolbar,
|
|
355
|
+
composerHeight,
|
|
356
|
+
isTypingDisabled,
|
|
357
|
+
textInputRef,
|
|
358
|
+
textInputProps,
|
|
359
|
+
_onInputTextChanged,
|
|
360
|
+
])
|
|
361
|
+
|
|
362
|
+
const contextValues = useMemo(
|
|
363
|
+
() => ({
|
|
364
|
+
actionSheet:
|
|
365
|
+
actionSheet ||
|
|
366
|
+
(() => ({
|
|
367
|
+
showActionSheetWithOptions:
|
|
368
|
+
actionSheetRef.current!.showActionSheetWithOptions,
|
|
369
|
+
})),
|
|
370
|
+
getLocale: () => locale,
|
|
371
|
+
}),
|
|
372
|
+
[actionSheet, locale]
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
useEffect(() => {
|
|
376
|
+
if (props.text != null)
|
|
377
|
+
setText(props.text)
|
|
378
|
+
}, [props.text])
|
|
379
|
+
|
|
380
|
+
useAnimatedReaction(
|
|
381
|
+
() => -keyboard.height.value,
|
|
382
|
+
(value, prevValue) => {
|
|
383
|
+
if (prevValue !== null && value !== prevValue) {
|
|
384
|
+
const isKeyboardMovingUp = value > prevValue
|
|
385
|
+
if (isKeyboardMovingUp !== trackingKeyboardMovement.value) {
|
|
386
|
+
trackingKeyboardMovement.value = isKeyboardMovingUp
|
|
387
|
+
keyboardOffsetBottom.value = withTiming(
|
|
388
|
+
isKeyboardMovingUp ? bottomOffset : 0,
|
|
389
|
+
{
|
|
390
|
+
// If `bottomOffset` exists, we change the duration to a smaller value to fix the delay in the keyboard animation speed
|
|
391
|
+
duration: bottomOffset ? 150 : 400,
|
|
392
|
+
}
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
if (focusOnInputWhenOpeningKeyboard)
|
|
396
|
+
if (isKeyboardMovingUp)
|
|
397
|
+
runOnJS(handleTextInputFocusWhenKeyboardShow)()
|
|
398
|
+
else
|
|
399
|
+
runOnJS(handleTextInputFocusWhenKeyboardHide)()
|
|
400
|
+
|
|
401
|
+
if (value === 0) {
|
|
402
|
+
runOnJS(enableTyping)()
|
|
403
|
+
} else {
|
|
404
|
+
runOnJS(disableTyping)()
|
|
405
|
+
runOnJS(debounceEnableTyping)()
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
},
|
|
410
|
+
[
|
|
411
|
+
keyboard,
|
|
412
|
+
trackingKeyboardMovement,
|
|
413
|
+
focusOnInputWhenOpeningKeyboard,
|
|
414
|
+
handleTextInputFocusWhenKeyboardHide,
|
|
415
|
+
handleTextInputFocusWhenKeyboardShow,
|
|
416
|
+
enableTyping,
|
|
417
|
+
disableTyping,
|
|
418
|
+
debounceEnableTyping,
|
|
419
|
+
bottomOffset,
|
|
420
|
+
]
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
console.log('!')
|
|
424
|
+
|
|
425
|
+
return (
|
|
426
|
+
<GiftedChatContext.Provider value={contextValues}>
|
|
427
|
+
<ActionSheetProvider ref={actionSheetRef}>
|
|
428
|
+
<View
|
|
429
|
+
testID={TEST_ID.WRAPPER}
|
|
430
|
+
style={[stylesCommon.fill, styles.contentContainer]}
|
|
431
|
+
onLayout={onInitialLayoutViewLayout}
|
|
432
|
+
>
|
|
433
|
+
{isInitialized
|
|
434
|
+
? (
|
|
435
|
+
<Animated.View style={[stylesCommon.fill, isKeyboardInternallyHandled && contentStyleAnim]}>
|
|
436
|
+
{renderMessages}
|
|
437
|
+
{inputToolbarFragment}
|
|
438
|
+
</Animated.View>
|
|
439
|
+
)
|
|
440
|
+
: (
|
|
441
|
+
renderLoading?.()
|
|
442
|
+
)}
|
|
443
|
+
</View>
|
|
444
|
+
</ActionSheetProvider>
|
|
445
|
+
</GiftedChatContext.Provider>
|
|
446
|
+
)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function GiftedChatWrapper (props: GiftedChatProps) {
|
|
450
|
+
return (
|
|
451
|
+
<KeyboardProvider>
|
|
452
|
+
<GiftedChat {...props} />
|
|
453
|
+
</KeyboardProvider>
|
|
454
|
+
)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
GiftedChatWrapper.append = <TMessage extends IMessage>(
|
|
458
|
+
currentMessages: TMessage[] = [],
|
|
459
|
+
messages: TMessage[],
|
|
460
|
+
inverted = true
|
|
461
|
+
) => {
|
|
462
|
+
if (!Array.isArray(messages))
|
|
463
|
+
messages = [messages]
|
|
464
|
+
|
|
465
|
+
return inverted
|
|
466
|
+
? messages.concat(currentMessages)
|
|
467
|
+
: currentMessages.concat(messages)
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
GiftedChatWrapper.prepend = <TMessage extends IMessage>(
|
|
471
|
+
currentMessages: TMessage[] = [],
|
|
472
|
+
messages: TMessage[],
|
|
473
|
+
inverted = true
|
|
474
|
+
) => {
|
|
475
|
+
if (!Array.isArray(messages))
|
|
476
|
+
messages = [messages]
|
|
477
|
+
|
|
478
|
+
return inverted
|
|
479
|
+
? currentMessages.concat(messages)
|
|
480
|
+
: messages.concat(currentMessages)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
export * from '../types'
|
|
484
|
+
|
|
485
|
+
export {
|
|
486
|
+
GiftedChatWrapper as GiftedChat,
|
|
487
|
+
Actions,
|
|
488
|
+
Avatar,
|
|
489
|
+
Bubble,
|
|
490
|
+
SystemMessage,
|
|
491
|
+
MessageImage,
|
|
492
|
+
MessageText,
|
|
493
|
+
Composer,
|
|
494
|
+
Day,
|
|
495
|
+
InputToolbar,
|
|
496
|
+
LoadEarlier,
|
|
497
|
+
Message,
|
|
498
|
+
MessageContainer,
|
|
499
|
+
Send,
|
|
500
|
+
Time,
|
|
501
|
+
GiftedAvatar,
|
|
502
|
+
utils
|
|
503
|
+
}
|