Optimize formatted_time.js (#1503)

* Optimize formatted_time.js

* trailing commas

* export default class
This commit is contained in:
Michał Odziemczyk 2018-03-14 20:58:24 +01:00 committed by Harrison Healey
parent 3a3be22af0
commit d827192d72

View file

@ -3,28 +3,35 @@
import React from 'react';
import PropTypes from 'prop-types';
import {injectIntl, intlShape} from 'react-intl';
import {Text} from 'react-native';
class FormattedTime extends React.PureComponent {
export default class FormattedTime extends React.PureComponent {
static propTypes = {
intl: intlShape.isRequired,
value: PropTypes.any.isRequired,
format: PropTypes.string,
children: PropTypes.func,
hour12: PropTypes.bool,
};
render() {
const {
intl,
value,
children,
...props
hour12,
} = this.props;
Reflect.deleteProperty(props, 'format');
const date = new Date(value);
const militaryTime = !hour12;
const formattedTime = intl.formatDate(value, {...props, hour: 'numeric', minute: 'numeric'});
const hour = militaryTime ? date.getHours() : (date.getHours() % 12 || 12);
let minute = date.getMinutes();
minute = minute >= 10 ? minute : ('0' + minute);
let time = '';
if (!militaryTime) {
time = (date.getHours() >= 12 ? ' PM' : ' AM');
}
const formattedTime = hour + ':' + minute + time;
if (typeof children === 'function') {
return children(formattedTime);
@ -33,5 +40,3 @@ class FormattedTime extends React.PureComponent {
return <Text>{formattedTime}</Text>;
}
}
export default injectIntl(FormattedTime);