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 hasNumberFormat = !!(
typeof Intl === 'object' &&
Intl &&
typeof Intl.NumberFormat === 'function'
);
interface Props extends FieldProps {
locales?: string | string[];
options?: object;
}
/**
* Display a numeric value as a locale string.
*
* Uses Intl.NumberFormat() if available, passing the locales and options props as arguments.
* If Intl is not available, it outputs number as is (and ignores the locales and options props).
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
* @example
*
* // renders the record { id: 1234, score: 567 } as
* 567
*
*
* // renders the record { id: 1234, score: 567 } as
* 567
*
*
* // renders the record { id: 1234, share: 0.2545 } as
* 25%
*
*
* // renders the record { id: 1234, price: 25.99 } as
* $25.99
*
*
* // renders the record { id: 1234, price: 25.99 } as
* 25,99 $US
*/
export const NumberField: SFC = ({
className,
record,
source,
locales,
options,
textAlign,
...rest
}) => {
if (!record) {
return null;
}
const value = get(record, source);
if (value == null) {
return null;
}
if (!hasNumberFormat) {
return (
{value}
);
}
return (
{value.toLocaleString(locales, options)}
);
};
// wat? TypeScript looses the displayName if we don't set it explicitly
NumberField.displayName = 'NumberField';
const EnhancedNumberField = pure(NumberField);
EnhancedNumberField.defaultProps = {
addLabel: true,
textAlign: 'right',
};
EnhancedNumberField.propTypes = {
...Typography.propTypes,
...fieldPropTypes,
locales: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
]),
options: PropTypes.object,
};
EnhancedNumberField.displayName = 'EnhancedNumberField';
export default EnhancedNumberField;