# All files / components/Money Money.js

0% Statements 0/4
0% Branches 0/4
0% Functions 0/2
0% Lines 0/4

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56

     import React from 'react';
    import PropTypes from 'prop-types';
    import formatMoney from '../../extensions/formatMoney';
    import MoneyStyles from './styles/MoneyStyles';

    class Money extends React.Component {
      static propTypes = {
        /**
         * The monetary value to show.
         */
        value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
        /**
         * Whether or not to show a +/- sign before the amount (defaults true)
         */
        showSign: PropTypes.bool,
        /**
         * Adds a class name to the element.
         */
        className: PropTypes.string,
        /**
         * Adds an id to the element.
         */
        id: PropTypes.string,
        /**
         * A component to render the text itself. Will be passed a `value` Number prop.
         */
        Styles: PropTypes.func,
      };

      static defaultProps = {
        showSign: true,
        className: null,
        id: null,
        value: 0,
        Styles: MoneyStyles,
      };

      getSign() {
        const { value } = this.props;
        return value >= 0 ? '+' : '-';
      }

      render() {
        const { value, showSign, id, className, Styles } = this.props;
        return (
          <Styles value={value} id={id} className={className}>
            {showSign ? this.getSign(value) : null}
            {/* sign is already present, so remove it from the formatted number */}
            ${formatMoney(value).replace('-', '')}
          </Styles>
        );
      }
    }

    export default Money;