All files ReactAmountField.jsx

100% Statements 26/26
85.71% Branches 12/14
100% Functions 4/4
100% Lines 26/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61            26x 26x     26x 1x 25x 22x         132x 18x 18x 18x 15x 15x 15x 15x 15x 15x     3x 3x             44x 44x 44x 44x 44x 44x 44x       2x                        
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
 
class ReactAmountField extends PureComponent {
 
  constructor(props) {
    super(props);
    this.state = {
      unflushedValue: '',
    };
    if (this.props.value === 0) {
      this.state.unflushedValue = 0;
    } else if (this.props.value && this.props.value !== '') {
      this.state.unflushedValue = this.props.value / 100;
    }
  }
 
  proxyEvent(target) {
    return (event) => {
      Eif (target) {
        const eventValue = event.target.value;
        if (eventValue && eventValue !== '') {
          this.setState({ unflushedValue: eventValue.toString() });
          Eif (!eventValue.toString().endsWith('.')) {
            const oldValue = eventValue.toString().replace(/[^\d.,]/g, '').replace(',', '.');
            const newEvent = event;
            newEvent.target.value = parseInt(oldValue * 100, 10).toString();
            target(newEvent);
          }
        } else {
          this.setState({ unflushedValue: '' });
          target(event);
        }
      }
    };
  }
 
  render() {
    const props = { ...this.props };
    props.value = this.state.unflushedValue;
    props.onChange = this.proxyEvent(this.props.onChange);
    props.onBlur = this.proxyEvent(this.props.onBlur);
    props.onDrop = this.proxyEvent(this.props.onDrop);
    delete props.children;
    return <div>{React.cloneElement(this.props.children, { ...props })}</div>;
  }
}
 
ReactAmountField.propTypes = {
  value: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
  ]),
  children: PropTypes.node,
  onBlur: PropTypes.func,
  onChange: PropTypes.func,
  onDrop: PropTypes.func,
};
 
export default ReactAmountField;