Browse Source

Prevent negative values and Infinity to be inputable

master
Gaëtan Renaudeau 7 years ago
parent
commit
ff0cb8d9b3
  1. 11
      src/components/base/InputCurrency/index.js

11
src/components/base/InputCurrency/index.js

@ -14,10 +14,6 @@ import Select from 'components/base/LegacySelect'
import type { Unit } from '@ledgerhq/live-common/lib/types'
function parseValue(value) {
return value.toString().replace(/,/g, '.')
}
function format(unit: Unit, value: number, { isFocused, showAllDigits, subMagnitude }) {
// FIXME do we need locale for the input too ?
return formatCurrencyUnit(unit, value, {
@ -104,7 +100,9 @@ class InputCurrency extends PureComponent<Props, State> {
}
handleChange = (v: string) => {
v = parseValue(v)
// FIXME this is to refactor. this is hacky and don't cover everything..
v = v.toString().replace(/,/g, '.')
// allow to type directly `.` in input to have `0.`
if (v.startsWith('.')) {
@ -120,7 +118,8 @@ class InputCurrency extends PureComponent<Props, State> {
}
// Check if value is valid Number
if (isNaN(Number(v))) {
const asNumber = parseFloat(v)
if (isNaN(asNumber) || !isFinite(asNumber) || asNumber < 0) {
return
}

Loading…
Cancel
Save