aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/send/currency-display.js
blob: e0147012ffcf18430d9b6207653d5c53344f7bd8 (plain) (blame)
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
85
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const Identicon = require('../identicon')
const AutosizeInput = require('react-input-autosize').default
const { conversionUtil } = require('../../conversion-util')

module.exports = CurrencyDisplay

inherits(CurrencyDisplay, Component)
function CurrencyDisplay () {
  Component.call(this)

  this.state = {
    minWidth: null,
  }
}

function isValidNumber (text) {
  const re = /^([1-9]\d*|0)(\.|\.\d*)?$/
  return re.test(text)
}

CurrencyDisplay.prototype.componentDidMount = function () {
  this.setState({ minWidth: this.refs.currencyDisplayInput.sizer.scrollWidth + 10 })
}

CurrencyDisplay.prototype.render = function () {
  const {
    className,
    primaryCurrency,
    convertedCurrency,
    value = '',
    placeholder = '0',
    conversionRate,
    convertedPrefix = '',
    readOnly = false,
    handleChange,
    inputFontSize,
  } = this.props
  const { minWidth } = this.state

  const convertedValue = conversionUtil(value, {
    fromNumericBase: 'dec',
    fromCurrency: primaryCurrency,
    toCurrency: convertedCurrency,
    conversionRate,
  })

  return h('div.currency-display', {
    className,
  }, [

    h('div.currency-display__primary-row', [

      h(AutosizeInput, {
        ref: 'currencyDisplayInput',
        className: 'currency-display__input-wrapper',
        inputClassName: 'currency-display__input',
        value,
        placeholder,
        readOnly,
        minWidth,
        onChange: (event) => {
          const newValue = event.target.value
          if (newValue && !isValidNumber(newValue)) {
            event.preventDefault()
          }
          else {
            handleChange(newValue)
          }
        },
        style: { fontSize: inputFontSize },
      }),

      h('span.currency-display__primary-currency', {}, primaryCurrency),

    ]),

    h('div.currency-display__converted-value', {}, `${convertedPrefix}${convertedValue} ${convertedCurrency}`),

  ])
    
}