1 import React from 'react'
2 import styles from './AmountUnitField.scss'
3 import { DropdownButton, MenuItem } from 'react-bootstrap'
4 import { converIntToDec, addZeroToDecimalPosition, formatBTMAmount, parseBTMAmount } from 'utility/buildInOutDisplay'
5 import { FieldLabel } from 'features/shared/components'
6 import pick from 'lodash/pick'
8 const BTMAmountUnit = ['BTM', 'mBTM', 'NEU']
10 const TEXT_FIELD_PROPS = [
18 class AmountUnitField extends React.Component {
26 value: props.fieldProps.value,
29 this.select = this.select.bind(this)
30 this.toggleDropwdown = this.toggleDropwdown.bind(this)
31 this.closeDropdown = this.closeDropdown.bind(this)
32 this.handleChange = this.handleChange.bind(this)
33 this.handleBlur = this.handleBlur.bind(this)
38 this.setState({ showDropdown: !this.state.showDropdown })
42 this.setState({ showDropdown: false })
46 this.setState({ selected: value })
47 const amount = this.props.fieldProps.value
50 this.setState({pos: 8})
51 this.setState({value: converIntToDec(amount, 8)})
54 this.setState({pos: 5})
55 this.setState({value: converIntToDec(amount, 5)})
58 this.setState({pos: 0})
59 this.setState({value: amount})
66 const value = event.target.value
67 this.setState({ value: addZeroToDecimalPosition( value, this.state.pos ) })
68 if (this.props.fieldProps.onBlur) {
69 // Swallow the event to prevent Redux Form from
70 // extracting the form value
71 this.props.fieldProps.onBlur()
76 const value = event.target.value
77 // Update the internal state to trigger a re-render
78 // using the formatted value
79 this.setState({ value: value })
80 if (this.props.fieldProps.onChange) {
81 // Notify the normalized value
82 this.props.fieldProps.onChange(
83 parseBTMAmount(value, this.state.pos )
89 const fieldProps = pick(this.props.fieldProps, TEXT_FIELD_PROPS)
90 const {touched, error} = this.props.fieldProps
93 <div className='form-group'>
94 {this.props.title && <FieldLabel>{this.props.title}</FieldLabel>}
95 <div className='input-group'>
96 {<input className='form-control'
97 type={this.state.type}
99 value={formatBTMAmount(this.state.value, this.state.pos)}
100 placeholder={this.props.placeholder}
101 autoFocus={!!this.props.autoFocus}
102 onBlur={this.handleBlur}
103 onChange={this.handleChange}
107 <div className={`input-group-btn ${this.state.showDropdown && 'open'}`}>
109 className={styles.dropdownButton}
110 id='input-dropdown-addon'
111 title={this.state.selected}
112 onSelect={this.select}
114 {BTMAmountUnit.map((option) =>
115 <MenuItem eventKey={option}>{option}</MenuItem>
121 {touched && error && <span className='text-danger'><strong>{error}</strong></span>}
122 {this.props.hint && <span className='help-block'>{this.props.hint}</span>}
128 export default AmountUnitField