2017-11-08 00:36:26 +01:00
|
|
|
const Component = require('react').Component
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
|
|
|
|
|
|
|
module.exports = CurrencyInput
|
|
|
|
|
|
|
|
inherits(CurrencyInput, Component)
|
|
|
|
function CurrencyInput (props) {
|
|
|
|
Component.call(this)
|
|
|
|
|
2018-03-16 16:32:07 +01:00
|
|
|
const sanitizedValue = sanitizeValue(props.value)
|
|
|
|
|
2017-11-08 00:36:26 +01:00
|
|
|
this.state = {
|
2018-03-16 16:32:07 +01:00
|
|
|
value: sanitizedValue,
|
|
|
|
emptyState: false,
|
|
|
|
focused: false,
|
2017-11-08 00:36:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeNonDigits (str) {
|
|
|
|
return str.match(/\d|$/g).join('')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes characters that are not digits, then removes leading zeros
|
|
|
|
function sanitizeInteger (val) {
|
|
|
|
return String(parseInt(removeNonDigits(val) || '0', 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitizeDecimal (val) {
|
|
|
|
return removeNonDigits(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take a single string param and returns a non-negative integer or float as a string.
|
|
|
|
// Breaks the input into three parts: the integer, the decimal point, and the decimal/fractional part.
|
|
|
|
// Removes leading zeros from the integer, and non-digits from the integer and decimal
|
|
|
|
// The integer is returned as '0' in cases where it would be empty. A decimal point is
|
|
|
|
// included in the returned string if one is included in the param
|
|
|
|
// Examples:
|
|
|
|
// sanitizeValue('0') -> '0'
|
|
|
|
// sanitizeValue('a') -> '0'
|
|
|
|
// sanitizeValue('010.') -> '10.'
|
|
|
|
// sanitizeValue('0.005') -> '0.005'
|
|
|
|
// sanitizeValue('22.200') -> '22.200'
|
|
|
|
// sanitizeValue('.200') -> '0.200'
|
|
|
|
// sanitizeValue('a.b.1.c,89.123') -> '0.189123'
|
|
|
|
function sanitizeValue (value) {
|
2017-11-08 20:18:27 +01:00
|
|
|
let [ , integer, point, decimal] = (/([^.]*)([.]?)([^.]*)/).exec(value)
|
2017-11-08 00:36:26 +01:00
|
|
|
|
|
|
|
integer = sanitizeInteger(integer) || '0'
|
|
|
|
decimal = sanitizeDecimal(decimal)
|
|
|
|
|
|
|
|
return `${integer}${point}${decimal}`
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrencyInput.prototype.handleChange = function (newValue) {
|
|
|
|
const { onInputChange } = this.props
|
2018-01-15 23:37:59 +01:00
|
|
|
const { value } = this.state
|
2017-11-08 00:36:26 +01:00
|
|
|
|
2018-01-15 23:37:59 +01:00
|
|
|
let parsedValue = newValue
|
|
|
|
const newValueLastIndex = newValue.length - 1
|
2017-11-08 00:36:26 +01:00
|
|
|
|
2018-01-15 23:37:59 +01:00
|
|
|
if (value === '0' && newValue[newValueLastIndex] === '0') {
|
|
|
|
parsedValue = parsedValue.slice(0, newValueLastIndex)
|
|
|
|
}
|
|
|
|
const sanitizedValue = sanitizeValue(parsedValue)
|
2018-03-16 16:32:07 +01:00
|
|
|
this.setState({
|
|
|
|
value: sanitizedValue,
|
|
|
|
emptyState: newValue === '' && sanitizedValue === '0',
|
|
|
|
})
|
2018-01-15 23:37:59 +01:00
|
|
|
onInputChange(sanitizedValue)
|
2017-11-08 00:36:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If state.value === props.value plus a decimal point, or at least one
|
|
|
|
// zero or a decimal point and at least one zero, then this returns state.value
|
|
|
|
// after it is sanitized with getValueParts
|
|
|
|
CurrencyInput.prototype.getValueToRender = function () {
|
|
|
|
const { value } = this.props
|
|
|
|
const { value: stateValue } = this.state
|
|
|
|
|
|
|
|
const trailingStateString = (new RegExp(`^${value}(.+)`)).exec(stateValue)
|
|
|
|
const trailingDecimalAndZeroes = trailingStateString && (/^[.0]0*/).test(trailingStateString[1])
|
|
|
|
|
|
|
|
return sanitizeValue(trailingDecimalAndZeroes
|
|
|
|
? stateValue
|
|
|
|
: value)
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrencyInput.prototype.render = function () {
|
|
|
|
const {
|
|
|
|
className,
|
|
|
|
placeholder,
|
|
|
|
readOnly,
|
2017-11-10 19:47:02 +01:00
|
|
|
inputRef,
|
2018-03-20 23:50:30 +01:00
|
|
|
type,
|
2017-11-08 00:36:26 +01:00
|
|
|
} = this.props
|
2018-03-16 16:32:07 +01:00
|
|
|
const { emptyState, focused } = this.state
|
2017-11-08 00:36:26 +01:00
|
|
|
|
|
|
|
const inputSizeMultiplier = readOnly ? 1 : 1.2
|
|
|
|
|
|
|
|
const valueToRender = this.getValueToRender()
|
|
|
|
return h('input', {
|
|
|
|
className,
|
2018-03-20 23:50:30 +01:00
|
|
|
type,
|
2018-03-16 16:32:07 +01:00
|
|
|
value: emptyState ? '' : valueToRender,
|
|
|
|
placeholder: focused ? '' : placeholder,
|
2017-11-08 00:36:26 +01:00
|
|
|
size: valueToRender.length * inputSizeMultiplier,
|
|
|
|
readOnly,
|
2018-03-16 16:32:07 +01:00
|
|
|
onFocus: () => this.setState({ focused: true, emptyState: valueToRender === '0' }),
|
|
|
|
onBlur: () => this.setState({ focused: false, emptyState: false }),
|
2017-11-08 00:36:26 +01:00
|
|
|
onChange: e => this.handleChange(e.target.value),
|
2018-01-15 23:37:59 +01:00
|
|
|
ref: inputRef,
|
2017-11-08 00:36:26 +01:00
|
|
|
})
|
|
|
|
}
|