Optimize formatted_time.js (#1503)
* Optimize formatted_time.js * trailing commas * export default class
This commit is contained in:
parent
3a3be22af0
commit
d827192d72
1 changed files with 15 additions and 10 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue