All files / source/input_money input_money.js

100% Statements 15/15
33.33% Branches 2/6
80% Functions 4/5
100% Lines 15/15
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                  7x     7x           1x     1x 1x 1x       1x 1x     1x           1x     1x           7x     7x                     24x               24x              
// Dependencies.
import React from 'react'
import PropTypes from 'prop-types'
 
// Utility methods.
import {
  bind,
  formatNumber
} from '@t7/utils'
 
// UI components.
import { Input } from '../'
 
// Define class.
class InputMoney extends React.Component {
  constructor (props) {
    // Pass `props` into scope.
    super(props)
 
    // Bind context.
    bind(this)
  }
 
  // Get API value.
  getApiValue (x = '') {
    // Props.
    const { countryCode } = this.props
 
    // Germany?
    Eif (countryCode === 'DE') {
      x = String(x).replace(/[^0-9,]/g, '')
      x = x.replace(/,([^,]*)$/, '.' + '$1')
    }
 
    // To number.
    x = formatNumber(x)
    x = parseFloat(x) || 0
 
    // Expose number.
    return x
  }
 
  // Change event.
  handleChange (e, value = '') {
    // Convert to number.
    const apiValue = this.getApiValue(value)
 
    // Callback.
    this.props.handleChange(e, value, apiValue)
  }
 
  // Render method.
  render () {
    // Events.
    const { handleChange } = this
 
    // Expose UI.
    return (
      <Input
        {...this.props}
        maxlength='20'
        handleChange={handleChange}
      />
    )
  }
}
 
// Validation.
InputMoney.propTypes = {
  countryCode: PropTypes.string,
 
  // Events.
  handleChange: PropTypes.func
}
 
// Defaults.
InputMoney.defaultProps = {
  // Events.
  handleChange: () => {}
}
 
// Export.
export default InputMoney