import React, { Component } from "react";

class Amount extends Component {

  constructor(props) {
    super(props);
    this.state = {
      value: this.props.value || "",
      canEdit: this.props.canEditAmount === undefined || this.props.canEditAmount === true
    };
    this.handleChange = this.handleChange.bind(this);
    this.onKeyPress = this.onKeyPress.bind(this);
  }

  handleChange(event) {
    var parsed = parseFloat(event.target.value);
    if (parsed && !isNaN(parsed) && parsed >= this.props.min) {
      var split = event.target.value.split(".");
      if (split.length < 2 || split[1].length < 3) {
        this.setState({value: parsed});
        if (this.props.onValueChanged) {
          this.props.onValueChanged(parsed);
        }
      }
    }
  }

  onKeyPress(event) {
    if (event.key === "Enter") {
      event.preventDefault();
      if (this.props.onEnterKey) {
        this.props.onEnterKey(event);
      }
    }
  }

  render() {
    return (
      <div className="input-group">
        <span className="input-group-addon" aria-hidden="true">{this.props.currency || "£"}</span>
        <input name={this.props.name || ""}
               className="form-control"
               placeholder={this.props.placeholder || "0.00"}
               min={this.props.min || "0.00"}
               max={this.props.max || ""}
               step="0.01"
               type="number"
               disabled={!this.state.canEdit}
               value={this.state.value || ""}
               onChange={this.handleChange}
               onKeyPress={this.onKeyPress}
               aria-label={this.props.label || ""}
               label={this.props.label || ""} />
      </div>
    );
  }
}

export default Amount;

@@ -402,60 +402,253 @@
 ) %7B%0A
-    var parsed = parseFloat(event.target.value);%0A   
+%0A    var value = event.target.value;%0A%0A    if (!value) %7B%0A      this.setState(%7B value: parsed %7D);%0A      return;%0A    %7D%0A%0A    var parsed = parseFloat(event.target.value);%0A%0A    if (parsed === 0) %7B%0A      this.setState(%7B value: parsed %7D);%0A    %7D%0A    else
  if 
@@ -829,16 +829,17 @@
 tState(%7B
+ 
 value: p
@@ -843,16 +843,17 @@
 : parsed
+ 
 %7D);%0A    
@@ -1355,21 +1355,16 @@
         
-     
 classNam
@@ -1390,21 +1390,16 @@
         
-    
- 
 placehol
@@ -1447,21 +1447,16 @@
         
-     
 min=%7Bthi
@@ -1488,21 +1488,16 @@
         
-  
-   
 max=%7Bthi
@@ -1507,37 +1507,32 @@
 rops.max %7C%7C %22%22%7D%0A
-     
           step=%22
@@ -1547,21 +1547,16 @@
         
-     
 type=%22nu
@@ -1571,21 +1571,16 @@
         
-   
-  
 disabled
@@ -1612,21 +1612,16 @@
         
-     
 value=%7Bt
@@ -1635,28 +1635,18 @@
 te.value
- %7C%7C %22%22%7D%0A    
+%7D%0A
         
@@ -1647,17 +1647,16 @@
         
- 
 onChange
@@ -1676,21 +1676,16 @@
 Change%7D%0A
-     
         
@@ -1725,21 +1725,16 @@
         
-  
-   
 aria-lab
@@ -1757,29 +1757,24 @@
 abel %7C%7C %22%22%7D%0A
-     
           la

Fix handling of zero for amounts