var React       = require('react/addons');

var _val = {};

module.exports = React.createClass({

  /**
   * The all data in this component is stored 
   * in the value key of an object.  This is
   * to ensure that parameters are always
   * passed by reference instead of by value
   * (hence preserving all relationships).
   *
   * This function iterates through an object,
   * removing all the "value" keys.
   */
  getRealVal: function (val) {
    var realVal;
    if (Array.isArray(val.value)) {
      realVal = val.value.map(function (v) {
        return this.getRealVal(v);
      }.bind(this));
    } else {
      realVal = val.value;
    }
    return realVal;
  },

  /**
   * Calls the change handler supplied
   * to this component.
   */
  emitChange: function () {
    var val;
    if (this.props.onChange) {
      val = this.getRealVal(_val);
      this.props.onChange(val);
    }
  },

  /**
   * Updates the value of an integer input.
   * 
   * Params:
   *
   * - __val__ : The value object to update
   * - __event__: The click event that was triggered
   */
  updateIntegerVal: function (val, event) {
    val.value = parseInt(event.target.value);
    this.emitChange();
  },

  /**
   * Adds an item to an array input.
   *
   * Params:
   *
   * - __val__: The value object of the array to append to
   */
  addItemToArray: function (val) {
    val.value.push({})
    this.forceUpdate();
  },

  /**
   * Constructs an array input
   *
   * Params:
   *
   * - __schema__:        The schema for the array input
   * - __currentValue__:  The current value of the object
   * - __valueObject__:   The value object to be used for the array
   */
  buildArray: function (schema, currentValue, valueObject) {
    if (currentValue) {
      valueObject.value = currentValue;
    }
    if (!valueObject.value) {
      valueObject.value = [];
    }
    return (
      <ul>
        {schema.label ? <li>{schema.label}</li> : null}
        {valueObject.value.map(function (value, index) {
          return (
            <li>
              <span>{index}: </span>
              {this.buildItem(schema.items, currentValue[index], value)}
            </li>
          );
        }.bind(this))}
        <li>
          <button onClick={this.addItemToArray.bind(this, valueObject)}>Add Item</button>
        </li>
      </ul>
    );
  },

  /**
   * Constructs an integer input
   *
   * Params:
   *
   * - __schema__:        The schema for the integer input
   * - __currentValue__:  The current value of the input
   * - __valueObject__:   The value object to be used for the input
   */
  buildInteger: function (schema, currentValue, valueObject) {
    var placeholder = schema.label || '';
    if (currentValue) {
      valueObject.value = currentValue;
    }
    return (
      <input 
        type="number" 
        pattern="\d*" 
        placeholder={placeholder}  
        value={currentValue}
        onChange={this.updateIntegerVal.bind(this, valueObject)} />
    );
  },

  /**
   * Constructs a generic item
   *
   * Params:
   *
   * - __schema__:        The schema for the generic input item
   * - __currentValue__:  The current value of the item
   * - __valueObject__:   The value object to be used for the item
   */
  buildItem: function (schema, currentValue, valueObject) {
    switch (schema.type) {
      case "integer":
        return this.buildInteger(schema, currentValue, valueObject);

      case "array":
        return this.buildArray(schema, currentValue, valueObject);

      default:
        return (<fieldset />);
    }
  },

  render: function () {
    return this.buildItem(this.props.validation, this.props.data, _val);
  }
});
@@ -51,16 +51,40 @@
 al = %7B%7D;
+%0Avar firstRender = true;
 %0A%0Amodule
@@ -1404,16 +1404,40 @@
 push(%7B%7D)
+;%0A    this.emitChange();
 %0A    thi
@@ -1580,66 +1580,8 @@
 put%0A
-   * - __currentValue__:  The current value of the object%0A
    *
@@ -1644,16 +1644,74 @@
 e array%0A
+   * - __currentValue__:  The current value of the object%0A
    */%0A  
@@ -1744,33 +1744,33 @@
 ma, 
-currentValue, valueObject
+valueObject, currentValue
 ) %7B%0A
@@ -1823,32 +1823,95 @@
 e = currentValue
+.map(function (item) %7B%0A        return %7B value: item %7D;%0A      %7D)
 ;%0A    %7D%0A    if (
@@ -2221,16 +2221,23 @@
 a.items,
+ value,
  current
@@ -2248,23 +2248,16 @@
 e%5Bindex%5D
-, value
 )%7D%0A     
@@ -2572,65 +2572,8 @@
 put%0A
-   * - __currentValue__:  The current value of the input%0A
    *
@@ -2632,24 +2632,81 @@
 r the input%0A
+   * - __currentValue__:  The current value of the input%0A
    */%0A  buil
@@ -2737,33 +2737,33 @@
 ma, 
-currentValue, valueObject
+valueObject, currentValue
 ) %7B%0A
@@ -3221,64 +3221,8 @@
 tem%0A
-   * - __currentValue__:  The current value of the item%0A
    *
@@ -3280,24 +3280,80 @@
 or the item%0A
+   * - __currentValue__:  The current value of the item%0A
    */%0A  buil
@@ -3381,33 +3381,33 @@
 ma, 
-currentValue, valueObject
+valueObject, currentValue
 ) %7B%0A
@@ -3500,33 +3500,33 @@
 ma, 
-currentValue, valueObject
+valueObject, currentValue
 );%0A%0A
@@ -3588,33 +3588,33 @@
 ma, 
-currentValue, valueObject
+valueObject, currentValue
 );%0A%0A
@@ -3695,30 +3695,55 @@
 on () %7B%0A    
-return
+var elem = firstRender %0A      ?
  this.buildI
@@ -3768,16 +3768,22 @@
 idation,
+ _val,
  this.pr
@@ -3794,15 +3794,79 @@
 data
-, _val)
+) %0A      : this.buildItem(this.props.validation, _val);%0A    return elem
 ;%0A  

fix modifying existing data for sequence