UNPKG

admin-on-rest-fr05t1k

Version:

A frontend Framework for building admin applications on top of REST services, using ES6, React and Material UI

65 lines (58 loc) 2.23 kB
import React, { PropTypes } from 'react'; import get from 'lodash.get'; import pure from 'recompose/pure'; const hasNumberFormat = !!(typeof Intl === 'object' && Intl && typeof Intl.NumberFormat === 'function'); /** * 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 * <NumberField source="score" /> * // renders the record { id: 1234, score: 567 } as * <span>567</span> * * <NumberField source="score" elStyle={{ color: 'red' }} /> * // renders the record { id: 1234, score: 567 } as * <span style="color:red;">567</span> * * <NumberField source="share" options={{ style: 'percent' }} /> * // renders the record { id: 1234, share: 0.2545 } as * <span>25%</span> * * <NumberField source="price" options={{ style: 'currency', currency: 'USD' }} /> * // renders the record { id: 1234, price: 25.99 } as * <span>$25.99</span> * * <NumberField source="price" locales="fr-FR" options={{ style: 'currency', currency: 'USD' }} /> * // renders the record { id: 1234, price: 25.99 } as * <span>25,99 $US</span> */ export const NumberField = ({ record, source, locales, options, elStyle }) => { if (!record) return null; const value = get(record, source); if (value == null) return null; if (!hasNumberFormat) return <span style={elStyle}>{value}</span>; return <span style={elStyle}>{value.toLocaleString(locales, options)}</span>; }; NumberField.propTypes = { addLabel: PropTypes.bool, elStyle: PropTypes.object, label: PropTypes.string, locales: PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string), ]), options: PropTypes.object, record: PropTypes.object, source: PropTypes.string.isRequired, }; const PureNumberField = pure(NumberField); PureNumberField.defaultProps = { addLabel: true, style: { textAlign: 'right' }, headerStyle: { textAlign: 'right' }, }; export default PureNumberField;