-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Time.tsx
99 lines (92 loc) 路 2.16 KB
/
Time.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
import * as React from 'react'
import PropTypes from 'prop-types'
import { StyleSheet, Text, View, ViewStyle, TextStyle } from 'react-native'
import dayjs from 'dayjs'
import Color from './Color'
import { TIME_FORMAT } from './Constant'
import { LeftRightStyle, IMessage } from './Models'
import { StylePropType } from './utils'
import { useChatContext } from './GiftedChatContext'
const { containerStyle } = StyleSheet.create({
containerStyle: {
marginLeft: 10,
marginRight: 10,
marginBottom: 5,
},
})
const { textStyle } = StyleSheet.create({
textStyle: {
fontSize: 10,
backgroundColor: 'transparent',
textAlign: 'right',
},
})
const styles = {
left: StyleSheet.create({
container: {
...containerStyle,
},
text: {
color: Color.timeTextColor,
...textStyle,
},
}),
right: StyleSheet.create({
container: {
...containerStyle,
},
text: {
color: Color.white,
...textStyle,
},
}),
}
export interface TimeProps<TMessage extends IMessage> {
position?: 'left' | 'right'
currentMessage?: TMessage
containerStyle?: LeftRightStyle<ViewStyle>
timeTextStyle?: LeftRightStyle<TextStyle>
timeFormat?: string
}
export function Time<TMessage extends IMessage = IMessage>({
position = 'left',
containerStyle,
currentMessage,
timeFormat = TIME_FORMAT,
timeTextStyle,
}: TimeProps<TMessage>) {
const { getLocale } = useChatContext()
if (currentMessage == null) {
return null
}
return (
<View
style={[
styles[position].container,
containerStyle && containerStyle[position],
]}
>
<Text
style={[
styles[position].text,
timeTextStyle && timeTextStyle[position],
]}
>
{dayjs(currentMessage.createdAt).locale(getLocale()).format(timeFormat)}
</Text>
</View>
)
}
Time.propTypes = {
position: PropTypes.oneOf(['left', 'right']),
currentMessage: PropTypes.object,
containerStyle: PropTypes.shape({
left: StylePropType,
right: StylePropType,
}),
timeFormat: PropTypes.string,
timeTextStyle: PropTypes.shape({
left: StylePropType,
right: StylePropType,
}),
}