import React, {PropTypes} from 'react'
import FormBuilderPropTypes from '../FormBuilderPropTypes'
import DefaultTextField from 'part:@sanity/components/textfields/default'

export default class Num extends React.Component {
  static displayName = 'Number';

  static propTypes = {
    field: FormBuilderPropTypes.field,
    level: PropTypes.number.isRequired,
    value: PropTypes.number,
    onChange: PropTypes.func,
    focus: PropTypes.bool
  };

  static defaultProps = {
    onChange() {}
  };

  constructor(props, context) {
    super(props, context)
    this.handleChange = this.handleChange.bind(this)
  }

  handleChange(e) {
    const val = e.target.value.trim()
    this.props.onChange({
      patch: {
        type: 'set',
        value: val === '' ? undefined : Number(e.target.value)
      }
    })
  }

  render() {
    const {value, field, focus, level} = this.props
    return (
      <DefaultTextField
        label={field.title}
        description={field.description}
        type="number"
        level={level}
        placeholder={field.placeholder || 'Must be a number. Ex 1234'}
        onChange={this.handleChange}
        onKeyPress={this.handleKeyPress}
        value={String(value)}
        focus={focus}
        ref={this.setInputElement}
      />
    )
  }
}

@@ -500,319 +500,345 @@
 %0A%0A  
-constructor(props, context) %7B%0A    super(props, context)%0A    this.handleChange = this.handleChange.bind(this)%0A  %7D%0A%0A  handleChange(e) %7B%0A    const val = e.target.value.trim()%0A    this.props.onChange(%7B%0A      patch: %7B%0A        type: 'set',%0A        value: val === '' ? undefined : Number(e.target.value)%0A      %7D%0A    %7D)
+handleChange = event =%3E %7B%0A    const eventValue = event.target.value%0A    let outputVal = Number(eventValue)%0A    if (eventValue === '') %7B%0A      outputVal = undefined%0A    %7D%0A    this.props.onChange(%7B%0A      patch: %7B%0A        type: 'set',%0A        value: outputVal%0A      %7D%0A    %7D)%0A  %7D%0A%0A  setInputElement = input =%3E %7B%0A    this.inputElement = input
 %0A  %7D
@@ -1173,56 +1173,54 @@
     
-onKeyPress=%7Bthis.handleKeyPress%7D%0A       
+value=%7Btypeof value === 'undefined' ?
  value
-=%7B
+ : 
 Stri
@@ -1256,43 +1256,8 @@
 us%7D%0A
-        ref=%7Bthis.setInputElement%7D%0A
     

Fix warning from Number input + remove unused and invalid code