<commit_before>import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {InputGroup, FormControl, DropdownButton, MenuItem} from 'react-bootstrap'

class EtherAmount extends Component {

    constructor(props) {
        super(props)
        this.state = {
            unit: props.unit,
        }

        this.onUnitSelected = this.onUnitSelected.bind(this)
        this.handleInputChange = this.handleInputChange.bind(this)
    }

    toWei(value) {
        return window.web3.toBigNumber(window.web3.toWei(value, this.state.unit))
    }

    fromWei(wei) {
        return window.web3.fromWei(window.web3.toBigNumber(wei), this.state.unit).toFixed()
    }

    handleInputChange(event) {
        const target = event.target
        const value = target.value
        this.props.onChange(this.toWei(value))
    }

    onUnitSelected(key, event) {
        this.setState({unit: key})
    }

    render() {
        return <InputGroup>
            <FormControl
                type="number"
                value={this.fromWei(this.props.wei)}
                onChange={this.handleInputChange}
            />
            <DropdownButton
                componentClass={InputGroup.Button}
                id="input-dropdown-addon"
                title={this.state.unit}
                onSelect={this.onUnitSelected}
            >
                <MenuItem eventKey='wei'    active={(this.state.unit === 'wei')}>wei</MenuItem>
                <MenuItem eventKey='kwei'   active={(this.state.unit === 'kwei')}>kwei</MenuItem>
                <MenuItem eventKey='mwei'   active={(this.state.unit === 'mwei')}>mwei</MenuItem>
                <MenuItem eventKey='gwei'   active={(this.state.unit === 'gwei')}>gwei</MenuItem>
                <MenuItem eventKey='szabo'  active={(this.state.unit === 'szabo')}>szabo</MenuItem>
                <MenuItem eventKey='finney'    active={(this.state.unit === 'finney')}>finney</MenuItem>
                <MenuItem eventKey='ether'   active={(this.state.unit === 'ether')}>ether</MenuItem>
                <MenuItem eventKey='kether'   active={(this.state.unit === 'kether')}>kether</MenuItem>
            </DropdownButton>
        </InputGroup>
    }
}

// static units
EtherAmount.units = [
    'wei',
    'kwei',<commit_msg>StandingOrderDapp-65 Can't enter payment amounts with zeros like 1.005<commit_after>import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {InputGroup, FormControl, DropdownButton, MenuItem} from 'react-bootstrap'

class EtherAmount extends Component {

    constructor(props) {
        super(props)
        this.state = {
            unit: props.unit,
        }
        this.lastRepresentation = ''

        this.onUnitSelected = this.onUnitSelected.bind(this)
        this.handleInputChange = this.handleInputChange.bind(this)
    }

    toWei(value) {
        return window.web3.toBigNumber(window.web3.toWei(value, this.state.unit))
    }

    fromWei(wei) {
        return window.web3.fromWei(window.web3.toBigNumber(wei), this.state.unit).toFixed()
    }

    handleInputChange(event) {
        const target = event.target
        const value = target.value
        this.lastRepresentation = value
        this.props.onChange(this.toWei(value))
    }

    onUnitSelected(key, event) {
        this.lastRepresentation = '' // reset lastRepresentation to clear leading/trailing zeros
        this.setState({
            unit: key,
        })
    }

    render() {
        const {wei} = this.props
        // keep the string value entered by the user as long as it results in the same number of WEI. This enables entering
        // values with leading/trailing zeros like 0.005. Otherwise user would not be able to enter a zero after the decimal
        // point, as "0.0" would always be converted to plain "0" in the OnChange handler.
        // See https://github.com/TripleSpeeder/StandingOrderDapp/issues/65
        const oldwei = this.toWei(this.lastRepresentation)
        const value = (oldwei.equals(wei)) ? this.lastRepresentation : this.fromWei(wei)

        return <InputGroup>
            <FormControl
                type="number"
                value={value}
                onChange={this.handleInputChange}
            />
            <DropdownButton
                componentClass={InputGroup.Button}
                id="input-dropdown-addon"
                title={this.state.unit}
                onSelect={this.onUnitSelected}
            >
                <MenuItem eventKey='wei'    active={(this.state.unit === 'wei')}>wei</MenuItem>
                <MenuItem eventKey='kwei'   active={(this.state.unit === 'kwei')}>kwei</MenuItem>
                <MenuItem eventKey='mwei'   active={(this.state.unit === 'mwei')}>mwei</MenuItem>
                <MenuItem eventKey='gwei'   active={(this.state.unit === 'gwei')}>gwei</MenuItem>
                <MenuItem eventKey='szabo'  active={(this.state.unit === 'szabo')}>szabo</MenuItem>
                <MenuItem eventKey='finney'    active={(this.state.unit === 'finney')}>finney</MenuItem>
                <MenuItem eventKey='ether'   active={(this.state.unit === 'ether')}>ether</MenuItem>
                <MenuItem eventKey='kether'   active={(this.state.unit === 'kether')}>kether</MenuItem>
            </DropdownButton>
        </InputGroup>
    }
}

// static units
EtherAmount.units = [
    'wei',
    'kwei',
{'fraction_non_alphanumeric': 0.13192514359829535, 'fraction_numerical': 0.0042616268297202145, 'mean_word_length': 2.2974954184483813, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6674531', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1554, 'n_words': 286}