2017-08-25 18:18:52 +02:00
|
|
|
const Component = require('react').Component
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
2017-11-15 19:30:35 +01:00
|
|
|
const CurrencyInput = require('./currency-input')
|
2017-10-26 18:06:34 +02:00
|
|
|
const {
|
|
|
|
addCurrencies,
|
|
|
|
conversionGTE,
|
|
|
|
conversionLTE,
|
2017-10-30 22:46:56 +01:00
|
|
|
subtractCurrencies,
|
2017-10-26 18:06:34 +02:00
|
|
|
} = require('../conversion-util')
|
2017-08-25 18:18:52 +02:00
|
|
|
|
|
|
|
module.exports = InputNumber
|
|
|
|
|
|
|
|
inherits(InputNumber, Component)
|
|
|
|
function InputNumber () {
|
|
|
|
Component.call(this)
|
|
|
|
|
2017-08-29 16:50:48 +02:00
|
|
|
this.setValue = this.setValue.bind(this)
|
2017-08-25 18:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 22:46:56 +01:00
|
|
|
function isValidInput (text) {
|
|
|
|
const re = /^([1-9]\d*|0)(\.|\.\d*)?$/
|
|
|
|
return re.test(text)
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:18:52 +02:00
|
|
|
InputNumber.prototype.setValue = function (newValue) {
|
2017-10-30 22:46:56 +01:00
|
|
|
if (newValue && !isValidInput(newValue)) return
|
2017-10-13 22:19:22 +02:00
|
|
|
const { fixed, min = -1, max = Infinity, onChange } = this.props
|
2017-08-25 18:18:52 +02:00
|
|
|
|
2017-10-30 22:46:56 +01:00
|
|
|
newValue = fixed ? newValue.toFixed(4) : newValue
|
2017-08-25 18:18:52 +02:00
|
|
|
|
2017-10-26 18:06:34 +02:00
|
|
|
const newValueGreaterThanMin = conversionGTE(
|
2017-10-30 22:46:56 +01:00
|
|
|
{ value: newValue || '0', fromNumericBase: 'dec' },
|
2017-10-26 18:06:34 +02:00
|
|
|
{ value: min, fromNumericBase: 'hex' },
|
|
|
|
)
|
|
|
|
|
|
|
|
const newValueLessThanMax = conversionLTE(
|
2017-10-30 22:46:56 +01:00
|
|
|
{ value: newValue || '0', fromNumericBase: 'dec' },
|
2017-10-26 18:06:34 +02:00
|
|
|
{ value: max, fromNumericBase: 'hex' },
|
|
|
|
)
|
|
|
|
if (newValueGreaterThanMin && newValueLessThanMax) {
|
2017-08-25 18:18:52 +02:00
|
|
|
onChange(newValue)
|
2017-10-26 18:06:34 +02:00
|
|
|
} else if (!newValueGreaterThanMin) {
|
|
|
|
onChange(min)
|
|
|
|
} else if (!newValueLessThanMax) {
|
|
|
|
onChange(max)
|
2017-08-25 18:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InputNumber.prototype.render = function () {
|
2017-10-13 22:19:22 +02:00
|
|
|
const { unitLabel, step = 1, placeholder, value = 0 } = this.props
|
2017-08-29 16:50:48 +02:00
|
|
|
|
2017-08-25 18:18:52 +02:00
|
|
|
return h('div.customize-gas-input-wrapper', {}, [
|
2017-11-15 19:30:35 +01:00
|
|
|
h(CurrencyInput, {
|
|
|
|
className: 'customize-gas-input',
|
2017-10-26 18:06:34 +02:00
|
|
|
value,
|
2017-11-15 19:30:35 +01:00
|
|
|
placeholder,
|
|
|
|
onInputChange: newValue => {
|
|
|
|
this.setValue(newValue)
|
|
|
|
},
|
2017-08-25 18:18:52 +02:00
|
|
|
}),
|
|
|
|
h('span.gas-tooltip-input-detail', {}, [unitLabel]),
|
|
|
|
h('div.gas-tooltip-input-arrows', {}, [
|
|
|
|
h('i.fa.fa-angle-up', {
|
2017-10-13 22:19:22 +02:00
|
|
|
onClick: () => this.setValue(addCurrencies(value, step)),
|
2017-08-25 18:18:52 +02:00
|
|
|
}),
|
|
|
|
h('i.fa.fa-angle-down', {
|
2017-09-11 09:45:06 +02:00
|
|
|
style: { cursor: 'pointer' },
|
2017-10-30 22:46:56 +01:00
|
|
|
onClick: () => this.setValue(subtractCurrencies(value, step)),
|
2017-08-25 18:18:52 +02:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
}
|