1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/app/components/send/currency-display.js

131 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-10-09 18:25:23 +02:00
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const Identicon = require('../identicon')
2017-10-21 03:57:59 +02:00
const { conversionUtil, multiplyCurrencies } = require('../../conversion-util')
2017-10-09 18:25:23 +02:00
module.exports = CurrencyDisplay
inherits(CurrencyDisplay, Component)
function CurrencyDisplay () {
Component.call(this)
this.state = {
value: null,
2017-10-09 18:25:23 +02:00
}
}
function isValidInput (text) {
2017-10-09 18:25:23 +02:00
const re = /^([1-9]\d*|0)(\.|\.\d*)?$/
return re.test(text)
}
2017-10-17 22:13:20 +02:00
function toHexWei (value) {
return conversionUtil(value, {
fromNumericBase: 'dec',
toNumericBase: 'hex',
toDenomination: 'WEI',
})
}
2017-10-21 03:26:18 +02:00
CurrencyDisplay.prototype.getAmount = function (value) {
const { selectedToken } = this.props
const { decimals } = selectedToken || {}
const multiplier = Math.pow(10, Number(decimals || 0))
2017-10-21 03:57:59 +02:00
const sendAmount = multiplyCurrencies(value, multiplier, {toNumericBase: 'hex'})
2017-10-21 03:26:18 +02:00
return selectedToken
? sendAmount
: toHexWei(value)
}
2017-10-09 18:25:23 +02:00
CurrencyDisplay.prototype.render = function () {
const {
className = 'currency-display',
primaryBalanceClassName = 'currency-display__input',
convertedBalanceClassName = 'currency-display__converted-value',
conversionRate,
2017-10-09 18:25:23 +02:00
primaryCurrency,
convertedCurrency,
convertedPrefix = '',
placeholder = '0',
2017-10-09 18:25:23 +02:00
readOnly = false,
2017-10-17 22:13:20 +02:00
inError = false,
value: initValue,
2017-10-17 22:13:20 +02:00
handleChange,
validate,
2017-10-09 18:25:23 +02:00
} = this.props
const { value } = this.state
const initValueToRender = conversionUtil(initValue, {
fromNumericBase: 'hex',
toNumericBase: 'dec',
fromDenomination: 'WEI',
numberOfDecimals: 6,
conversionRate,
})
2017-10-09 18:25:23 +02:00
const convertedValue = conversionUtil(value || initValueToRender, {
2017-10-09 18:25:23 +02:00
fromNumericBase: 'dec',
fromCurrency: primaryCurrency,
toCurrency: convertedCurrency,
numberOfDecimals: 2,
2017-10-09 18:25:23 +02:00
conversionRate,
})
const inputSizeMultiplier = readOnly ? 1 : 1.2;
return h('div', {
2017-10-09 18:25:23 +02:00
className,
2017-10-17 22:13:20 +02:00
style: {
borderColor: inError ? 'red' : null,
},
2017-10-09 18:25:23 +02:00
}, [
h('div.currency-display__primary-row', [
h('div.currency-display__input-wrapper', [
h('input', {
className: primaryBalanceClassName,
value: `${value || initValueToRender}`,
placeholder: '0',
size: (value || initValueToRender).length * inputSizeMultiplier,
readOnly,
onChange: (event) => {
let newValue = event.target.value
if (newValue === '') {
newValue = '0'
}
else if (newValue.match(/^0[1-9]$/)) {
newValue = newValue.match(/[1-9]/)[0]
}
if (newValue && !isValidInput(newValue)) {
event.preventDefault()
}
else {
validate(this.getAmount(newValue))
this.setState({ value: newValue })
}
},
onBlur: event => !readOnly && handleChange(this.getAmount(event.target.value)),
}),
h('span.currency-display__currency-symbol', primaryCurrency),
]),
2017-10-09 18:25:23 +02:00
]),
h('div', {
className: convertedBalanceClassName,
}, `${convertedValue} ${convertedCurrency.toUpperCase()}`),
2017-10-09 18:25:23 +02:00
])
}