import React, { SFC } from 'react'; import PropTypes from 'prop-types'; import get from 'lodash/get'; import pure from 'recompose/pure'; import Typography, { TypographyProps } from '@material-ui/core/Typography'; import sanitizeRestProps from './sanitizeRestProps'; import { FieldProps, InjectedFieldProps, fieldPropTypes } from './types'; const toLocaleStringSupportsLocales = (() => { // from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString try { new Date().toLocaleString('i'); } catch (error) { return error instanceof RangeError; } return false; })(); interface Props extends FieldProps { locales?: string | string[]; options?: object; showTime?: boolean; } /** * Display a date value as a locale string. * * Uses Intl.DateTimeFormat() if available, passing the locales and options props as arguments. * If Intl is not available, it outputs date as is (and ignores the locales and options props). * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString * @example * * // renders the record { id: 1234, published_at: new Date('2012-11-07') } as * 07/11/2012 * * * // renders the record { id: 1234, new Date('2012-11-07') } as * 07/11/2012 * * * // renders the record { id: 1234, new Date('2012-11-07') } as * Wednesday, November 7, 2012 * * * // renders the record { id: 1234, new Date('2012-11-07') } as * mercredi 7 novembre 2012 */ export const DateField: SFC = ({ className, locales, options, record, showTime = false, source, ...rest }) => { if (!record) { return null; } const value = get(record, source); if (value == null) { return null; } const date = value instanceof Date ? value : new Date(value); const dateString = showTime ? toLocaleStringSupportsLocales ? date.toLocaleString(locales, options) : date.toLocaleString() : toLocaleStringSupportsLocales ? date.toLocaleDateString(locales, options) : date.toLocaleDateString(); return ( {dateString} ); }; const EnhancedDateField = pure(DateField); EnhancedDateField.defaultProps = { addLabel: true, }; EnhancedDateField.propTypes = { ...Typography.propTypes, ...fieldPropTypes, locales: PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string), ]), options: PropTypes.object, showTime: PropTypes.bool, }; EnhancedDateField.displayName = 'EnhancedDateField'; export default EnhancedDateField;