1 import React from 'react'
2 import { parseBTMAmount, formatBTMAmount, addZeroToDecimalPosition } from 'utility/buildInOutDisplay'
3 import { FieldLabel } from 'features/shared/components'
4 import pick from 'lodash/pick'
7 const TEXT_FIELD_PROPS = [
15 class AmountInputMask extends React.Component {
20 value: props.fieldProps.value,
23 this.handleChange = this.handleChange.bind(this)
24 this.handleBlur = this.handleBlur.bind(this)
29 const value = event.target.value
30 this.setState({ value: addZeroToDecimalPosition( value, this.props.decimal ) })
31 if (this.props.fieldProps.onBlur) {
32 // Swallow the event to prevent Redux Form from
33 // extracting the form value
34 this.props.fieldProps.onBlur()
39 const value = event.target.value
40 // Update the internal state to trigger a re-render
41 // using the formatted value
42 this.setState({ value: value })
43 if (this.props.fieldProps.onChange) {
44 // Notify the normalized value
45 this.props.fieldProps.onChange(
46 parseBTMAmount(value, this.props.decimal )
52 const fieldProps = pick(this.props.fieldProps, TEXT_FIELD_PROPS)
53 const {touched, error} = this.props.fieldProps
56 <div className='form-group'>
57 {this.props.title && <FieldLabel>{this.props.title}</FieldLabel>}
58 <input className='form-control'
61 value={formatBTMAmount(this.state.value, this.props.decimal)}
62 placeholder={this.props.placeholder}
63 autoFocus={!!this.props.autoFocus}
64 onBlur={this.handleBlur}
65 onChange={this.handleChange}
67 {touched && error && <span className='text-danger'><strong>{error}</strong></span>}
68 {this.props.hint && <span className='help-block'>{this.props.hint}</span>}
74 export default AmountInputMask