react-native-gifted-chat 2.4.1 → 2.6.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 +34 -14
- package/lib/Actions.d.ts +5 -6
- package/lib/Actions.js +16 -13
- package/lib/Actions.js.map +1 -1
- package/lib/Avatar.d.ts +9 -25
- package/lib/Avatar.js +12 -18
- package/lib/Avatar.js.flow +1 -1
- package/lib/Avatar.js.map +1 -1
- package/lib/Bubble.d.ts +29 -30
- package/lib/Bubble.js +99 -92
- package/lib/Bubble.js.flow +2 -2
- package/lib/Bubble.js.map +1 -1
- package/lib/Composer.d.ts +1 -1
- package/lib/Composer.js +30 -32
- package/lib/Composer.js.map +1 -1
- package/lib/Constant.js +1 -0
- package/lib/Constant.js.map +1 -1
- package/lib/Day.d.ts +3 -15
- package/lib/Day.js +2 -14
- package/lib/Day.js.flow +1 -1
- package/lib/Day.js.map +1 -1
- package/lib/GiftedAvatar.d.ts +7 -7
- package/lib/GiftedAvatar.js +30 -29
- package/lib/GiftedAvatar.js.map +1 -1
- package/lib/GiftedChat.d.ts +15 -89
- package/lib/GiftedChat.js +204 -350
- package/lib/GiftedChat.js.flow +1 -3
- package/lib/GiftedChat.js.map +1 -1
- package/lib/GiftedChatContext.d.ts +2 -1
- package/lib/GiftedChatContext.js.map +1 -1
- package/lib/InputToolbar.d.ts +7 -5
- package/lib/InputToolbar.js +41 -34
- package/lib/InputToolbar.js.map +1 -1
- package/lib/LoadEarlier.d.ts +4 -4
- package/lib/LoadEarlier.js +8 -6
- package/lib/LoadEarlier.js.map +1 -1
- package/lib/Message.d.ts +8 -9
- package/lib/Message.js +47 -40
- package/lib/Message.js.flow +1 -1
- package/lib/Message.js.map +1 -1
- package/lib/MessageAudio.d.ts +2 -1
- package/lib/MessageAudio.js +4 -4
- package/lib/MessageAudio.js.flow +1 -1
- package/lib/MessageAudio.js.map +1 -1
- package/lib/MessageContainer.d.ts +17 -17
- package/lib/MessageContainer.js +33 -51
- package/lib/MessageContainer.js.map +1 -1
- package/lib/MessageImage.d.ts +5 -4
- package/lib/MessageImage.js +4 -5
- package/lib/MessageImage.js.flow +1 -1
- package/lib/MessageImage.js.map +1 -1
- package/lib/MessageText.d.ts +11 -10
- package/lib/MessageText.js +5 -10
- package/lib/MessageText.js.flow +1 -1
- package/lib/MessageText.js.map +1 -1
- package/lib/MessageVideo.d.ts +2 -1
- package/lib/MessageVideo.js +4 -4
- package/lib/MessageVideo.js.flow +1 -1
- package/lib/MessageVideo.js.map +1 -1
- package/lib/Models.d.ts +7 -7
- package/lib/QuickReplies.d.ts +3 -3
- package/lib/QuickReplies.js +8 -14
- package/lib/QuickReplies.js.flow +1 -1
- package/lib/QuickReplies.js.map +1 -1
- package/lib/Send.d.ts +4 -4
- package/lib/Send.js +6 -9
- package/lib/Send.js.map +1 -1
- package/lib/SystemMessage.d.ts +6 -5
- package/lib/SystemMessage.js +1 -2
- package/lib/SystemMessage.js.flow +1 -1
- package/lib/SystemMessage.js.map +1 -1
- package/lib/Time.d.ts +7 -6
- package/lib/Time.js +1 -3
- package/lib/Time.js.flow +1 -1
- package/lib/Time.js.map +1 -1
- package/lib/TypingIndicator.d.ts +2 -1
- package/lib/TypingIndicator.js +5 -5
- package/lib/TypingIndicator.js.map +1 -1
- package/lib/hooks/useUpdateLayoutEffect.js +2 -4
- package/lib/hooks/useUpdateLayoutEffect.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/logging.d.ts +2 -2
- package/lib/logging.js.map +1 -1
- package/lib/types.js.flow +1 -1
- package/lib/utils.d.ts +1 -1
- package/lib/utils.js +2 -4
- package/lib/utils.js.map +1 -1
- package/package.json +86 -59
- package/src/Actions.tsx +114 -0
- package/src/Avatar.tsx +178 -0
- package/src/Bubble.tsx +596 -0
- package/src/Color.ts +17 -0
- package/src/Composer.tsx +147 -0
- package/src/Constant.ts +18 -0
- package/src/Day.tsx +71 -0
- package/src/GiftedAvatar.tsx +205 -0
- package/src/GiftedChat.tsx +670 -0
- package/src/GiftedChatContext.ts +23 -0
- package/src/InputToolbar.tsx +113 -0
- package/src/LoadEarlier.tsx +108 -0
- package/src/Message.tsx +229 -0
- package/src/MessageAudio.tsx +19 -0
- package/src/MessageContainer.tsx +362 -0
- package/src/MessageImage.tsx +78 -0
- package/src/MessageText.tsx +187 -0
- package/src/MessageVideo.tsx +19 -0
- package/src/Models.ts +84 -0
- package/src/QuickReplies.tsx +186 -0
- package/src/Send.tsx +102 -0
- package/src/SystemMessage.tsx +61 -0
- package/src/Time.tsx +97 -0
- package/src/TypingIndicator.tsx +108 -0
- package/src/__tests__/Actions.test.tsx +10 -0
- package/src/__tests__/Avatar.test.tsx +13 -0
- package/src/__tests__/Bubble.test.tsx +23 -0
- package/src/__tests__/Color.test.tsx +5 -0
- package/src/__tests__/Composer.test.tsx +11 -0
- package/src/__tests__/Constant.test.tsx +5 -0
- package/src/__tests__/Day.test.tsx +23 -0
- package/src/__tests__/GiftedAvatar.test.tsx +11 -0
- package/src/__tests__/GiftedChat.test.tsx +36 -0
- package/src/__tests__/InputToolbar.test.tsx +11 -0
- package/src/__tests__/LoadEarlier.test.tsx +11 -0
- package/src/__tests__/Message.test.tsx +77 -0
- package/src/__tests__/MessageContainer.test.tsx +11 -0
- package/src/__tests__/MessageImage.test.tsx +27 -0
- package/src/__tests__/MessageText.test.tsx +11 -0
- package/src/__tests__/Send.test.tsx +22 -0
- package/src/__tests__/SystemMessage.test.tsx +27 -0
- package/src/__tests__/Time.test.tsx +29 -0
- package/src/__tests__/__snapshots__/Actions.test.tsx.snap +76 -0
- package/src/__tests__/__snapshots__/Avatar.test.tsx.snap +17 -0
- package/src/__tests__/__snapshots__/Bubble.test.tsx.snap +145 -0
- package/src/__tests__/__snapshots__/Color.test.tsx.snap +21 -0
- package/src/__tests__/__snapshots__/Composer.test.tsx.snap +35 -0
- package/src/__tests__/__snapshots__/Constant.test.tsx.snap +16 -0
- package/src/__tests__/__snapshots__/Day.test.tsx.snap +37 -0
- package/src/__tests__/__snapshots__/GiftedAvatar.test.tsx.snap +22 -0
- package/src/__tests__/__snapshots__/GiftedChat.test.tsx.snap +15 -0
- package/src/__tests__/__snapshots__/InputToolbar.test.tsx.snap +60 -0
- package/src/__tests__/__snapshots__/LoadEarlier.test.tsx.snap +74 -0
- package/src/__tests__/__snapshots__/Message.test.tsx.snap +628 -0
- package/src/__tests__/__snapshots__/MessageContainer.test.tsx.snap +127 -0
- package/src/__tests__/__snapshots__/MessageImage.test.tsx.snap +38 -0
- package/src/__tests__/__snapshots__/MessageText.test.tsx.snap +30 -0
- package/src/__tests__/__snapshots__/Send.test.tsx.snap +129 -0
- package/src/__tests__/__snapshots__/SystemMessage.test.tsx.snap +38 -0
- package/src/__tests__/__snapshots__/Time.test.tsx.snap +33 -0
- package/src/__tests__/data.ts +8 -0
- package/src/__tests__/utils.test.ts +31 -0
- package/src/hooks/useUpdateLayoutEffect.ts +21 -0
- package/src/index.ts +4 -0
- package/src/logging.ts +8 -0
- package/src/utils.ts +39 -0
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -21
- package/jest.config.js +0 -15
package/package.json
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-gifted-chat",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "The most complete chat UI for React Native",
|
|
5
|
-
"main": "lib/index.js",
|
|
6
|
-
"types": "lib/index.d.ts",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/FaridSafi/react-native-gifted-chat.git"
|
|
10
|
-
},
|
|
11
5
|
"keywords": [
|
|
12
6
|
"android",
|
|
13
7
|
"ios",
|
|
@@ -18,72 +12,105 @@
|
|
|
18
12
|
"message",
|
|
19
13
|
"chat"
|
|
20
14
|
],
|
|
21
|
-
"
|
|
22
|
-
"license": "MIT",
|
|
15
|
+
"homepage": "https://github.com/FaridSafi/react-native-gifted-chat#readme",
|
|
23
16
|
"bugs": {
|
|
24
17
|
"url": "https://github.com/FaridSafi/react-native-gifted-chat/issues"
|
|
25
18
|
},
|
|
26
|
-
"
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/FaridSafi/react-native-gifted-chat.git"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "Farid Safi",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "lib/index.js",
|
|
27
|
+
"types": "lib/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"src",
|
|
30
|
+
"lib"
|
|
31
|
+
],
|
|
27
32
|
"scripts": {
|
|
28
|
-
"
|
|
29
|
-
"lint
|
|
30
|
-
"
|
|
31
|
-
"
|
|
33
|
+
"build": "rm -rf lib/ && yarn tsc && cp flow-typedefs/*.js.flow lib/",
|
|
34
|
+
"lint": "yarn eslint src",
|
|
35
|
+
"lint:fix": "yarn eslint --cache --fix",
|
|
36
|
+
"prepublishOnly": "yarn lint && yarn build && yarn test",
|
|
32
37
|
"start": "cd example && expo start",
|
|
33
38
|
"start:web": "cd example && expo start -w --dev",
|
|
34
|
-
"build": "rm -rf lib/ && node_modules/.bin/tsc && cp flow-typedefs/*.js.flow lib/",
|
|
35
39
|
"test": "TZ=Europe/Paris jest --no-watchman",
|
|
36
|
-
"test:watch": "TZ=Europe/Paris jest --watch",
|
|
37
40
|
"test:coverage": "TZ=Europe/Paris jest --coverage",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
41
|
+
"test:watch": "TZ=Europe/Paris jest --watch",
|
|
42
|
+
"tsc:write": "yarn tsc --project tsconfig.json",
|
|
43
|
+
"tsc:watch": "yarn tsc --watch --noEmit",
|
|
44
|
+
"fresh": "yarn start --reset-cache",
|
|
45
|
+
"prepare": "yarn husky"
|
|
40
46
|
},
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"@types/jest": "27.4.1",
|
|
47
|
-
"@types/react": "~16.9.35",
|
|
48
|
-
"@types/react-native": "~0.63.2",
|
|
49
|
-
"@types/react-test-renderer": "16.9.2",
|
|
50
|
-
"@types/uuid": "3.4.9",
|
|
51
|
-
"@typescript-eslint/eslint-plugin": "5.18.0",
|
|
52
|
-
"@typescript-eslint/parser": "5.18.0",
|
|
53
|
-
"babel-jest": "27.5.1",
|
|
54
|
-
"eslint": "8.13.0",
|
|
55
|
-
"eslint-config-prettier": "8.5.0",
|
|
56
|
-
"flow-bin": "0.125.0",
|
|
57
|
-
"husky": "4.2.5",
|
|
58
|
-
"jest": "27.5.1",
|
|
59
|
-
"json": "9.0.6",
|
|
60
|
-
"metro-react-native-babel-preset": "0.70.1",
|
|
61
|
-
"prettier": "2.0.5",
|
|
62
|
-
"react": "16.13.1",
|
|
63
|
-
"react-dom": "16.13.1",
|
|
64
|
-
"react-native": "0.63.4",
|
|
65
|
-
"react-test-renderer": "16.9.0",
|
|
66
|
-
"typescript": "4.6.3"
|
|
47
|
+
"lint-staged": {
|
|
48
|
+
"src/*.{json,js,jsx,ts,tsx}": [
|
|
49
|
+
"yarn lint:fix",
|
|
50
|
+
"bash -c 'yarn tsc:write'"
|
|
51
|
+
]
|
|
67
52
|
},
|
|
68
53
|
"dependencies": {
|
|
69
|
-
"@expo/react-native-action-sheet": "4.0
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"react-native-
|
|
75
|
-
"react-native-
|
|
76
|
-
"react-native-
|
|
77
|
-
"
|
|
78
|
-
"
|
|
54
|
+
"@expo/react-native-action-sheet": "^4.1.0",
|
|
55
|
+
"@types/lodash.isequal": "^4.5.8",
|
|
56
|
+
"dayjs": "^1.11.13",
|
|
57
|
+
"lodash.isequal": "^4.5.0",
|
|
58
|
+
"prop-types": "^15.8.1",
|
|
59
|
+
"react-native-communications": "^2.2.1",
|
|
60
|
+
"react-native-get-random-values": "^1.11.0",
|
|
61
|
+
"react-native-iphone-x-helper": "^1.3.1",
|
|
62
|
+
"react-native-lightbox-v2": "^0.9.0",
|
|
63
|
+
"react-native-parsed-text": "^0.0.22",
|
|
64
|
+
"react-native-typing-animation": "^0.1.7",
|
|
65
|
+
"use-memo-one": "^1.1.3",
|
|
66
|
+
"uuid": "^10.0.0"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@babel/core": "^7.25.2",
|
|
70
|
+
"@babel/plugin-transform-react-jsx": "^7.25.2",
|
|
71
|
+
"@babel/plugin-transform-unicode-property-regex": "^7.24.7",
|
|
72
|
+
"@babel/preset-env": "^7.25.4",
|
|
73
|
+
"@react-native/eslint-config": "^0.74.82",
|
|
74
|
+
"@stylistic/eslint-plugin": "^2.6.4",
|
|
75
|
+
"@types/jest": "^29.5.12",
|
|
76
|
+
"@types/react": "^18.3.3",
|
|
77
|
+
"@types/react-native": "^0.72.8",
|
|
78
|
+
"@types/react-test-renderer": "^18.3.0",
|
|
79
|
+
"@types/uuid": "^10.0.0",
|
|
80
|
+
"@typescript-eslint/eslint-plugin": "^8.2.0",
|
|
81
|
+
"@typescript-eslint/parser": "^8.2.0",
|
|
82
|
+
"babel-jest": "^29.7.0",
|
|
83
|
+
"eslint": "^8.57.0",
|
|
84
|
+
"eslint-config-standard": "^17.1.0",
|
|
85
|
+
"eslint-config-standard-jsx": "^11.0.0",
|
|
86
|
+
"eslint-plugin-import": "^2.29.1",
|
|
87
|
+
"eslint-plugin-jest": "^28.8.0",
|
|
88
|
+
"eslint-plugin-json": "^4.0.1",
|
|
89
|
+
"eslint-plugin-n": "^17.10.2",
|
|
90
|
+
"eslint-plugin-node": "^11.1.0",
|
|
91
|
+
"eslint-plugin-promise": "^7.1.0",
|
|
92
|
+
"eslint-plugin-react": "^7.35.0",
|
|
93
|
+
"flow-bin": "^0.244.0",
|
|
94
|
+
"husky": "^9.1.5",
|
|
95
|
+
"jest": "^29.7.0",
|
|
96
|
+
"json": "^11.0.0",
|
|
97
|
+
"lint-staged": "^15.2.9",
|
|
98
|
+
"react": "^18.3.1",
|
|
99
|
+
"react-dom": "^18.3.1",
|
|
100
|
+
"react-native": "^0.74.5",
|
|
101
|
+
"react-native-reanimated": "^3.15.0",
|
|
102
|
+
"react-native-safe-area-context": "^4.10.9",
|
|
103
|
+
"react-test-renderer": "^18.3.1",
|
|
104
|
+
"typescript": "^5.5.4"
|
|
79
105
|
},
|
|
80
106
|
"peerDependencies": {
|
|
81
107
|
"react": "*",
|
|
82
|
-
"react-native": "*"
|
|
108
|
+
"react-native": "*",
|
|
109
|
+
"react-native-reanimated": "*",
|
|
110
|
+
"react-native-safe-area-context": "*"
|
|
83
111
|
},
|
|
84
|
-
"
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
112
|
+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
|
|
113
|
+
"engines": {
|
|
114
|
+
"node": ">=18"
|
|
88
115
|
}
|
|
89
116
|
}
|
package/src/Actions.tsx
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'
|
|
2
|
+
import React, { ReactNode, useCallback } from 'react'
|
|
3
|
+
import {
|
|
4
|
+
StyleSheet,
|
|
5
|
+
Text,
|
|
6
|
+
TouchableOpacity,
|
|
7
|
+
View,
|
|
8
|
+
StyleProp,
|
|
9
|
+
ViewStyle,
|
|
10
|
+
TextStyle,
|
|
11
|
+
} from 'react-native'
|
|
12
|
+
import Color from './Color'
|
|
13
|
+
import { StylePropType } from './utils'
|
|
14
|
+
import { useChatContext } from './GiftedChatContext'
|
|
15
|
+
|
|
16
|
+
export interface ActionsProps {
|
|
17
|
+
options?: { [key: string]: () => void }
|
|
18
|
+
optionTintColor?: string
|
|
19
|
+
icon?: () => ReactNode
|
|
20
|
+
wrapperStyle?: StyleProp<ViewStyle>
|
|
21
|
+
iconTextStyle?: StyleProp<TextStyle>
|
|
22
|
+
containerStyle?: StyleProp<ViewStyle>
|
|
23
|
+
onPressActionButton?(): void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function Actions ({
|
|
27
|
+
options,
|
|
28
|
+
optionTintColor = Color.optionTintColor,
|
|
29
|
+
icon,
|
|
30
|
+
wrapperStyle,
|
|
31
|
+
iconTextStyle,
|
|
32
|
+
onPressActionButton,
|
|
33
|
+
containerStyle,
|
|
34
|
+
}: ActionsProps) {
|
|
35
|
+
const { actionSheet } = useChatContext()
|
|
36
|
+
|
|
37
|
+
const onActionsPress = useCallback(() => {
|
|
38
|
+
if (!options)
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
const optionKeys = Object.keys(options)
|
|
42
|
+
const cancelButtonIndex = optionKeys.indexOf('Cancel')
|
|
43
|
+
|
|
44
|
+
actionSheet().showActionSheetWithOptions(
|
|
45
|
+
{
|
|
46
|
+
options: optionKeys,
|
|
47
|
+
cancelButtonIndex,
|
|
48
|
+
tintColor: optionTintColor,
|
|
49
|
+
},
|
|
50
|
+
(buttonIndex: number | undefined) => {
|
|
51
|
+
if (buttonIndex === undefined)
|
|
52
|
+
return
|
|
53
|
+
|
|
54
|
+
const key = optionKeys[buttonIndex]
|
|
55
|
+
if (key)
|
|
56
|
+
options[key]()
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
}, [actionSheet, options, optionTintColor])
|
|
60
|
+
|
|
61
|
+
const renderIcon = useCallback(() => {
|
|
62
|
+
if (icon)
|
|
63
|
+
return icon()
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<View style={[styles.wrapper, wrapperStyle]}>
|
|
67
|
+
<Text style={[styles.iconText, iconTextStyle]}>{'+'}</Text>
|
|
68
|
+
</View>
|
|
69
|
+
)
|
|
70
|
+
}, [icon, iconTextStyle, wrapperStyle])
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<TouchableOpacity
|
|
74
|
+
style={[styles.container, containerStyle]}
|
|
75
|
+
onPress={onPressActionButton || onActionsPress}
|
|
76
|
+
>
|
|
77
|
+
{renderIcon()}
|
|
78
|
+
</TouchableOpacity>
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
Actions.propTypes = {
|
|
83
|
+
options: PropTypes.object,
|
|
84
|
+
optionTintColor: PropTypes.string,
|
|
85
|
+
icon: PropTypes.func,
|
|
86
|
+
onPressActionButton: PropTypes.func,
|
|
87
|
+
wrapperStyle: StylePropType,
|
|
88
|
+
containerStyle: StylePropType,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const styles = StyleSheet.create({
|
|
92
|
+
container: {
|
|
93
|
+
width: 26,
|
|
94
|
+
height: 26,
|
|
95
|
+
marginLeft: 10,
|
|
96
|
+
marginBottom: 10,
|
|
97
|
+
},
|
|
98
|
+
wrapper: {
|
|
99
|
+
borderRadius: 13,
|
|
100
|
+
borderColor: Color.defaultColor,
|
|
101
|
+
borderWidth: 2,
|
|
102
|
+
flex: 1,
|
|
103
|
+
alignItems: 'center',
|
|
104
|
+
justifyContent: 'center',
|
|
105
|
+
},
|
|
106
|
+
iconText: {
|
|
107
|
+
color: Color.defaultColor,
|
|
108
|
+
fontWeight: 'bold',
|
|
109
|
+
fontSize: 16,
|
|
110
|
+
lineHeight: 16,
|
|
111
|
+
backgroundColor: Color.backgroundTransparent,
|
|
112
|
+
textAlign: 'center',
|
|
113
|
+
},
|
|
114
|
+
})
|
package/src/Avatar.tsx
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'
|
|
2
|
+
import React, { ReactNode } from 'react'
|
|
3
|
+
import {
|
|
4
|
+
ImageStyle,
|
|
5
|
+
StyleSheet,
|
|
6
|
+
TextStyle,
|
|
7
|
+
View,
|
|
8
|
+
ViewStyle,
|
|
9
|
+
} from 'react-native'
|
|
10
|
+
import GiftedAvatar from './GiftedAvatar'
|
|
11
|
+
import { StylePropType, isSameUser, isSameDay } from './utils'
|
|
12
|
+
import { IMessage, LeftRightStyle, User } from './Models'
|
|
13
|
+
|
|
14
|
+
interface Styles {
|
|
15
|
+
left: {
|
|
16
|
+
container: ViewStyle
|
|
17
|
+
onTop: ViewStyle
|
|
18
|
+
image: ImageStyle
|
|
19
|
+
}
|
|
20
|
+
right: {
|
|
21
|
+
container: ViewStyle
|
|
22
|
+
onTop: ViewStyle
|
|
23
|
+
image: ImageStyle
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const styles: Styles = {
|
|
28
|
+
left: StyleSheet.create({
|
|
29
|
+
container: {
|
|
30
|
+
marginRight: 8,
|
|
31
|
+
},
|
|
32
|
+
onTop: {
|
|
33
|
+
alignSelf: 'flex-start',
|
|
34
|
+
},
|
|
35
|
+
image: {
|
|
36
|
+
height: 36,
|
|
37
|
+
width: 36,
|
|
38
|
+
borderRadius: 18,
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
right: StyleSheet.create({
|
|
42
|
+
container: {
|
|
43
|
+
marginLeft: 8,
|
|
44
|
+
},
|
|
45
|
+
onTop: {
|
|
46
|
+
alignSelf: 'flex-start',
|
|
47
|
+
},
|
|
48
|
+
image: {
|
|
49
|
+
height: 36,
|
|
50
|
+
width: 36,
|
|
51
|
+
borderRadius: 18,
|
|
52
|
+
},
|
|
53
|
+
}),
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface AvatarProps<TMessage extends IMessage> {
|
|
57
|
+
currentMessage: TMessage
|
|
58
|
+
previousMessage?: TMessage
|
|
59
|
+
nextMessage?: TMessage
|
|
60
|
+
position: 'left' | 'right'
|
|
61
|
+
renderAvatarOnTop?: boolean
|
|
62
|
+
showAvatarForEveryMessage?: boolean
|
|
63
|
+
imageStyle?: LeftRightStyle<ImageStyle>
|
|
64
|
+
containerStyle?: LeftRightStyle<ViewStyle>
|
|
65
|
+
textStyle?: TextStyle
|
|
66
|
+
renderAvatar?(props: Omit<AvatarProps<TMessage>, 'renderAvatar'>): ReactNode
|
|
67
|
+
onPressAvatar?: (user: User) => void
|
|
68
|
+
onLongPressAvatar?: (user: User) => void
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function Avatar<TMessage extends IMessage = IMessage> (
|
|
72
|
+
props: AvatarProps<TMessage>
|
|
73
|
+
) {
|
|
74
|
+
const {
|
|
75
|
+
renderAvatarOnTop,
|
|
76
|
+
showAvatarForEveryMessage,
|
|
77
|
+
containerStyle,
|
|
78
|
+
position,
|
|
79
|
+
currentMessage,
|
|
80
|
+
renderAvatar,
|
|
81
|
+
previousMessage,
|
|
82
|
+
nextMessage,
|
|
83
|
+
imageStyle,
|
|
84
|
+
onPressAvatar,
|
|
85
|
+
onLongPressAvatar,
|
|
86
|
+
} = props
|
|
87
|
+
|
|
88
|
+
const messageToCompare = renderAvatarOnTop ? previousMessage : nextMessage
|
|
89
|
+
|
|
90
|
+
if (renderAvatar === null)
|
|
91
|
+
return null
|
|
92
|
+
|
|
93
|
+
if (
|
|
94
|
+
!showAvatarForEveryMessage &&
|
|
95
|
+
currentMessage &&
|
|
96
|
+
messageToCompare &&
|
|
97
|
+
isSameUser(currentMessage, messageToCompare) &&
|
|
98
|
+
isSameDay(currentMessage, messageToCompare)
|
|
99
|
+
)
|
|
100
|
+
return (
|
|
101
|
+
<View
|
|
102
|
+
style={[
|
|
103
|
+
styles[position].container,
|
|
104
|
+
containerStyle?.[position],
|
|
105
|
+
]}
|
|
106
|
+
>
|
|
107
|
+
<GiftedAvatar
|
|
108
|
+
avatarStyle={[
|
|
109
|
+
styles[position].image,
|
|
110
|
+
imageStyle?.[position],
|
|
111
|
+
]}
|
|
112
|
+
/>
|
|
113
|
+
</View>
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
const renderAvatarComponent = () => {
|
|
117
|
+
if (renderAvatar)
|
|
118
|
+
return renderAvatar({
|
|
119
|
+
renderAvatarOnTop,
|
|
120
|
+
showAvatarForEveryMessage,
|
|
121
|
+
containerStyle,
|
|
122
|
+
position,
|
|
123
|
+
currentMessage,
|
|
124
|
+
previousMessage,
|
|
125
|
+
nextMessage,
|
|
126
|
+
imageStyle,
|
|
127
|
+
onPressAvatar,
|
|
128
|
+
onLongPressAvatar,
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
if (currentMessage)
|
|
132
|
+
return (
|
|
133
|
+
<GiftedAvatar
|
|
134
|
+
avatarStyle={[
|
|
135
|
+
styles[position].image,
|
|
136
|
+
imageStyle?.[position],
|
|
137
|
+
]}
|
|
138
|
+
user={currentMessage.user}
|
|
139
|
+
onPress={() => onPressAvatar?.(currentMessage.user)}
|
|
140
|
+
onLongPress={() => onLongPressAvatar?.(currentMessage.user)}
|
|
141
|
+
/>
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
return null
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<View
|
|
149
|
+
style={[
|
|
150
|
+
styles[position].container,
|
|
151
|
+
renderAvatarOnTop && styles[position].onTop,
|
|
152
|
+
containerStyle?.[position],
|
|
153
|
+
]}
|
|
154
|
+
>
|
|
155
|
+
{renderAvatarComponent()}
|
|
156
|
+
</View>
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
Avatar.propTypes = {
|
|
161
|
+
renderAvatarOnTop: PropTypes.bool,
|
|
162
|
+
showAvatarForEveryMessage: PropTypes.bool,
|
|
163
|
+
position: PropTypes.oneOf(['left', 'right']),
|
|
164
|
+
currentMessage: PropTypes.object,
|
|
165
|
+
previousMessage: PropTypes.object,
|
|
166
|
+
nextMessage: PropTypes.object,
|
|
167
|
+
onPressAvatar: PropTypes.func,
|
|
168
|
+
onLongPressAvatar: PropTypes.func,
|
|
169
|
+
renderAvatar: PropTypes.func,
|
|
170
|
+
containerStyle: PropTypes.shape({
|
|
171
|
+
left: StylePropType,
|
|
172
|
+
right: StylePropType,
|
|
173
|
+
}),
|
|
174
|
+
imageStyle: PropTypes.shape({
|
|
175
|
+
left: StylePropType,
|
|
176
|
+
right: StylePropType,
|
|
177
|
+
}),
|
|
178
|
+
}
|