1 | import * as React from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import { StyleSheet, Text, View } from 'react-native';
|
4 | import dayjs from 'dayjs';
|
5 | import Color from './Color';
|
6 | import { TIME_FORMAT } from './Constant';
|
7 | import { StylePropType } from './utils';
|
8 | import { useChatContext } from './GiftedChatContext';
|
9 | const { containerStyle } = StyleSheet.create({
|
10 | containerStyle: {
|
11 | marginLeft: 10,
|
12 | marginRight: 10,
|
13 | marginBottom: 5,
|
14 | },
|
15 | });
|
16 | const { textStyle } = StyleSheet.create({
|
17 | textStyle: {
|
18 | fontSize: 10,
|
19 | backgroundColor: 'transparent',
|
20 | textAlign: 'right',
|
21 | },
|
22 | });
|
23 | const styles = {
|
24 | left: StyleSheet.create({
|
25 | container: {
|
26 | ...containerStyle,
|
27 | },
|
28 | text: {
|
29 | color: Color.timeTextColor,
|
30 | ...textStyle,
|
31 | },
|
32 | }),
|
33 | right: StyleSheet.create({
|
34 | container: {
|
35 | ...containerStyle,
|
36 | },
|
37 | text: {
|
38 | color: Color.white,
|
39 | ...textStyle,
|
40 | },
|
41 | }),
|
42 | };
|
43 | export function Time({ position = 'left', containerStyle, currentMessage, timeFormat = TIME_FORMAT, timeTextStyle, }) {
|
44 | const { getLocale } = useChatContext();
|
45 | if (currentMessage == null) {
|
46 | return null;
|
47 | }
|
48 | return (<View style={[
|
49 | styles[position].container,
|
50 | containerStyle && containerStyle[position],
|
51 | ]}>
|
52 | <Text style={[
|
53 | styles[position].text,
|
54 | timeTextStyle && timeTextStyle[position],
|
55 | ]}>
|
56 | {dayjs(currentMessage.createdAt).locale(getLocale()).format(timeFormat)}
|
57 | </Text>
|
58 | </View>);
|
59 | }
|
60 | Time.propTypes = {
|
61 | position: PropTypes.oneOf(['left', 'right']),
|
62 | currentMessage: PropTypes.object,
|
63 | containerStyle: PropTypes.shape({
|
64 | left: StylePropType,
|
65 | right: StylePropType,
|
66 | }),
|
67 | timeFormat: PropTypes.string,
|
68 | timeTextStyle: PropTypes.shape({
|
69 | left: StylePropType,
|
70 | right: StylePropType,
|
71 | }),
|
72 | };
|
73 |
|
\ | No newline at end of file |