mattermost-mobile/app/components/formatted_time.js
Mattermost Build 81b1ba8489
Fix propTypes warnings (#5285) (#5287)
(cherry picked from commit c6953bef47)

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2021-04-07 10:41:03 -04:00

56 lines
1.5 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import {Text} from 'react-native';
import moment from 'moment-timezone';
export default class FormattedTime extends React.PureComponent {
static propTypes = {
value: PropTypes.any.isRequired,
timeZone: PropTypes.string,
children: PropTypes.func,
hour12: PropTypes.bool,
style: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
testID: PropTypes.string,
};
getFormattedTime = () => {
const {
value,
timeZone,
hour12,
} = this.props;
let format = 'H:mm';
if (hour12) {
const localeFormat = moment.localeData().longDateFormat('LT');
format = localeFormat?.includes('A') ? localeFormat : 'h:mm A';
}
if (timeZone) {
return moment.tz(value, timeZone).format(format);
}
return moment(value).format(format);
};
render() {
const {children, style, testID} = this.props;
const formattedTime = this.getFormattedTime();
if (typeof children === 'function') {
return children(formattedTime);
}
return (
<Text
style={style}
testID={testID}
>
{formattedTime}
</Text>
);
}
}