import React, { Component, PropTypes } from 'react'
import { cloneDeep } from 'lodash'

import autoBind from '../utils/autoBind'
import getTypeProps from '../utils/getTypeProps'

import Struct from './Struct' // eslint-disable-line

class ObjectEditor extends Component {

  constructor() {
    super()

    this.state = {}

    autoBind(this, [ 'updateStateKey', 'createChangeHandler' ])
  }

  componentWillMount() {
    this.setState(this.props.value)
  }

  componentWillReceiveProps(nextProps) {
    this.setState(nextProps.value)
  }

  updateStateKey(key, val) {
    this.setState({ [key]: val }, () => this.props.onChange(this.state))
  }

  createChangeHandler(key) {
    return this.updateStateKey.bind(this, key)
  }

  render() {
    const { struct: { objectStruct } } = this.props
    const value = cloneDeep(this.state)

    Object.keys(objectStruct).map((key, index) => (
      <Struct
        key={index}
        value={value[key]}
        struct={objectStruct[key]}
        onChange={this.createChangeHandler(key)}
        {...getTypeProps(this.props)}
      />
    ))
  }
}

ObjectEditor.propTypes = {
  value: PropTypes.object.isRequired,
  struct: PropTypes.object.isRequired,
  onChange: PropTypes.func.isRequired,
  textTypes: PropTypes.object.isRequired,
  arrayTypes: PropTypes.object.isRequired,
  numberTypes: PropTypes.object.isRequired,
  optionTypes: PropTypes.object.isRequired,
  objectTypes: PropTypes.object.isRequired,
  booleanTypes: PropTypes.object.isRequired,
  keyValueTypes: PropTypes.object.isRequired,
  collectionTypes: PropTypes.object.isRequired
}

export default ObjectEditor

@@ -832,16 +832,46 @@
 e)%0A%0A    
+return (%0A      %3Cdiv%3E%0A        %7B
 Object.k
@@ -920,16 +920,20 @@
     
+    
 %3CStruct%0A
     
@@ -928,16 +928,20 @@
 %3CStruct%0A
+    
         
@@ -960,16 +960,20 @@
         
+   
+ 
 value=%7Bv
@@ -979,24 +979,28 @@
 value%5Bkey%5D%7D%0A
+    
         stru
@@ -1030,16 +1030,20 @@
         
+ 
+   
 onChange
@@ -1083,16 +1083,20 @@
         
+  
+  
 %7B...getT
@@ -1127,16 +1127,44 @@
     
-/%3E%0A    )
+    /%3E%0A        ))%7D%0A      %3C/div%3E%0A    
 )%0A  

fix render and rebuild